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';
|
2019-05-17 03:19:49 +00:00
|
|
|
import XHRToken from 'nomad-ui/utils/classes/xhr-token';
|
2018-07-05 17:17:56 +00:00
|
|
|
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) {
|
2019-05-17 03:19:49 +00:00
|
|
|
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');
|
|
|
|
}
|
2018-07-05 17:17:56 +00:00
|
|
|
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;
|
2018-02-14 23:41:07 +00:00
|
|
|
} finally {
|
2019-05-17 03:19:49 +00:00
|
|
|
token.abort();
|
2018-02-08 23:43:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-14 23:41:07 +00:00
|
|
|
}).drop();
|
2018-02-08 23:43:54 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 23:41:07 +00:00
|
|
|
export function watchRelationship(relationshipName) {
|
2018-02-12 23:23:07 +00:00
|
|
|
return task(function*(model, throttle = 2000) {
|
2019-05-17 03:19:49 +00:00
|
|
|
const token = new XHRToken();
|
2018-07-05 17:17:56 +00:00
|
|
|
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;
|
2018-02-14 23:41:07 +00:00
|
|
|
} finally {
|
2019-05-17 03:19:49 +00:00
|
|
|
token.abort();
|
2018-02-12 23:23:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-14 23:41:07 +00:00
|
|
|
}).drop();
|
2018-02-12 23:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function watchAll(modelName) {
|
|
|
|
return task(function*(throttle = 2000) {
|
2019-05-17 03:19:49 +00:00
|
|
|
const token = new XHRToken();
|
2018-07-05 17:17:56 +00:00
|
|
|
while (isEnabled && !Ember.testing) {
|
2018-02-12 23:23:07 +00:00
|
|
|
try {
|
|
|
|
yield RSVP.all([
|
2019-05-17 03:19:49 +00:00
|
|
|
this.store.findAll(modelName, {
|
|
|
|
reload: true,
|
2019-05-20 18:08:16 +00:00
|
|
|
adapterOptions: { watch: true, abortToken: token },
|
2019-05-17 03:19:49 +00:00
|
|
|
}),
|
2018-02-12 23:23:07 +00:00
|
|
|
wait(throttle),
|
2018-02-08 23:43:54 +00:00
|
|
|
]);
|
|
|
|
} catch (e) {
|
|
|
|
yield e;
|
|
|
|
break;
|
2018-02-14 23:41:07 +00:00
|
|
|
} finally {
|
2019-05-17 03:19:49 +00:00
|
|
|
token.abort();
|
2018-02-08 23:43:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-14 23:41:07 +00:00
|
|
|
}).drop();
|
2018-02-08 23:43:54 +00:00
|
|
|
}
|
2020-03-25 12:51:26 +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();
|
|
|
|
}
|