|
1 | 1 | # ember-resources
|
2 | 2 |
|
| 3 | +## 6.4.0 |
| 4 | + |
| 5 | +### Minor Changes |
| 6 | + |
| 7 | +- [#975](https://github.com/NullVoxPopuli/ember-resources/pull/975) [`1a964f1`](https://github.com/NullVoxPopuli/ember-resources/commit/1a964f16ca6e528c337ad2788d4c870aa699ef32) Thanks [@NullVoxPopuli](https://github.com/NullVoxPopuli)! - `trackedFunction` can now be composed just like regular resources. |
| 8 | + |
| 9 | + The function body still auto-tracks and will update within consuming resource appropriately. |
| 10 | + |
| 11 | + <details><summary>Example</summary> |
| 12 | + |
| 13 | + ```ts |
| 14 | + const Person = resourceFactory((maybeIdFn) => { |
| 15 | + return resource(({ use }) => { |
| 16 | + let request = use( |
| 17 | + trackedFunction(async () => { |
| 18 | + let id = typeof maybeIdFn === "function" ? maybeIdFn() : maybeIdFn; |
| 19 | + let response = await fetch(`https://api.github.com/users/${id}`); |
| 20 | + return response.json(); |
| 21 | + }) |
| 22 | + ); |
| 23 | + |
| 24 | + // `use` returns a ReadonlyCell where `.current` |
| 25 | + // is the State of trackedFunction. |
| 26 | + return () => request.current; |
| 27 | + }); |
| 28 | + }); |
| 29 | + ``` |
| 30 | + |
| 31 | + Usage examples: |
| 32 | + |
| 33 | + ```gjs |
| 34 | + <template> |
| 35 | + {{#let (Person 1) as |request|}} |
| 36 | + {{#if request.isLoading}} |
| 37 | + ... loading ... |
| 38 | + {{/if}} |
| 39 | +
|
| 40 | + {{#if request.value}} |
| 41 | +
|
| 42 | + {{/if}} |
| 43 | + {{/let}} |
| 44 | + </template> |
| 45 | + ``` |
| 46 | + |
| 47 | + </details> |
| 48 | + |
| 49 | + <details><summary>An async doubler</summary> |
| 50 | + |
| 51 | + ```ts |
| 52 | + const Doubled = resourceFactory((num: number) => { |
| 53 | + return resource(({ use }) => { |
| 54 | + let doubler = use(trackedFunction(async () => num * 2)); |
| 55 | + |
| 56 | + // Since current is the "State" of `trackedFunction`, |
| 57 | + // accessing .value on it means that the overall value of |
| 58 | + // `Doubled` is the eventual return value of the `trackedFunction` |
| 59 | + return () => doubler.current.value; |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + // Actual code from a test |
| 64 | + class State { |
| 65 | + @tracked num = 2; |
| 66 | + } |
| 67 | + |
| 68 | + let state = new State(); |
| 69 | + |
| 70 | + setOwner(state, this.owner); |
| 71 | + |
| 72 | + await render(<template><out>{{Doubled state.num}}</out></template>); |
| 73 | + ``` |
| 74 | + |
| 75 | + </details> |
| 76 | + |
| 77 | + <details><summary>Example with arguments</summary> |
| 78 | + |
| 79 | + Imagine you want to compute the hypotenuse of a triangle, |
| 80 | + but all calculations are asynchronous (maybe the measurements exist on external APIs or something). |
| 81 | + |
| 82 | + ```ts |
| 83 | + // Actual code from a test |
| 84 | + type NumberThunk = () => number; |
| 85 | + |
| 86 | + const Sqrt = resourceFactory((numFn: NumberThunk) => |
| 87 | + trackedFunction(async () => { |
| 88 | + let num = numFn(); |
| 89 | + |
| 90 | + return Math.sqrt(num); |
| 91 | + }) |
| 92 | + ); |
| 93 | + |
| 94 | + const Squared = resourceFactory((numFn: NumberThunk) => |
| 95 | + trackedFunction(async () => { |
| 96 | + let num = numFn(); |
| 97 | + |
| 98 | + return Math.pow(num, 2); |
| 99 | + }) |
| 100 | + ); |
| 101 | + |
| 102 | + const Hypotenuse = resourceFactory((aFn: NumberThunk, bFn: NumberThunk) => { |
| 103 | + return resource(({ use }) => { |
| 104 | + const aSquared = use(Squared(aFn)); |
| 105 | + const bSquared = use(Squared(bFn)); |
| 106 | + const c = use( |
| 107 | + Sqrt(() => { |
| 108 | + return (aSquared.current.value ?? 0) + (bSquared.current.value ?? 0); |
| 109 | + }) |
| 110 | + ); |
| 111 | + |
| 112 | + // We use the function return because we want this property chain |
| 113 | + // to be what's lazily evaluated -- in this example, since |
| 114 | + // we want to return the hypotenuse, we don't (atm) |
| 115 | + // care about loading / error state, etc. |
| 116 | + // In real apps, you might care about loading state though! |
| 117 | + return () => c.current.value; |
| 118 | + |
| 119 | + // In situations where you care about forwarding other states, |
| 120 | + // you could do this |
| 121 | + return { |
| 122 | + get value() { |
| 123 | + return c.current.value; |
| 124 | + }, |
| 125 | + get isLoading() { |
| 126 | + return ( |
| 127 | + a.current.isLoading || b.current.isLoading || c.current.isLoading |
| 128 | + ); |
| 129 | + }, |
| 130 | + }; |
| 131 | + }); |
| 132 | + }); |
| 133 | + ``` |
| 134 | + |
| 135 | + </details> |
| 136 | + |
3 | 137 | ## 6.3.1
|
4 | 138 |
|
5 | 139 | ### Patch Changes
|
|
0 commit comments