Skip to content

Commit

Permalink
Add support for initial and persist states on blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Exelord committed May 10, 2020
1 parent b524f33 commit 78e6c0b
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 37 deletions.
11 changes: 11 additions & 0 deletions addon/components/await/complete/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Component from '@glimmer/component';

class AwaitStateComponent extends Component {
get shouldRender() {
const { shouldRender, persist, value } = this.args;

return shouldRender || persist && value;
}
}

export default AwaitStateComponent;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#if @shouldRender}}
{{#if this.shouldRender}}
{{yield @value}}
{{/if}}
54 changes: 37 additions & 17 deletions addon/components/await/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,52 +23,72 @@ class AwaitComponent extends Component {

@tracked finishedAt;

@computed('promiseTask.last')
get lastPromiseTask() {
return this.promiseTask.last;
}

@computed('promiseTask.performCount')
get counter() {
return this.promiseTask.performCount;
}

@computed('lastPromiseTask.value', 'isFulfilled', 'args.initialValue')
@computed('promiseTask.lastComplete.value', 'isFulfilled', 'args.initialValue')
get data() {
if (this.lastPromiseTask) return this.lastPromiseTask.value;
if (this.promiseTask.lastComplete) return this.promiseTask.lastComplete.value;

return this.isFulfilled ? this.args.initialValue : undefined;
}

@computed('lastPromiseTask.error', 'isRejected', 'args.initialValue')
@computed('promiseTask.lastSuccessful.value', 'isRejected', 'data')
get persistedData() {
if (this.isRejected && this.promiseTask.lastSuccessful) return this.promiseTask.lastSuccessful.value;

return this.data;
}

@computed('promiseTask.lastComplete.error', 'isRejected', 'args.initialValue')
get error() {
if (this.lastPromiseTask) return this.lastPromiseTask.error;
if (this.promiseTask.lastComplete) return this.promiseTask.lastComplete.error;

return this.isRejected ? this.args.initialValue : undefined;
}

@computed('isFulfilled', 'error', 'data')
@computed('promiseTask.lastErrored.value', 'isPending', 'error')
get persistedError() {
if (this.isPending && this.promiseTask.lastErrored) return this.promiseTask.lastErrored.error;

return this.error;
}

@computed('isFulfilled', 'isPending', 'error', 'data')
get value() {
if (this.isPending) return undefined;

const { isFulfilled, error, data } = this;

return isFulfilled ? data : error;
}

@computed('lastPromiseTask.isSuccessful')
@computed('promiseTask.lastComplete.isError', 'isPending', 'data')
get persistedValue() {
if (this.promiseTask.lastComplete) {
return this.promiseTask.lastComplete.isError ? this.error : this.data;
}

return this.value;
}

@computed('promiseTask.last.isSuccessful')
get isFulfilled() {
if (this.lastPromiseTask) {
return this.lastPromiseTask.isSuccessful;
if (this.promiseTask.last) {
return this.promiseTask.last.isSuccessful;
}

const { initialValue } = this.args;

return isDefined(initialValue) && !(initialValue instanceof Error);
}

@computed('lastPromiseTask.isError')
@computed('promiseTask.last.isError')
get isRejected() {
if (this.lastPromiseTask) {
return this.lastPromiseTask.isError;
if (this.promiseTask.last) {
return this.promiseTask.last.isError;
}

const { initialValue } = this.args;
Expand Down Expand Up @@ -123,7 +143,7 @@ class AwaitComponent extends Component {
* @protected
*/
trigger(name, currentTask) {
if (this.lastPromiseTask && currentTask !== this.lastPromiseTask) return;
if (this.promiseTask.last && currentTask !== this.promiseTask.last) return;

if (name === 'promiseTask:started') {
this.startedAt = new Date();
Expand Down
11 changes: 11 additions & 0 deletions addon/components/await/initial/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Component from '@glimmer/component';

class AwaitStateComponent extends Component {
get shouldRender() {
const { shouldRender, persist, value } = this.args;

return shouldRender || persist && !value;
}
}

export default AwaitStateComponent;
3 changes: 3 additions & 0 deletions addon/components/await/initial/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#if this.shouldRender}}
{{yield @value}}
{{/if}}
11 changes: 11 additions & 0 deletions addon/components/await/pending/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Component from '@glimmer/component';

class AwaitPendingComponent extends Component {
get shouldRender() {
const { shouldRender, initial, value } = this.args;

return shouldRender && (!initial || !value);
}
}

export default AwaitPendingComponent;
3 changes: 3 additions & 0 deletions addon/components/await/pending/template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{#if this.shouldRender}}
{{yield}}
{{/if}}
22 changes: 11 additions & 11 deletions addon/components/await/template.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{yield (hash
data=this.data
error=this.error
value=this.value
data=this.persistedData
error=this.persistedError
value=this.persistedValue
initialValue=@initialValue

startedAt=this.startedAt
Expand All @@ -17,16 +17,16 @@
isInitial=this.isInitial

counter=this.counter
task=this.lastPromiseTask
task=this.promiseTask.last
reload=this.reload
cancel=this.cancel
run=this.run

Pending=(component "await/state" shouldRender=this.isPending)
Loading=(component "await/state" shouldRender=this.isPending)
Fulfilled=(component "await/state" shouldRender=this.isFulfilled value=this.data)
Resolved=(component "await/state" shouldRender=this.isFulfilled value=this.data)
Rejected=(component "await/state" shouldRender=this.isRejected value=this.error)
Settled=(component "await/state" shouldRender=this.isSettled)
Initial=(component "await/state" shouldRender=this.isInitial)
Pending=(component "await/pending" shouldRender=this.isPending value=this.persistedValue)
Loading=(component "await/pending" shouldRender=this.isPending value=this.persistedValue)
Fulfilled=(component "await/complete" shouldRender=this.isFulfilled value=this.persistedData)
Resolved=(component "await/complete" shouldRender=this.isFulfilled value=this.persistedData)
Rejected=(component "await/complete" shouldRender=this.isRejected value=this.persistedError)
Settled=(component "await/complete" shouldRender=this.isSettled value=this.persistedValue)
Initial=(component "await/initial" shouldRender=this.isInitial value=this.persistedData)
)}}
1 change: 1 addition & 0 deletions app/components/await/complete/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-await/components/await/complete/component';
1 change: 1 addition & 0 deletions app/components/await/complete/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-await/components/await/complete/template';
1 change: 1 addition & 0 deletions app/components/await/initial/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-await/components/await/initial/component';
1 change: 1 addition & 0 deletions app/components/await/initial/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-await/components/await/initial/template';
1 change: 1 addition & 0 deletions app/components/await/pending/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-await/components/await/pending/component';
1 change: 1 addition & 0 deletions app/components/await/pending/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-await/components/await/pending/template';
1 change: 0 additions & 1 deletion app/components/await/state/template.js

This file was deleted.

10 changes: 5 additions & 5 deletions docs/api/yielded-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Renders only while the deferred promise is still waiting to be run, or you have

### Arguments

- ~~`persist` `boolean` Show until we have data, even while loading or when an error occurred. By default it hides as soon as the promise starts loading.~~
- `persist` `boolean` Show until we have data, even while loading or when an error occurred. By default it hides as soon as the promise starts loading.

### Examples

Expand All @@ -28,7 +28,7 @@ Alias: `<Await.Loading>`

### Arguments

- ~~`initial` `boolean` Show only on initial load \(when `data` is `undefined`\).~~
- `initial` `boolean` Show only on initial load \(when `data` is `undefined`\).

### Examples

Expand All @@ -52,7 +52,7 @@ Alias: `<Await.Resolved>`

### Arguments

- ~~`persist` `boolean` Show old data while loading new data. By default it hides as soon as a new promise starts.~~
- `persist` `boolean` Show old data while loading new data. By default it hides as soon as a new promise starts.

### Examples

Expand All @@ -74,7 +74,7 @@ This component renders only when the promise is rejected.

### Arguments

- ~~`persist` `boolean` Show old error while loading new data. By default it hides as soon as a new promise starts.~~
- `persist` `boolean` Show old error while loading new data. By default it hides as soon as a new promise starts.

### Examples

Expand All @@ -92,7 +92,7 @@ This component renders only when the promise is settled (resolved or rejected).

### Arguments

- ~~`persist` `boolean` Show old error while loading new data. By default it hides as soon as a new promise starts.~~
- `persist` `boolean` Show old error or data while loading new data. By default it hides as soon as a new promise starts.

### Examples

Expand Down
Loading

0 comments on commit 78e6c0b

Please sign in to comment.