|
1 | | -import Component from '@ember/component'; |
2 | | -import layout from '../templates/components/w-async'; |
3 | | -import { and } from '@ember/object/computed'; |
| 1 | +import Component from "@ember/component"; |
4 | 2 | import { computed } from '@ember/object'; |
5 | | -import { next } from '@ember/runloop'; |
| 3 | +import layout from '../templates/components/w-async'; |
| 4 | +import { later } from '@ember/runloop'; |
6 | 5 |
|
7 | | -export default class WAsync extends Component { |
| 6 | +export default class WAsyncComponent extends Component { |
8 | 7 | layout = layout |
9 | | - showLoading = 'w-async/loading' |
10 | | - fireTrigger = false |
11 | | - |
12 | | - @and('task.isRunning', 'showLoading') isLoading |
13 | | - |
14 | | - @computed('task.last') |
15 | | - get result() { |
16 | | - return this.task.last.isError ? |
17 | | - this.task.lastErrored.error : |
18 | | - this.task.lastSuccessful.value |
19 | | - } |
20 | | - |
| 8 | + |
21 | 9 | didReceiveAttrs() { |
22 | | - if (this.autoFire || this.fireTrigger) { |
| 10 | + this._super(...arguments); |
| 11 | + if (this.promise) { |
| 12 | + this.set('task', { |
| 13 | + isRunning: true, |
| 14 | + last: {value: null} |
| 15 | + }) |
| 16 | + |
| 17 | + this.promise.then(resolved => { |
| 18 | + this.set('task', { |
| 19 | + isRunning: false, |
| 20 | + last: { |
| 21 | + value: resolved |
| 22 | + } |
| 23 | + }) |
| 24 | + }) |
| 25 | + } |
| 26 | + else if (this.triggered) { |
23 | 27 | this.task.perform() |
24 | | - next(() => this.set('fireTrigger', false)) |
| 28 | + later(() => this.set('triggered', false)) |
25 | 29 | } |
| 30 | + else if (this.autoFire) this.task.perform(); |
| 31 | + } |
| 32 | + |
| 33 | + @computed('task.{isRunning,last.value}') |
| 34 | + get showLoader() { |
| 35 | + const { task } = this |
| 36 | + return task.isRunning && !task.last.value |
26 | 37 | } |
27 | 38 | } |
0 commit comments