You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function-based resources are _implicitly_ reactive,
58
-
in that there is no ceramony required by the consumer to make them reactive
57
+
Function-based resources are _implicitly_ reactive,
58
+
in that there is no ceremony required by the consumer to make them reactive
59
59
or update in response to changes in reactive source-data.
60
60
61
61
For example, consider a resource that doubles a number (this is over engineered, and you wouldn't want a resource for doubling a number)
@@ -188,7 +188,7 @@ But this is not feature-complete! We still need to handle cleanup to prevent mem
188
188
- setInterval(() => (time.current = new Date()), 1_000);
189
189
+ let interval = setInterval(() => (time.current = new Date()), 1_000);
190
190
+
191
-
+ on.cleanup(() => clearInteral(interval))
191
+
+ on.cleanup(() => clearInterval(interval))
192
192
193
193
return time.current;
194
194
```
@@ -197,7 +197,7 @@ Now when the `resource` updates or is torn down, won't leave a bunch of `setInte
197
197
198
198
Lastly, adding in locale-aware formatting with [`Intl.DateTimeFormat`][mdn-DateTimeFormat].
199
199
```diff
200
-
on.cleanup(() => clearInteral(interval))
200
+
on.cleanup(() => clearInterval(interval))
201
201
202
202
- return time.current;
203
203
+ return new Intl.DateTimeFormat('en-US', {
@@ -210,7 +210,7 @@ Lastly, adding in locale-aware formatting with [`Intl.DateTimeFormat`][mdn-DateT
210
210
211
211
212
212
However, there is a goofy behavior with this implementation.
213
-
By accessing `time.current`, we end up consuming tracaked data within the `resource`
213
+
By accessing `time.current`, we end up consuming tracked data within the `resource`
214
214
callback function. When `setInterval` updates `time.current`, the reactivity system
215
215
detects that "tracked data that was consumed in the `resource` callback has changed,
216
216
and must re-evaluate".
@@ -223,7 +223,7 @@ To solve this, we need to enclose access to the tracked data via an arrow functi
223
223
let time = new TrackedObject({ current: new Date() });
224
224
let interval = setInterval(() => (time.current = new Date()), 1_000);
225
225
226
-
on.cleanup(() => clearInteral(interval))
226
+
on.cleanup(() => clearInterval(interval))
227
227
228
228
- return new Intl.DateTimeFormat('en-US', { /* ... ✂️ ...*/ });
229
229
+ let formatter = new Intl.DateTimeFormat('en-US', { /* ... ✂️ ...*/ });
@@ -243,7 +243,7 @@ function Clock(locale = 'en-US') {
243
243
let time =newTrackedObject({ current:newDate() });
244
244
let interval =setInterval(() => (time.current=newDate()), 1_000);
245
245
246
-
on.cleanup(() =>clearInteral(interval))
246
+
on.cleanup(() =>clearInterval(interval))
247
247
248
248
let formatter =newIntl.DateTimeFormat(locale, { /* ... ✂️ ...*/ });
249
249
@@ -265,7 +265,7 @@ function Clock(locale = 'en-US') {
265
265
let time =newTrackedObject({ current:newDate() });
266
266
let interval =setInterval(() => (time.current=newDate()), 1_000);
267
267
268
-
on.cleanup(() =>clearInteral(interval))
268
+
on.cleanup(() =>clearInterval(interval))
269
269
270
270
let formatter =newIntl.DateTimeFormat(locale, { /* ... ✂️ ...*/ });
271
271
@@ -283,7 +283,7 @@ resourceFactory(Clock);
283
283
</details>
284
284
285
285
Up until now, all we've needed in the template for these clocks to work is to have `{{clock}}` in our template.
286
-
But becasue we now need to pass data to a function, we need to invoke that function. The `resourceFactory` utility handles some framework-wiring so that the `Clock` function can immediately invoke the `resource` function.
286
+
But because we now need to pass data to a function, we need to invoke that function. The `resourceFactory` utility handles some framework-wiring so that the `Clock` function can immediately invoke the `resource` function.
287
287
288
288
```hbs
289
289
{{ (Clock 'en-GB') }}
@@ -321,7 +321,7 @@ if (typeof locale === 'function') {
321
321
let time =newTrackedObject({ current:newDate() });
322
322
let interval =setInterval(() => (time.current=newDate()), 1_000);
323
323
324
-
on.cleanup(() =>clearInteral(interval))
324
+
on.cleanup(() =>clearInterval(interval))
325
325
326
326
let formatter =newIntl.DateTimeFormat(currentLocale, { /* ... ✂️ ...*/ });
327
327
@@ -355,4 +355,3 @@ class {
355
355
[🔝 back to top](#authoring-resources)
356
356
357
357
See: Cookbook entry, [`fetch` with `AbortController`](https://github.com/NullVoxPopuli/ember-resources/blob/main/docs/docs/cookbook/fetch-with-AbortController.md#using-resource-1)
0 commit comments