|
1 | 1 | # ember-resources
|
2 | 2 |
|
| 3 | +## 5.6.2 |
| 4 | + |
| 5 | +### Patch Changes |
| 6 | + |
| 7 | +- [#742](https://github.com/NullVoxPopuli/ember-resources/pull/742) [`dd7234a`](https://github.com/NullVoxPopuli/ember-resources/commit/dd7234a4054b6ffb546c95e234caceb7e703536e) Thanks [@NullVoxPopuli](https://github.com/NullVoxPopuli)! - When using the `resourceFactory` (blueprint in [Starbeam][docs-starbeam] terms), |
| 8 | + there was an issue where a returned `resource` would not get torn down when the |
| 9 | + surrounding context of the `resourceFactory` would get torn down. |
| 10 | + |
| 11 | + For example, in this situation, |
| 12 | + which has been added as the [second example][demo-polling] on [this blog post][gts-examples], |
| 13 | + |
| 14 | + ```js |
| 15 | + const poll = resourceFactory((fn, interval) => { |
| 16 | + return resource(({ on }) => { |
| 17 | + let x = setInterval(fn, interval); |
| 18 | + on.cleanup(() => clearInterval(x)); |
| 19 | + }); |
| 20 | + }); |
| 21 | + ``` |
| 22 | + |
| 23 | + usage would be: |
| 24 | + |
| 25 | + ```hbs |
| 26 | + {{poll someFn someSeconds}} |
| 27 | + ``` |
| 28 | + |
| 29 | + So, when this was in an `if` statement, or in a component or route, or any content that could be torn down, |
| 30 | + the `on.cleanup` callback would not be called. |
| 31 | + |
| 32 | + This fix addresses the issue and the `on.cleanup` callback is now called. |
| 33 | + |
| 34 | + NOTE: this approach to using resources is equivelent to this 0-dependency solution to polling: |
| 35 | + |
| 36 | + ```ts |
| 37 | + import Component from "@glimmer/component"; |
| 38 | + import Helper from "@ember/component/helper"; |
| 39 | + import type RouterService from "@ember/routing/router-service"; |
| 40 | + import { service } from "@ember/service"; |
| 41 | + |
| 42 | + class Poll extends Helper { |
| 43 | + compute([fn, interval]: [(...args: unknown[]) => unknown, number]) { |
| 44 | + let x = setInterval(fn, interval); |
| 45 | + registerDestructor(this, () => clearInterval(x)); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + export default class Demo extends Component { |
| 50 | + @service declare router: RouterService; |
| 51 | + |
| 52 | + poll = Poll; |
| 53 | + refreshData = () => this.router.refresh(); |
| 54 | + } |
| 55 | + ``` |
| 56 | + |
| 57 | + ```hbs |
| 58 | + {{this.poll this.refreshData 4000}} |
| 59 | + ``` |
| 60 | + |
| 61 | + [docs-starbeam]: https://www.starbeamjs.com |
| 62 | + [gts-examples]: https://nullvoxpopuli.com/2022-09-05-gjs-cookbook-examples#polling |
| 63 | + [demo-polling]: https://limber.glimdown.com/edit?format=glimdown&t=%23%20Polling%20data%0A%0AA%20common%20thing%20folks%20ask%20is%20to%20re-call%20%2F%20re-run%20the%20%5Broute%27s%20model%20hook%5D%5Broute-model%5D%20on%20some%20interval.%0A%0AThis%20technique%20can%20be%20used%20to%20poll%20anything%2C%20%0Anot%20just%20the%20%5Brouter%20service%5D%5Brouter-service%5D%27s%20%5B%60refresh%60%5D%5Brouter-refresh%5D%20method.%0AIt%20could%20be%20used%20for%20any%20function%2C%20%5B%60fetch%60%5D%5Bmdn-fetch%5D%2C%20plain%20old%20functions%2C%20etc.%0A%0AWhen%20polling%2C%20the%20most%20important%20thing%20to%20remember%20is%20that%20the%20polling%20function%20needs%20to%20be%20cancelled%20when%20the%20surrounding%20context%20is%20torn%20down%2C%20or%20if%20the%20app%20is%20destroyed.%20This%20is%20so%20that%20as%20you%20navigate%20within%20your%20app%2C%20or%20while%20running%20tests%2C%20a%20memory%20leak%20does%20not%20occur.%0A%0AThis%20approach%20uses%20%5B%60setInterval%60%5D%5Bmdn-setInterval%5D%20so%20as%20to%20not%20induce%20a%20%5B%60too%20much%20recursion%60%5D%5Bmdn-too-much-recursion%5D%20error.%0A%0A%5Brouter-service%5D%3A%20https%3A%2F%2Fapi.emberjs.com%2Fember%2Frelease%2Fclasses%2FRouterService%0A%5Brouter-refresh%5D%3A%20https%3A%2F%2Fapi.emberjs.com%2Fember%2F4.9%2Fclasses%2FRouterService%2Fmethods%2Frefresh%3Fanchor%3Drefresh%0A%5Broute-model%5D%3A%20https%3A%2F%2Fguides.emberjs.com%2Frelease%2Frouting%2Fspecifying-a-routes-model%2F%0A%5Bmdn-fetch%5D%3A%20https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FFetch_API%0A%5Bmdn-setInterval%5D%3A%20https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FAPI%2FsetInterval%0A%5Bmdn-too-much-recursion%5D%3A%20https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FErrors%2FToo_much_recursion%0A%0A%60%60%60gjs%20live%20preview%0Aimport%20%7B%20on%20%7D%20from%20%27%40ember%2Fmodifier%27%3B%0Aimport%20%7B%20cell%2C%20resource%2C%20resourceFactory%20%7D%20from%20%27ember-resources%27%3B%0A%0Aconst%20show%20%3D%20cell(true)%3B%0Aconst%20pollCount%20%3D%20cell(0)%3B%0Aconst%20fetchData%20%3D%20()%20%3D%3E%20%7B%0A%20%20pollCount.current%2B%2B%3B%0A%20%20console.log(%27Fetching%20data%20(for%20pretend%20%2F%20example)%27)%3B%0A%7D%0Aconst%20ONE_SECOND%20%3D%201_000%3B%20%2F%2F%20ms%0A%0A%2F**********************************************************%0A%20*%20DEMO%20starts%20here%2C%20everything%20above%20is%20mostly%20irrelevant%0A%20**********************************************************%2F%0Aconst%20poll%20%3D%20resourceFactory((fn%2C%20interval)%20%3D%3E%20%7B%0A%20%20return%20resource((%7B%20on%20%7D)%20%3D%3E%20%7B%0A%20%20%20%20let%20x%20%3D%20setInterval(fn%2C%20interval)%3B%20%20%20%20%0A%20%20%20%20on.cleanup(()%20%3D%3E%20clearInterval(x))%3B%0A%20%20%7D)%3B%0A%7D)%3B%0A%0A%3Ctemplate%3E%0A%20%20Poll%20count%3A%20%7B%7BpollCount.current%7D%7D%3Cbr%3E%0A%20%20%3Cbutton%20%7B%7Bon%20%27click%27%20show.toggle%7D%7D%3EToggle%3C%2Fbutton%3E%3Cbr%20%2F%3E%0A%20%20%0A%20%20%7B%7B%23if%20show.current%7D%7D%0A%20%20%20%20%20Data%20is%20being%20polled.%0A%0A%20%20%20%20%20%7B%7Bpoll%20fetchData%20ONE_SECOND%7D%7D%0A%20%20%7B%7Belse%7D%7D%0A%20%20%20%20%20Polling%20is%20not%20occurring.%0A%20%20%7B%7B%2Fif%7D%7D%0A%0A%20%20%20%20%0A%3C%2Ftemplate%3E%0A%60%60%60 |
| 64 | + |
3 | 65 | ## 5.6.1
|
4 | 66 |
|
5 | 67 | ### Patch Changes
|
|
0 commit comments