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

100 lines
2.4 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 XHRToken from 'nomad-ui/utils/classes/xhr-token';
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) {
const token = new XHRToken();
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([
2019-03-26 07:46:44 +00:00
this.store.findRecord(modelName, id, {
2018-02-12 23:23:35 +00:00
reload: true,
2019-05-20 18:08:16 +00:00
adapterOptions: { watch: true, abortToken: token },
2018-02-12 23:23:35 +00:00
}),
wait(throttle),
2018-02-08 23:43:54 +00:00
]);
} catch (e) {
yield e;
break;
} finally {
token.abort();
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) {
const token = new XHRToken();
while (isEnabled && !Ember.testing) {
2018-02-08 23:43:54 +00:00
try {
yield RSVP.all([
2019-03-26 07:46:44 +00:00
this.store
2018-02-12 23:23:07 +00:00
.adapterFor(model.constructor.modelName)
2019-05-20 18:08:16 +00:00
.reloadRelationship(model, relationshipName, { watch: true, abortToken: token }),
2018-02-12 23:23:07 +00:00
wait(throttle),
]);
} catch (e) {
yield e;
break;
} finally {
token.abort();
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) {
const token = new XHRToken();
while (isEnabled && !Ember.testing) {
2018-02-12 23:23:07 +00:00
try {
yield RSVP.all([
this.store.findAll(modelName, {
reload: true,
2019-05-20 18:08:16 +00:00
adapterOptions: { watch: true, abortToken: token },
}),
2018-02-12 23:23:07 +00:00
wait(throttle),
2018-02-08 23:43:54 +00:00
]);
} catch (e) {
yield e;
break;
} finally {
token.abort();
2018-02-08 23:43:54 +00:00
}
}
}).drop();
2018-02-08 23:43:54 +00:00
}
export function watchQuery(modelName) {
return task(function*(params, throttle = 10000) {
const token = new XHRToken();
while (isEnabled && !Ember.testing) {
try {
yield RSVP.all([
this.store.query(modelName, params, {
reload: true,
adapterOptions: { watch: true, abortToken: token },
}),
wait(throttle),
]);
} catch (e) {
yield e;
break;
} finally {
token.abort();
}
}
}).drop();
}