|
1 | 1 | # ember-resources
|
2 | 2 |
|
| 3 | +## 6.2.0 |
| 4 | + |
| 5 | +### Minor Changes |
| 6 | + |
| 7 | +- [#936](https://github.com/NullVoxPopuli/ember-resources/pull/936) [`6246a3c`](https://github.com/NullVoxPopuli/ember-resources/commit/6246a3cec6d6a32ac412c9655344e2b49b8b9284) Thanks [@NullVoxPopuli](https://github.com/NullVoxPopuli)! - The `use` import from `ember-resources` now supports an alternate style of usage. |
| 8 | + This is partly to provide consistency across the different kinds of resources (and resource builders), whether or not arguments are provided. |
| 9 | + |
| 10 | + The motivation from this change comes from trying to better align with Starbeam's composition capabilities, and "define something once, use it anywhere" approach to that composition. |
| 11 | + |
| 12 | + For example, before, only this was possible: |
| 13 | + |
| 14 | + ```js |
| 15 | + import { resource, use } from "ember-resources"; |
| 16 | + |
| 17 | + const StuckClock = resource(() => 2); |
| 18 | + |
| 19 | + class MyClass { |
| 20 | + @use data = StuckClock; |
| 21 | + } |
| 22 | + |
| 23 | + new MyClass().data === 2; |
| 24 | + ``` |
| 25 | + |
| 26 | + That looks a little awkward, because it looks like `data` is set to a constant. |
| 27 | + In `TypeScript`, this still worked out, and the type of `data` would be a `number`, |
| 28 | + but it still didn't look intuitive. |
| 29 | + |
| 30 | + _Now, we can do this_: |
| 31 | + |
| 32 | + ```js |
| 33 | + import { resource, use } from "ember-resources"; |
| 34 | + |
| 35 | + const StuckClock = resource(() => 2); |
| 36 | + |
| 37 | + class MyClass { |
| 38 | + data = use(this, StuckClock); |
| 39 | + } |
| 40 | + |
| 41 | + new MyClass().data.current === 2; |
| 42 | + ``` |
| 43 | + |
| 44 | + The key difference here is that `data` is now a `Reactive<number>`, which, like a `cell`, has a `.current` property. |
| 45 | + This is a _readonly_ value -- however `current` can still return a mutable data structure. |
| 46 | + |
| 47 | + This style of `use` ends up extending nicely to Resources that take arguments: |
| 48 | + |
| 49 | + ```js |
| 50 | + import { tracked } from "@glimmer/tracking"; |
| 51 | + import { resource, use, resourceFactory } from "ember-resources"; |
| 52 | + |
| 53 | + const Clock = resourceFactory((locale) => resource(/* ... */)); |
| 54 | + |
| 55 | + class MyClass { |
| 56 | + @tracked locale = "en-US"; |
| 57 | + |
| 58 | + data = use( |
| 59 | + this, |
| 60 | + Clock(() => this.locale) |
| 61 | + ); |
| 62 | + } |
| 63 | + ``` |
| 64 | + |
| 65 | + > **Note** <br> |
| 66 | + > The old way of using `@use` as a decorator is still supported, and has no plans of being deprecated. |
| 67 | + |
| 68 | + <details><summary>Another approach</summary> |
| 69 | + |
| 70 | + I can't recommend this approach for general usage, but it is supported under SemVer (for exploration and feedback). |
| 71 | + |
| 72 | + ```ts |
| 73 | + import { resource, use } from "ember-resources"; |
| 74 | + |
| 75 | + const StuckClock = resource(() => 2); |
| 76 | + |
| 77 | + class MyClass { |
| 78 | + @use(StuckClock) declare data: number; |
| 79 | + } |
| 80 | + |
| 81 | + new MyClass().data === 2; |
| 82 | + ``` |
| 83 | + |
| 84 | + This should feel familiar as it looks like what we're familiar with when it comes to declaring `@tracked` properties as well as `@service`s. |
| 85 | + |
| 86 | + However, this has the same problems as `@service` -- in TypeScript, it requires you to use `declare` and specify a type, which may or may not match the actual type of `StuckClock`. |
| 87 | + |
| 88 | + Additionally, whenever we want to pass arguments to the resource, like this: |
| 89 | + |
| 90 | + ```ts |
| 91 | + import { tracked } from '@glimmer/tracking'; |
| 92 | + import { resource, use } from 'ember-resources'; |
| 93 | + |
| 94 | + const Clock = resourceFactory((locale) => resource( /* ... */); |
| 95 | + |
| 96 | + class MyClass { |
| 97 | + @tracked locale = 'en-US'; |
| 98 | + |
| 99 | + @use(Clock(() => this.locale) declare data: number; |
| 100 | + } |
| 101 | + ``` |
| 102 | + |
| 103 | + The arrow function passed to `Clock` would not have the correct this. |
| 104 | + This is confusing, because in every other situation where we use classes, |
| 105 | + the arrow function has the same context as the instance of the class. |
| 106 | + But due to how decorators are configured / transpiled, the `this` is actually the surrounding context around `MyClass`, because decorators are _statically applied_. |
| 107 | + |
| 108 | + ```ts |
| 109 | + class MyClass { |
| 110 | + @tracked locale = 'en-US'; |
| 111 | + |
| 112 | + @use(Clock( static context here, not instance ) declare data: number; |
| 113 | + } |
| 114 | + ``` |
| 115 | + |
| 116 | + So... that's why I want to recommend `property = use(this, Foo)` by default. |
| 117 | + |
| 118 | + ```ts |
| 119 | + class MyClass { |
| 120 | + @tracked locale = 'en-US'; |
| 121 | + |
| 122 | + data = use(this, (Clock( instance access )); |
| 123 | + } |
| 124 | + ``` |
| 125 | + |
| 126 | + </details> |
| 127 | + |
3 | 128 | ## 6.1.1
|
4 | 129 |
|
5 | 130 | ### Patch Changes
|
|
0 commit comments