github.com/zhizhiboom/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/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  import config from 'nomad-ui/config/environment';
     7  
     8  const isEnabled = config.APP.blockingQueries !== false;
     9  
    10  export function watchRecord(modelName) {
    11    return task(function*(id, throttle = 2000) {
    12      if (typeof id === 'object') {
    13        id = get(id, 'id');
    14      }
    15      while (isEnabled && !Ember.testing) {
    16        try {
    17          yield RSVP.all([
    18            this.get('store').findRecord(modelName, id, {
    19              reload: true,
    20              adapterOptions: { watch: true },
    21            }),
    22            wait(throttle),
    23          ]);
    24        } catch (e) {
    25          yield e;
    26          break;
    27        } finally {
    28          this.get('store')
    29            .adapterFor(modelName)
    30            .cancelFindRecord(modelName, id);
    31        }
    32      }
    33    }).drop();
    34  }
    35  
    36  export function watchRelationship(relationshipName) {
    37    return task(function*(model, throttle = 2000) {
    38      while (isEnabled && !Ember.testing) {
    39        try {
    40          yield RSVP.all([
    41            this.get('store')
    42              .adapterFor(model.constructor.modelName)
    43              .reloadRelationship(model, relationshipName, true),
    44            wait(throttle),
    45          ]);
    46        } catch (e) {
    47          yield e;
    48          break;
    49        } finally {
    50          this.get('store')
    51            .adapterFor(model.constructor.modelName)
    52            .cancelReloadRelationship(model, relationshipName);
    53        }
    54      }
    55    }).drop();
    56  }
    57  
    58  export function watchAll(modelName) {
    59    return task(function*(throttle = 2000) {
    60      while (isEnabled && !Ember.testing) {
    61        try {
    62          yield RSVP.all([
    63            this.get('store').findAll(modelName, { reload: true, adapterOptions: { watch: true } }),
    64            wait(throttle),
    65          ]);
    66        } catch (e) {
    67          yield e;
    68          break;
    69        } finally {
    70          this.get('store')
    71            .adapterFor(modelName)
    72            .cancelFindAll(modelName);
    73        }
    74      }
    75    }).drop();
    76  }