Skip to content

Commit

Permalink
Update RemoteData
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Mar 11, 2023
1 parent 753a24c commit 54e2b50
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .changeset/great-fishes-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"ember-resources": major
---

The `RemoteData` resource now has the same state changes and semantics as `trackedFunction`.

Breaking Changes:

- `isResolved` is only true when the request succeeds. During migration, you may use `isFinished` for previous behavior.
28 changes: 24 additions & 4 deletions ember-resources/src/util/remote-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,29 @@ export class State<T = unknown> {
*/
@tracked status: null | number = null;

/**
* True if the request has succeeded
*/
@tracked isResolved = false;

/**
* True if the request has failed
*/
@tracked isRejected = false;

/**
* true if the request has finished
*/
get isResolved() {
return Boolean(this.value) || Boolean(this.error);
get isFinished() {
return this.isResolved || this.isRejected;
}

/**
* Alias for `isFinished`
* which is in turn an alias for `isResolved || isRejected`
*/
get isSettled() {
return this.isFinished;
}

/**
Expand All @@ -44,14 +62,14 @@ export class State<T = unknown> {
* true if the fetch request is in progress
*/
get isLoading() {
return !this.isResolved;
return !this.isFinished;
}

/**
* true if the request throws an exception
*/
get isError() {
return Boolean(this.error);
return this.isRejected;
}
}

Expand Down Expand Up @@ -110,9 +128,11 @@ export function remoteData<T = unknown>(
return response.json();
})
.then((data) => {
state.isResolved = true;
state.value = data;
})
.catch((error) => {
state.isRejected = true;
state.error = error;
})
);
Expand Down

0 comments on commit 54e2b50

Please sign in to comment.