@brightspace-ui/labs 2.3.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -54,5 +54,5 @@
54
54
  "@brightspace-ui/core": "^3",
55
55
  "lit": "^3"
56
56
  },
57
- "version": "2.3.0"
57
+ "version": "2.4.1"
58
58
  }
@@ -19,6 +19,7 @@ The `d2l-labs-attribute-picker` component is an autocompleting dropdown to choos
19
19
  | `attribute-list` | Array | An array of string/value pairs representing the attributes currently selected in the picker (eg `[{"name":"shown to user","value":"sent in event"}]`). Only the values are sent in events and the string names are otherwise ignored. |
20
20
  | `assignable-attributes` | Array | An array of string/value pairs, just like `attribute-list`, available in the dropdown list |
21
21
  | `invalid-tooltip-text` | String (default: At least one attribute must be set) | The text that will appear in the tooltip that informs a user that the state is invalid |
22
+ | `label` | String, Required | The label associated with the attribute picker for screen reader users |
22
23
  | `limit` | Number | The maximum length of attribute-list permitted |
23
24
  | `required` | Boolean | When true, an error state will appear if no attributes are set. Error state only appear once the user interacts with the component. |
24
25
 
@@ -14,7 +14,7 @@ class ViewToggle extends LitElement {
14
14
  },
15
15
  selectedOption: {
16
16
  type: String,
17
- attribute: 'selected-options'
17
+ attribute: 'selected-option'
18
18
  }
19
19
  };
20
20
  }
@@ -1,171 +1,194 @@
1
- # ComputedValues Controller
2
-
3
- The `ComputedValues` (plural) controller allows you to define a collection of `ComputedValue` (singular) controllers. Each of these `ComputedValue` controllers holds a value that is dependent on other variables or properties within a component and will automatically update that value whenever the dependencies change.
4
-
5
- The main benefit of this controller is that it gives a quick and clean way of definining the update and computation logic of an instance member that is dependent on other members or properties.
6
-
7
- Some use cases for this controller include:
8
- * Computing values that you want to show in render, but whose computation is too expensive to perform every render (ex: filtering or sorting a list in the front end based on user input).
9
- * Asynchronously computing values whenever a dependency changes (ex: refetching a value from the API any time a specific property changes).
10
- * Creating a reusable controller that knows how to recompute a value based on dependencies.
11
-
12
- ## `ComputedValues` vs `ComputedValue`
13
-
14
- Internally, the `ComputedValues` (plural) controller uses another controller called `ComputedValue` (singular). The `ComputedValue` controller holds the majority of the functionality that `ComputedValues` uses and the `ComputedValues` controller simply instantiates a collection of `ComputedValue` controllers and assigns them to itself for easy access.
15
-
16
- While it's possible to use the `ComputedValue` controller directly, it is recommended that the `ComputedValues` controller be used in most cases, since it makes it easier to create a collection of computed values. Direct use of the `ComputedValue` controller should be reserved for creating reusable controllers.
17
-
18
- ## Usage
19
-
20
- Create an instance of the `ComputedValues` controller and assign it to a member variable. Pass the host and an array of objects to the constructor that each represent one value that will be computed based on dependencies.
21
-
22
- ```js
23
- import ComputedValues from '@brightspace-ui/labs/controllers/computed-values.js';
24
-
25
- class DemoComponent extends LitElement {
26
- static properties = {
27
- firstName: { type: String },
28
- lastName: { type: String }
29
- };
30
-
31
- constructor() {
32
- super();
33
- this.firstName = 'John';
34
- this.lastName = 'Smith';
35
- }
36
-
37
- render() {
38
- return html`
39
- <p>
40
- fullName: ${this._computed.fullName.value}<br>
41
- screamingFullName: ${this._computed.screamingFullName.value}<br>
42
- shortName: ${this._computed.shortName.value}<br>
43
- asyncName: ${this._computed.asyncName.pending ? '<loading...>' : this._computed.asyncName.value}<br>
44
- </p>
45
- `;
46
- }
47
-
48
- _computed = new ComputedValues(this, [{
49
- // This computed value is dependent on both firstName and lastName
50
- name: 'fullName',
51
- initialValue: '',
52
- getDependencies: () => [this.firstName, this.lastName],
53
- compute: (firstName, lastName) => `${firstName} ${lastName}`
54
- }, {
55
- // This computed value is dependent on the fullName computed value
56
- name: 'screamingFullName',
57
- initialValue: '',
58
- getDependencies: () => [this._computed.fullName.value],
59
- compute: (fullName) => fullName.toUpperCase()
60
- }, {
61
- // This computed value implements a custom shouldCompute method
62
- name: 'shortName',
63
- initialValue: '',
64
- isAsync: true,
65
- getDependencies: () => [this.firstName, this.lastName],
66
- shouldCompute: (prevDeps, currDeps) => {
67
- if (prevDeps === null) return true;
68
-
69
- const [prevFirstName, prevLastName] = prevDeps;
70
- const [currFirstName, currLastName] = currDeps;
71
-
72
- return prevFirstName[0] !== currFirstName[0] || prevLastName !== currLastName;
73
- },
74
- compute: (firstName, lastName) => {
75
- return `${firstName[0]}. ${lastName}`;
76
- }
77
- }, {
78
- // This computed value is asynchronous
79
- name: 'asyncName',
80
- initialValue: '',
81
- isAsync: true,
82
- getDependencies: () => [this.firstName, this.lastName],
83
- compute: async(firstName, lastName) => {
84
- return await asyncApiCall(firstName, lastName);
85
- }
86
- }]);
87
- }
88
- ```
89
-
90
- Check the demo page for additional working examples.
91
-
92
- ## Compute Lifecycle
93
-
94
- There is a flow of steps that each `ComputedValue` instance follows in order to compute its value. Being aware of how this controller works is important to understand how best to make use of it.
95
-
96
- ### Host `update`
97
-
98
- The compute lifecycle starts with the host executing its `update` method, which then triggers the controller's `hostUpdate`.
99
-
100
- All computation logic for this controller starts here. So, if the host's `update` method isn't called (for example by never triggering one or by explicitly using `shouldUpdate` to skip updates), then computes will never be called.
101
-
102
- This also means that `compute` functions should not look at the dom directly, since the logic for calculating the updated value happens before the render executes.
103
-
104
- ### Controller `getDependencies` and `shouldCompute`
105
-
106
- The `getDependencies` and `shouldCompute` functions are important in order to decide whether to execute the `compute` function. As such, these two functions are called every host `update` for each `ComputedValue` instance.
107
-
108
- The `getDependencies` function must always be defined by the consumer, whereas the `shouldCompute` function has an internal default if the consumer does not define their own.
109
-
110
- The default `shouldCompute` function will do an indentity comparison for each of the previous and current dependencies one by one and return true immediately if one of the dependencies has changed.
111
-
112
- While this controller can be very helpful for making sure to only execute heavy computations when needed, be aware of the performance costs of using this approach. So, keep in mind what processes occur each `update` call.
113
-
114
- ### Controller `compute`
115
-
116
- Once the `shouldCompute` function returns true during the `update` step, the `compute` function will be called.
117
-
118
- If the controller is set to run synchronously, the `compute` function will execute in its entirety and the return value will be assigned to the controller's `value` instance member. This all happens during the host's `update` step.
119
-
120
- If the controller is set to run asynchronously, the controller will call the `compute` function and expect a promise as the return value. It will update its async statuses as appropriate before continuing with the host's `update` step. Once the promise returned from the `compute` function resolves, the controller will assign the result to the controller's `value` instance member, it will update its async statuses as appropriate, and will then request and update from the host using `requestUpdate`.
121
-
122
- Note that if the controller is asynchronous and a controller's `compute` functions is called while a previous `compute` call is in progress, only the last `compute` function call will update the value and async statuses.
123
-
124
- ## Order of `ComputeValue` Instances
125
-
126
- The compute lifecycle for each `ComputeValue` controller instance will be executed in its entirety before moving on to the next. This means that the order that these instances are defined in the array matters in some cases. If one of the controller instances is dependent on the result from another, then the one that is being depended on must come first in the order to make sure the most up-to-date value is passed to the dependant.
127
-
128
- ## `ComputedValues` Instance Methods
129
-
130
- ### Constructor
131
-
132
- | Parameter Name | Type | Description | Required |
133
- |---|---|---|---|
134
- | `host` | LitElement | The host for the controller. | Yes |
135
- | `valuesOptions` | Array | The array of objects that each define a computed value. | Yes |
136
- | `valuesOptions[i].name` | String | The name of the computed value. Used to assign the internal `ComputedValue` instance to the `ComputedValues` instance. | Yes |
137
- | `valuesOptions[i].Controller` | Class | The controller instantiated internally for this particular value. By default this uses the `ComputedValue` controller, but it can be overriden with a custom controller. | |
138
- | `...valuesOptions[i]` | Object | The rest of the attributes for the object are passed to the internal `ComputedValue` instance constructor. See the `ComputedValue` constructor for details. | Yes |
139
-
140
- ## `ComputedValues` Instance Members
141
-
142
- | Member Name | Type | Description |
143
- |---|---|---|
144
- |`this[name]` | `ComputedValue` controller | For each of the objects in the `valuesOptions` array, a `ComputedValue` controller is instantiated and assigned to a memeber on the `ComputedValues` controller instance.<br><br>For example, the following `ComputedValues` instance...<br><br><pre>class MyElement extends LitElement {<br> _computed = new ComputedValues(this, [{<br> name: 'myValue',<br> ...<br> }]);<br>}</pre><br>...will end up with a `ComputedValue` instance assigned to `this._computed.myValue`.<br><br>For details on what instance members each `ComputedValue` instance has, see the `ComputedValue` Instance Members section below. |
145
-
146
- ## `ComputedValue` Instance Methods
147
-
148
- ### Constructor
149
-
150
- | Parameter Name | Type | Description | Required |
151
- |---|---|---|---|
152
- | `host` | Lit Element | The host for the controller. | Yes |
153
- | `options` | Object | A collection of options for the controller. | Yes |
154
- | `options.getDependencies` | Function() : Array | The function used to get the array of dependencies for this value. Must return an array and the array should always be the same length. The previous and current dependencies will be used to decide whether or not to update, so they should be kept in the same order as well. | Yes |
155
- | `options.compute` | Function(...Any) : Any | The function used to calculate the computed value. It's passed the most recent dependencies every time it is called, and the return value is assigned to the controller value member before each render. | Yes |
156
- | `options.initialValue` | Any | The value of the controller value member before the first update occurs. | |
157
- | `options.shouldCompute` | Function(Array, Array) : Bool | The function used to decide whether or not to run the compute function.<br><br>This function is passed an array of the previous dependencies and an array of the current dependencies. It must return a boolean representing whether to call the compute function or not.<br><br>If not assigned, the default `shouldCompute` function will do an indentity comparison for each of the previous and current dependencies one by one and return true immediately if one of the dependencies has changed. | |
158
- | `options.isAsync` | Bool | This tells the controller whether the compute function is asynchronous. If this is true, the compute function must return a promise. | |
159
- | `options.shouldRequestUpdate` | Function(Object, Object) : Bool | This function is used to decide whether or not to call the host's `requestUpdate` method after an async `compute` function finished updating the value.<br><br>This function is passed an object that contains the value and async status before the compute finished executing, and one object with the current value and async status. It must return a boolean representing whether to call the `requestUpdate` method or not.<br><br>If not assigned, this defaults to a function that always returns true. | |
160
-
161
- ## `ComputedValue` Instance Members
162
-
163
- | Member Name | Type | Description |
164
- |---|---|---|
165
- | `host` | LitElement | The host element of this controller. |
166
- | `value` | Any | This holds the computed value of the controller. |
167
- | `pending` | Bool | Only used when the controller is async. This will be true while an async compute is processing, and false otherwise. |
168
- | `success` | Bool\|Null | Only used when the controller is async. This will be set to true if the previous async compute resolved successfully. This will be set to false if the previous async compute threw an error. This will be set to null if no async compute function has completed yet. |
169
- | `error` | Any | Only used when the controller is async. This will hold error info in the event that previous async compute threw an error. This will be assigned null otherwise. |
170
- | `asyncStatus` | String | Only used when the controller is async. This holds a string representing the current async status of the controller.<br>`"initial"`: Before the first compute function is called.<br>`"pending"`: While an async compute function is in progress.<br>`"success"`: If the last async compute was successful (and another is not currently pending).<br>`"error"`: If the last async compute ended in error (and another is not currently pending). |
171
- | `computeComplete` | Promise | This member is assigned a promise whenever the value starts being computed, and that promise resolves when the value is done being computed. Note that this is intended to be used for testing and should not be relied on for normal use cases. |
1
+ # ComputedValues Controller
2
+
3
+ The `ComputedValues` (plural) controller allows you to define a collection of `ComputedValue` (singular) controllers. Each of these `ComputedValue` controllers holds a value that is dependent on other variables or properties within a component and will automatically update that value whenever the dependencies change.
4
+
5
+ The main benefit of this controller is that it gives a quick and clean way of definining the update and computation logic of an instance member that is dependent on other members or properties.
6
+
7
+ Some use cases for this controller include:
8
+ * Computing values that you want to show in render, but whose computation is too expensive to perform every render (ex: filtering or sorting a list in the front end based on user input).
9
+ * Asynchronously computing values whenever a dependency changes (ex: refetching a value from the API any time a specific property changes).
10
+ * Creating a reusable controller that knows how to recompute a value based on dependencies.
11
+
12
+ ## `ComputedValues` vs `ComputedValue`
13
+
14
+ Internally, the `ComputedValues` (plural) controller uses another controller called `ComputedValue` (singular). The `ComputedValue` controller holds the majority of the functionality that `ComputedValues` uses and the `ComputedValues` controller simply instantiates a collection of `ComputedValue` controllers and assigns them to itself for easy access.
15
+
16
+ While it's possible to use the `ComputedValue` controller directly, it is recommended that the `ComputedValues` controller be used in most cases, since it makes it easier to create a collection of computed values. Direct use of the `ComputedValue` controller should be reserved for creating reusable controllers.
17
+
18
+ ## Usage
19
+
20
+ Create an instance of the `ComputedValues` controller and assign it to a member variable. Pass the host and an array of objects to the constructor that each represent one value that will be computed based on dependencies.
21
+
22
+ ```js
23
+ import ComputedValues from '@brightspace-ui/labs/controllers/computed-values.js';
24
+
25
+ class DemoComponent extends LitElement {
26
+ static properties = {
27
+ firstName: { type: String },
28
+ lastName: { type: String }
29
+ };
30
+
31
+ constructor() {
32
+ super();
33
+ this.firstName = 'John';
34
+ this.lastName = 'Smith';
35
+ }
36
+
37
+ render() {
38
+ return html`
39
+ <p>
40
+ fullName: ${this._computed.fullName.value}<br>
41
+ screamingFullName: ${this._computed.screamingFullName.value}<br>
42
+ shortName: ${this._computed.shortName.value}<br>
43
+ asyncName: ${this._computed.asyncName.pending ? '<loading...>' : this._computed.asyncName.value}<br>
44
+ </p>
45
+ `;
46
+ }
47
+
48
+ _computed = new ComputedValues(this, [{
49
+ // This computed value is dependent on both firstName and lastName
50
+ name: 'fullName',
51
+ initialValue: '',
52
+ getDependencies: () => [this.firstName, this.lastName],
53
+ compute: (firstName, lastName) => `${firstName} ${lastName}`
54
+ }, {
55
+ // This computed value is dependent on the fullName computed value
56
+ name: 'screamingFullName',
57
+ initialValue: '',
58
+ getDependencies: () => [this._computed.fullName.value],
59
+ compute: (fullName) => fullName.toUpperCase()
60
+ }, {
61
+ // This computed value implements a custom shouldCompute method
62
+ name: 'shortName',
63
+ initialValue: '',
64
+ isAsync: true,
65
+ getDependencies: () => [this.firstName, this.lastName],
66
+ shouldCompute: (prevDeps, currDeps) => {
67
+ if (prevDeps === null) return true;
68
+
69
+ const [prevFirstName, prevLastName] = prevDeps;
70
+ const [currFirstName, currLastName] = currDeps;
71
+
72
+ return prevFirstName[0] !== currFirstName[0] || prevLastName !== currLastName;
73
+ },
74
+ compute: (firstName, lastName) => {
75
+ return `${firstName[0]}. ${lastName}`;
76
+ }
77
+ }, {
78
+ // This computed value is asynchronous
79
+ name: 'asyncName',
80
+ initialValue: '',
81
+ isAsync: true,
82
+ getDependencies: () => [this.firstName, this.lastName],
83
+ compute: async(firstName, lastName) => {
84
+ return await asyncApiCall(firstName, lastName);
85
+ }
86
+ }]);
87
+ }
88
+ ```
89
+
90
+ Check the demo page for additional working examples.
91
+
92
+ ## Compute Lifecycle
93
+
94
+ There is a flow of steps that each `ComputedValue` instance follows in order to compute its value. Being aware of how this controller works is important to understand how best to make use of it.
95
+
96
+ ### Host `update`
97
+
98
+ The compute lifecycle starts with the host executing its `update` method, which then triggers the controller's `hostUpdate`.
99
+
100
+ All computation logic for this controller starts here. So, if the host's `update` method isn't called (for example by never triggering one or by explicitly using `shouldUpdate` to skip updates), then computes will never be called.
101
+
102
+ This also means that `compute` functions should not look at the dom directly, since the logic for calculating the updated value happens before the render executes.
103
+
104
+ ### Controller `getDependencies` and `shouldCompute`
105
+
106
+ The `getDependencies` and `shouldCompute` functions are important in order to decide whether to execute the `compute` function. As such, these two functions are called every host `update` for each `ComputedValue` instance.
107
+
108
+ The `getDependencies` function must always be defined by the consumer, whereas the `shouldCompute` function has an internal default if the consumer does not define their own.
109
+
110
+ The default `shouldCompute` function will do an indentity comparison for each of the previous and current dependencies one by one and return true immediately if one of the dependencies has changed.
111
+
112
+ While this controller can be very helpful for making sure to only execute heavy computations when needed, be aware of the performance costs of using this approach. So, keep in mind what processes occur each `update` call.
113
+
114
+ ### Controller `compute`
115
+
116
+ Once the `shouldCompute` function returns true during the `update` step, the `compute` function will be called.
117
+
118
+ If the controller is set to run synchronously, the `compute` function will execute in its entirety and the return value will be assigned to the controller's `value` instance member. This all happens during the host's `update` step.
119
+
120
+ If the controller is set to run asynchronously, the controller will call the `compute` function and expect a promise as the return value. It will update its async statuses as appropriate before continuing with the host's `update` step. Once the promise returned from the `compute` function resolves, the controller will assign the result to the controller's `value` instance member, it will update its async statuses as appropriate, and will then request and update from the host using `requestUpdate`.
121
+
122
+ Note that if the controller is asynchronous and a controller's `compute` functions is called while a previous `compute` call is in progress, only the last `compute` function call will update the value and async statuses.
123
+
124
+ ## Order of `ComputeValue` Instances
125
+
126
+ The compute lifecycle for each `ComputeValue` controller instance will be executed in its entirety before moving on to the next. This means that the order that these instances are defined in the array matters in some cases. If one of the controller instances is dependent on the result from another, then the one that is being depended on must come first in the order to make sure the most up-to-date value is passed to the dependant.
127
+
128
+ ## `ComputedValues` Instance Methods
129
+
130
+ ### Constructor
131
+
132
+ | Parameter Name | Type | Description | Required |
133
+ |---|---|---|---|
134
+ | `host` | LitElement | The host for the controller. | Yes |
135
+ | `valuesOptions` | Array | The array of objects that each define a computed value. | Yes |
136
+ | `valuesOptions[i].name` | String | The name of the computed value. Used to assign the internal `ComputedValue` instance to the `ComputedValues` instance. | Yes |
137
+ | `valuesOptions[i].Controller` | Class | The controller instantiated internally for this particular value. By default this uses the `ComputedValue` controller, but it can be overriden with a custom controller. | |
138
+ | `...valuesOptions[i]` | Object | The rest of the attributes for the object are passed to the internal `ComputedValue` instance constructor. See the `ComputedValue` constructor for details. | Yes |
139
+
140
+ ### `tryUpdate()`
141
+
142
+ This method calls the `tryUpdate()` method for each of its computed values in order. See the `ComputedValue` Instance Methods section below for more details.
143
+
144
+ ## `ComputedValues` Instance Members
145
+
146
+ | Member Name | Type | Description |
147
+ |---|---|---|
148
+ |`this[name]` | `ComputedValue` controller | For each of the objects in the `valuesOptions` array, a `ComputedValue` controller is instantiated and assigned to a memeber on the `ComputedValues` controller instance.<br><br>For example, the following `ComputedValues` instance...<br><br><pre>class MyElement extends LitElement {<br> _computed = new ComputedValues(this, [{<br> name: 'myValue',<br> ...<br> }]);<br>}</pre><br>...will end up with a `ComputedValue` instance assigned to `this._computed.myValue`.<br><br>For details on what instance members each `ComputedValue` instance has, see the `ComputedValue` Instance Members section below. |
149
+
150
+ ## `ComputedValue` Instance Methods
151
+
152
+ ### `constructor(host, options)`
153
+
154
+ | Parameter Name | Type | Description | Required |
155
+ |---|---|---|---|
156
+ | `host` | Lit Element | The host for the controller. | Yes |
157
+ | `options` | Object | A collection of options for the controller. | Yes |
158
+ | `options.getDependencies` | Function() : Array | The function used to get the array of dependencies for this value. Must return an array and the array should always be the same length. The previous and current dependencies will be used to decide whether or not to update, so they should be kept in the same order as well. | Yes |
159
+ | `options.compute` | Function(...Any) : Any | The function used to calculate the computed value. It's passed the most recent dependencies every time it is called, and the return value is assigned to the controller value member before each render. | Yes |
160
+ | `options.initialValue` | Any | The value of the controller value member before the first update occurs. | |
161
+ | `options.shouldCompute` | Function(Array, Array) : Bool | The function used to decide whether or not to run the compute function.<br><br>This function is passed an array of the previous dependencies and an array of the current dependencies. It must return a boolean representing whether to call the compute function or not.<br><br>If not assigned, the default `shouldCompute` function will do an indentity comparison for each of the previous and current dependencies one by one and return true immediately if one of the dependencies has changed. | |
162
+ | `options.isAsync` | Bool | This tells the controller whether the compute function is asynchronous. If this is true, the compute function must return a promise. | |
163
+ | `options.shouldRequestUpdate` | Function(Object, Object) : Bool | This function is used to decide whether or not to call the host's `requestUpdate` method after an async `compute` function finished updating the value.<br><br>This function is passed an object that contains the value and async status before the compute finished executing, and one object with the current value and async status. It must return a boolean representing whether to call the `requestUpdate` method or not.<br><br>If not assigned, this defaults to a function that always returns true. | |
164
+
165
+ ### `asyncRender(renderMap)`
166
+
167
+ This method is used to render different things depending on the async state of the computed value. If the computed value is not async, this method will always return `undefined`.
168
+
169
+ | Parameter Name | Type | Description | Required |
170
+ |---|---|---|---|
171
+ | `renderMap` | Object | This object must contain different functions to call depending on the async state of the computed value. | Yes |
172
+ | `renderMap.pending` | Function() : Any | If the current async state of the computed value is "pending", this function is called when calling `asyncRender`. The function is passed no arguments, and the return value from it is returned from `asyncRender`. | No |
173
+ | `renderMap.success` | Function(Any) : Any | If the current async state of the computed value is "success", this function is called when calling `asyncRender`. The function is passed the result of the computed value as its first arguement, and the return value from it is returned from `asyncRender`. | No |
174
+ | `renderMap.success` | Function(Any) : Any | If the current async state of the computed value is "error", this function is called when calling `asyncRender`. The function is passed the result of the error value as its first arguement, and the return value from it is returned from `asyncRender`. | No |
175
+
176
+ ### `tryUpdate()`
177
+
178
+ This method checks if the dependencies have changed since it was last called and runs the compute process if they have. It is called automatically by the controller in between the `willUpdate` and `render` steps of the host's render cycle.
179
+
180
+ There is normally no need to explicitly call this method since it's automatically called as part of the host's render cycle. However, calling this yourself might be useful if you wish to have the compute process run earlier than it normally would (e.g. during `willUpdate`) in order to make use the resulting value before getting to the render step.
181
+
182
+ This method takes no arguments and returns `true` if it detected that the dependencies changed and ran the compute process.
183
+
184
+ ## `ComputedValue` Instance Members
185
+
186
+ | Member Name | Type | Description |
187
+ |---|---|---|
188
+ | `host` | LitElement | The host element of this controller. |
189
+ | `value` | Any | This holds the computed value of the controller. |
190
+ | `pending` | Bool | Only used when the controller is async. This will be true while an async compute is processing, and false otherwise. |
191
+ | `success` | Bool\|Null | Only used when the controller is async. This will be set to true if the previous async compute resolved successfully. This will be set to false if the previous async compute threw an error. This will be set to null if no async compute function has completed yet. |
192
+ | `error` | Any | Only used when the controller is async. This will hold error info in the event that previous async compute threw an error. This will be assigned null otherwise. |
193
+ | `asyncStatus` | String | Only used when the controller is async. This holds a string representing the current async status of the controller.<br>`"initial"`: Before the first compute function is called.<br>`"pending"`: While an async compute function is in progress.<br>`"success"`: If the last async compute was successful (and another is not currently pending).<br>`"error"`: If the last async compute ended in error (and another is not currently pending). |
194
+ | `computeComplete` | Promise | This member is assigned a promise whenever the value starts being computed, and that promise resolves when the value is done being computed. Note that this is intended to be used for testing and should not be relied on for normal use cases. |
@@ -66,14 +66,30 @@ export default class ComputedValue {
66
66
  return ASYNC_STATUSES.ERROR;
67
67
  }
68
68
 
69
+ asyncRender(renderStates = {}) {
70
+ switch (this.asyncStatus) {
71
+ case ASYNC_STATUSES.SUCCESS:
72
+ return renderStates[ASYNC_STATUSES.SUCCESS]?.(this.value);
73
+ case ASYNC_STATUSES.ERROR:
74
+ return renderStates[ASYNC_STATUSES.ERROR]?.(this.error);
75
+ case ASYNC_STATUSES.PENDING:
76
+ return renderStates[ASYNC_STATUSES.PENDING]?.();
77
+ }
78
+ }
79
+
69
80
  hostUpdate() {
81
+ this.tryUpdate();
82
+ }
83
+
84
+ tryUpdate() {
70
85
  const currDependencies = this._getDependencies();
71
86
  const shouldCompute = this._shouldCompute(this._prevDependencies, currDependencies);
72
87
  this._prevDependencies = currDependencies;
73
88
 
74
- if (shouldCompute) {
75
- this._updateValue(currDependencies);
76
- }
89
+ if (!shouldCompute) return false;
90
+
91
+ this._updateValue(currDependencies);
92
+ return true;
77
93
  }
78
94
 
79
95
  _compute;
@@ -2,8 +2,13 @@ import ComputedValue from './computed-value.js';
2
2
 
3
3
  export default class ComputedValues {
4
4
  constructor(host, valuesOptions = []) {
5
+ this._valuesOptions = valuesOptions;
5
6
  valuesOptions.forEach(({ Controller = ComputedValue, name, ...options }) => {
6
7
  this[name] = new Controller(host, options);
7
8
  });
8
9
  }
10
+
11
+ tryUpdate() {
12
+ this._valuesOptions.forEach(({ name }) => this[name].tryUpdate());
13
+ }
9
14
  }