open-nomad/ui/app/utils/properties/watch.js

77 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-02-16 02:55:59 +00:00
import Ember from 'ember';
2018-02-08 23:43:54 +00:00
import { get } from '@ember/object';
import RSVP from 'rsvp';
import { task } from 'ember-concurrency';
import wait from 'nomad-ui/utils/wait';
import config from 'nomad-ui/config/environment';
const isEnabled = config.APP.blockingQueries !== false;
2018-02-08 23:43:54 +00:00
export function watchRecord(modelName) {
2018-02-12 23:23:35 +00:00
return task(function*(id, throttle = 2000) {
2018-02-22 21:21:32 +00:00
if (typeof id === 'object') {
2018-02-16 02:55:59 +00:00
id = get(id, 'id');
}
while (isEnabled && !Ember.testing) {
2018-02-08 23:43:54 +00:00
try {
yield RSVP.all([
2018-02-12 23:23:35 +00:00
this.get('store').findRecord(modelName, id, {
reload: true,
adapterOptions: { watch: true },
}),
wait(throttle),
2018-02-08 23:43:54 +00:00
]);
} catch (e) {
yield e;
break;
} finally {
this.get('store')
.adapterFor(modelName)
.cancelFindRecord(modelName, id);
2018-02-08 23:43:54 +00:00
}
}
}).drop();
2018-02-08 23:43:54 +00:00
}
export function watchRelationship(relationshipName) {
2018-02-12 23:23:07 +00:00
return task(function*(model, throttle = 2000) {
while (isEnabled && !Ember.testing) {
2018-02-08 23:43:54 +00:00
try {
yield RSVP.all([
2018-02-12 23:23:07 +00:00
this.get('store')
.adapterFor(model.constructor.modelName)
.reloadRelationship(model, relationshipName, true),
2018-02-12 23:23:07 +00:00
wait(throttle),
]);
} catch (e) {
yield e;
break;
} finally {
this.get('store')
2018-02-16 02:55:59 +00:00
.adapterFor(model.constructor.modelName)
.cancelReloadRelationship(model, relationshipName);
2018-02-12 23:23:07 +00:00
}
}
}).drop();
2018-02-12 23:23:07 +00:00
}
export function watchAll(modelName) {
return task(function*(throttle = 2000) {
while (isEnabled && !Ember.testing) {
2018-02-12 23:23:07 +00:00
try {
yield RSVP.all([
this.get('store').findAll(modelName, { reload: true, adapterOptions: { watch: true } }),
wait(throttle),
2018-02-08 23:43:54 +00:00
]);
} catch (e) {
yield e;
break;
} finally {
this.get('store')
.adapterFor(modelName)
.cancelFindAll(modelName);
2018-02-08 23:43:54 +00:00
}
}
}).drop();
2018-02-08 23:43:54 +00:00
}