github.com/emate/nomad@v0.8.2-wo-binpacking/ui/app/utils/properties/watch.js (about)

     1  import Ember from 'ember';
     2  import { get } from '@ember/object';
     3  import RSVP from 'rsvp';
     4  import { task } from 'ember-concurrency';
     5  import wait from 'nomad-ui/utils/wait';
     6  
     7  export function watchRecord(modelName) {
     8    return task(function*(id, throttle = 2000) {
     9      if (typeof id === 'object') {
    10        id = get(id, 'id');
    11      }
    12      while (!Ember.testing) {
    13        try {
    14          yield RSVP.all([
    15            this.get('store').findRecord(modelName, id, {
    16              reload: true,
    17              adapterOptions: { watch: true },
    18            }),
    19            wait(throttle),
    20          ]);
    21        } catch (e) {
    22          yield e;
    23          break;
    24        } finally {
    25          this.get('store')
    26            .adapterFor(modelName)
    27            .cancelFindRecord(modelName, id);
    28        }
    29      }
    30    }).drop();
    31  }
    32  
    33  export function watchRelationship(relationshipName) {
    34    return task(function*(model, throttle = 2000) {
    35      while (!Ember.testing) {
    36        try {
    37          yield RSVP.all([
    38            this.get('store')
    39              .adapterFor(model.constructor.modelName)
    40              .reloadRelationship(model, relationshipName, true),
    41            wait(throttle),
    42          ]);
    43        } catch (e) {
    44          yield e;
    45          break;
    46        } finally {
    47          this.get('store')
    48            .adapterFor(model.constructor.modelName)
    49            .cancelReloadRelationship(model, relationshipName);
    50        }
    51      }
    52    }).drop();
    53  }
    54  
    55  export function watchAll(modelName) {
    56    return task(function*(throttle = 2000) {
    57      while (!Ember.testing) {
    58        try {
    59          yield RSVP.all([
    60            this.get('store').findAll(modelName, { reload: true, adapterOptions: { watch: true } }),
    61            wait(throttle),
    62          ]);
    63        } catch (e) {
    64          yield e;
    65          break;
    66        } finally {
    67          this.get('store')
    68            .adapterFor(modelName)
    69            .cancelFindAll(modelName);
    70        }
    71      }
    72    }).drop();
    73  }