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';
|
|
|
|
|
|
|
|
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 (!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;
|
2018-02-14 23:41:07 +00:00
|
|
|
} finally {
|
|
|
|
this.get('store')
|
|
|
|
.adapterFor(modelName)
|
|
|
|
.cancelFindRecord(modelName, id);
|
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) {
|
2018-02-16 02:55:59 +00:00
|
|
|
while (!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)
|
2018-02-14 23:41:07 +00:00
|
|
|
.reloadRelationship(model, relationshipName, true),
|
2018-02-12 23:23:07 +00:00
|
|
|
wait(throttle),
|
|
|
|
]);
|
|
|
|
} catch (e) {
|
|
|
|
yield e;
|
|
|
|
break;
|
2018-02-14 23:41:07 +00:00
|
|
|
} finally {
|
|
|
|
this.get('store')
|
2018-02-16 02:55:59 +00:00
|
|
|
.adapterFor(model.constructor.modelName)
|
2018-02-14 23:41:07 +00:00
|
|
|
.cancelReloadRelationship(model, relationshipName);
|
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) {
|
2018-02-16 02:55:59 +00:00
|
|
|
while (!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;
|
2018-02-14 23:41:07 +00:00
|
|
|
} finally {
|
|
|
|
this.get('store')
|
|
|
|
.adapterFor(modelName)
|
|
|
|
.cancelFindAll(modelName);
|
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
|
|
|
}
|