-
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.
Improvements to trackedFunction and State
Co-Authored-By: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com>
- Loading branch information
1 parent
3d1749f
commit a471d9b
Showing
13 changed files
with
382 additions
and
189 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
"ember-resources": major | ||
--- | ||
|
||
`trackedFunction` has a new API and thus a major version release is required. | ||
|
||
tl;dr: the breaking changes: | ||
|
||
- no more manual initial value | ||
- `isResolved` is only true on success | ||
- `isError` has been renamed to `isRejected` | ||
- `isLoading` has been removed as it was redundant | ||
|
||
other changes: | ||
|
||
- `trackedFunction` is a wrapper around `ember-async-data`'s [`TrackedAsyncData`](https://github.com/tracked-tools/ember-async-data/blob/main/ember-async-data/src/tracked-async-data.ts) | ||
- behavior is otherwise the same | ||
|
||
NOTE: `trackedFunction` is an example utility of how to use auto-tracking with function invocation, | ||
and abstract away the various states involved with async behavior. Now that the heavy lifting is done by `ember-async-data`, | ||
`trackedFunction` is now more of an example of how to integrated existing tracked utilities in to resources. | ||
|
||
--- | ||
|
||
**Migration** | ||
|
||
**_Previously_, `trackedFunction` could take an initial value for its second argument.** | ||
|
||
```js | ||
class Demo { | ||
foo = trackedFunction(this, "initial value", async () => { | ||
/* ... */ | ||
}); | ||
} | ||
``` | ||
|
||
This has been removed, as initial value can be better maintained _and made more explicit_ | ||
in user-space. For example: | ||
|
||
```js | ||
class Demo { | ||
foo = trackedFunction(this, async () => { | ||
/* ... */ | ||
}); | ||
|
||
get value() { | ||
return this.foo.value ?? "initial value"; | ||
} | ||
} | ||
``` | ||
Or, in a template: | ||
```hbs | ||
{{#if this.foo.value}} | ||
{{this.foo.value}} | ||
{{else}} | ||
initial displayed content | ||
{{/if}} | ||
``` | ||
Or, in gjs/strict mode: | ||
```gjs | ||
const withDefault = (value) => value ?? 'initial value'; | ||
|
||
class Demo extends Component { | ||
foo = trackedFunction(this, async () => { /* ... */ }); | ||
|
||
<template> | ||
{{withDefault this.foo.value}} | ||
</template> | ||
} | ||
``` | ||
**_Previously_, the `isResolved` property was `true` for succesful and error states** | ||
Now, `isResolved` is only true when the function passed to `trackedFunction` has succesfully | ||
completed. | ||
To have behavior similar to the old behavior, you may want to implement your own `isFinished` getter: | ||
```js | ||
class Demo { | ||
foo = trackedFunction(this, async () => { | ||
/* ... */ | ||
}); | ||
|
||
get isFinished() { | ||
return this.foo.isResolved || this.foo.isRejected; | ||
} | ||
} | ||
``` |
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 |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
/dist/ | ||
/tmp/ | ||
CHANGELOG.md | ||
README.md | ||
LICENSE.md | ||
|
||
# dependencies | ||
/bower_components/ | ||
|
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
Oops, something went wrong.