github.com/hernad/nomad@v1.6.112/ui/app/components/trigger.js (about)

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