github.com/aminovpavel/nomad@v0.11.8/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 XHRToken from 'nomad-ui/utils/classes/xhr-token';
     7  import config from 'nomad-ui/config/environment';
     8  
     9  const isEnabled = config.APP.blockingQueries !== false;
    10  
    11  export function watchRecord(modelName) {
    12    return task(function*(id, throttle = 2000) {
    13      const token = new XHRToken();
    14      if (typeof id === 'object') {
    15        id = get(id, 'id');
    16      }
    17      while (isEnabled && !Ember.testing) {
    18        try {
    19          yield RSVP.all([
    20            this.store.findRecord(modelName, id, {
    21              reload: true,
    22              adapterOptions: { watch: true, abortToken: token },
    23            }),
    24            wait(throttle),
    25          ]);
    26        } catch (e) {
    27          yield e;
    28          break;
    29        } finally {
    30          token.abort();
    31        }
    32      }
    33    }).drop();
    34  }
    35  
    36  export function watchRelationship(relationshipName) {
    37    return task(function*(model, throttle = 2000) {
    38      const token = new XHRToken();
    39      while (isEnabled && !Ember.testing) {
    40        try {
    41          yield RSVP.all([
    42            this.store
    43              .adapterFor(model.constructor.modelName)
    44              .reloadRelationship(model, relationshipName, { watch: true, abortToken: token }),
    45            wait(throttle),
    46          ]);
    47        } catch (e) {
    48          yield e;
    49          break;
    50        } finally {
    51          token.abort();
    52        }
    53      }
    54    }).drop();
    55  }
    56  
    57  export function watchAll(modelName) {
    58    return task(function*(throttle = 2000) {
    59      const token = new XHRToken();
    60      while (isEnabled && !Ember.testing) {
    61        try {
    62          yield RSVP.all([
    63            this.store.findAll(modelName, {
    64              reload: true,
    65              adapterOptions: { watch: true, abortToken: token },
    66            }),
    67            wait(throttle),
    68          ]);
    69        } catch (e) {
    70          yield e;
    71          break;
    72        } finally {
    73          token.abort();
    74        }
    75      }
    76    }).drop();
    77  }
    78  
    79  export function watchQuery(modelName) {
    80    return task(function*(params, throttle = 10000) {
    81      const token = new XHRToken();
    82      while (isEnabled && !Ember.testing) {
    83        try {
    84          yield RSVP.all([
    85            this.store.query(modelName, params, {
    86              reload: true,
    87              adapterOptions: { watch: true, abortToken: token },
    88            }),
    89            wait(throttle),
    90          ]);
    91        } catch (e) {
    92          yield e;
    93          break;
    94        } finally {
    95          token.abort();
    96        }
    97      }
    98    }).drop();
    99  }