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

121 lines
3.3 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 { assert } from '@ember/debug';
2018-02-08 23:43:54 +00:00
import RSVP from 'rsvp';
import { task } from 'ember-concurrency';
2020-05-20 23:18:23 +00:00
import { AbortController } from 'fetch';
2018-02-08 23:43:54 +00:00
import wait from 'nomad-ui/utils/wait';
import Watchable from 'nomad-ui/adapters/watchable';
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) {
assert(
'To watch a record, the record adapter MUST extend Watchable',
this.store.adapterFor(modelName) instanceof Watchable
);
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) {
const controller = new AbortController();
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,
2020-05-20 23:18:23 +00:00
adapterOptions: { watch: true, abortController: controller },
2018-02-12 23:23:35 +00:00
}),
wait(throttle),
2018-02-08 23:43:54 +00:00
]);
} catch (e) {
yield e;
break;
} finally {
2020-05-20 23:18:23 +00:00
controller.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) {
assert(
'To watch a relationship, the adapter of the model provided to the watchRelationship task MUST extend Watchable',
this.store.adapterFor(model.constructor.modelName) instanceof Watchable
);
while (isEnabled && !Ember.testing) {
const controller = new AbortController();
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)
2020-05-20 23:18:23 +00:00
.reloadRelationship(model, relationshipName, {
watch: true,
abortController: controller,
}),
2018-02-12 23:23:07 +00:00
wait(throttle),
]);
} catch (e) {
yield e;
break;
} finally {
2020-05-20 23:18:23 +00:00
controller.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) {
assert(
'To watch all, the respective adapter MUST extend Watchable',
this.store.adapterFor(modelName) instanceof Watchable
);
while (isEnabled && !Ember.testing) {
const controller = new AbortController();
2018-02-12 23:23:07 +00:00
try {
yield RSVP.all([
this.store.findAll(modelName, {
reload: true,
2020-05-20 23:18:23 +00:00
adapterOptions: { watch: true, abortController: controller },
}),
2018-02-12 23:23:07 +00:00
wait(throttle),
2018-02-08 23:43:54 +00:00
]);
} catch (e) {
yield e;
break;
} finally {
2020-05-20 23:18:23 +00:00
controller.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) {
assert(
'To watch a query, the adapter for the type being queried MUST extend Watchable',
this.store.adapterFor(modelName) instanceof Watchable
);
while (isEnabled && !Ember.testing) {
const controller = new AbortController();
try {
yield RSVP.all([
this.store.query(modelName, params, {
reload: true,
2020-05-20 23:18:23 +00:00
adapterOptions: { watch: true, abortController: controller },
}),
wait(throttle),
]);
} catch (e) {
yield e;
break;
} finally {
2020-05-20 23:18:23 +00:00
controller.abort();
}
}
}).drop();
}