-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a small util that provides a single tracked property for easily creating tracked state within the function-based resources.
- Loading branch information
1 parent
2cd0974
commit ebc4c30
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { tracked } from '@glimmer/tracking'; | ||
import { assert } from '@ember/debug'; | ||
|
||
class Cell<T> { | ||
@tracked current: T; | ||
|
||
constructor(initialValue: T) { | ||
this.current = initialValue; | ||
} | ||
|
||
toggle = () => { | ||
assert( | ||
`toggle can only be used when 'current' is a boolean type`, | ||
typeof this.current === 'boolean' || this.current === undefined | ||
); | ||
|
||
(this.current as boolean) = !this.current; | ||
}; | ||
} | ||
|
||
/** | ||
* Small state utility for helping reduce the number of imports | ||
* when working with resources in isolation. | ||
* | ||
* The return value is an instance of a class with a single | ||
* `@tracked` property, `current`. If `current` is a boolean, | ||
* there is a `toggle` method available as well. | ||
* | ||
* For example, a Clock: | ||
* | ||
* ```js | ||
* import { resource, cell } from 'ember-resources'; | ||
* | ||
* const Clock = resource(({ on }) => { | ||
* let time = cell(new Date()); | ||
* let interval = setInterval(() => time.current = new Date(), 1000); | ||
* | ||
* on.cleanup(() => clearInterval(interval)); | ||
* | ||
* let formatter = new Intl.DateTimeFormat('en-US', { | ||
* hour: 'numeric', | ||
* minute: 'numeric', | ||
* second: 'numeric', | ||
* hour12: true, | ||
* }); | ||
* | ||
* return () => formatter.format(time.current); | ||
* }); | ||
* | ||
* <template> | ||
* It is: <time>{{Clock}}</time> | ||
* </template> | ||
* ``` | ||
*/ | ||
export function cell<T>(initialValue: T): Cell<T> { | ||
return new Cell(initialValue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { render, settled } from '@ember/test-helpers'; | ||
import { hbs } from 'ember-cli-htmlbars'; | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
|
||
import { cell } from 'ember-resources'; | ||
|
||
module('Utils | cell | rendering', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('it works', async function (assert) { | ||
let state = cell(); | ||
|
||
this.setProperties({ state }); | ||
|
||
await render(hbs`{{this.state.current}}`); | ||
|
||
assert.dom().doesNotContainText('hello'); | ||
|
||
state.current = 'hello'; | ||
|
||
await settled(); | ||
|
||
assert.dom().hasText('hello'); | ||
}); | ||
}); |