github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/components/trigger.js (about) 1 import { action } from '@ember/object'; 2 import Component from '@glimmer/component'; 3 import { tracked } from '@glimmer/tracking'; 4 import { task } from 'ember-concurrency'; 5 import { schedule } from '@ember/runloop'; 6 7 const noOp = () => undefined; 8 9 export default class Trigger extends Component { 10 @tracked error = null; 11 @tracked result = null; 12 13 get isBusy() { 14 return this.triggerTask.isRunning; 15 } 16 17 get isIdle() { 18 return this.triggerTask.isIdle; 19 } 20 21 get isSuccess() { 22 return this.triggerTask.last?.isSuccessful; 23 } 24 25 get isError() { 26 return !!this.error; 27 } 28 29 get fns() { 30 return { 31 do: this.onTrigger, 32 }; 33 } 34 35 get onError() { 36 return this.args.onError ?? noOp; 37 } 38 39 get onSuccess() { 40 return this.args.onSuccess ?? noOp; 41 } 42 43 get data() { 44 const { isBusy, isIdle, isSuccess, isError, result } = this; 45 return { isBusy, isIdle, isSuccess, isError, result }; 46 } 47 48 _reset() { 49 this.result = null; 50 this.error = null; 51 } 52 53 @task(function* () { 54 this._reset(); 55 try { 56 this.result = yield this.args.do(); 57 this.onSuccess(this.result); 58 } catch (e) { 59 this.error = { Error: e }; 60 this.onError(this.error); 61 } 62 }) 63 triggerTask; 64 65 @action 66 onTrigger() { 67 schedule('actions', () => { 68 this.triggerTask.perform(); 69 }); 70 } 71 }