2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
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';
|
2020-05-14 21:36:38 +00:00
|
|
|
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';
|
2020-05-14 21:36:38 +00:00
|
|
|
import Watchable from 'nomad-ui/adapters/watchable';
|
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) {
|
2021-12-28 14:45:20 +00:00
|
|
|
return task(function* (id, throttle = 2000) {
|
2020-05-14 21:36:38 +00:00
|
|
|
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');
|
|
|
|
}
|
2018-07-05 17:17:56 +00:00
|
|
|
while (isEnabled && !Ember.testing) {
|
2020-06-17 08:45:36 +00:00
|
|
|
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;
|
2018-02-14 23:41:07 +00:00
|
|
|
} finally {
|
2020-05-20 23:18:23 +00:00
|
|
|
controller.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
|
|
|
}
|
|
|
|
|
2022-08-26 03:22:55 +00:00
|
|
|
export function watchRelationship(relationshipName, replace = false) {
|
2021-12-28 14:45:20 +00:00
|
|
|
return task(function* (model, throttle = 2000) {
|
2020-05-14 21:36:38 +00:00
|
|
|
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
|
|
|
|
);
|
2018-07-05 17:17:56 +00:00
|
|
|
while (isEnabled && !Ember.testing) {
|
2020-06-17 08:45:36 +00:00
|
|
|
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,
|
2022-08-26 03:22:55 +00:00
|
|
|
replace,
|
2020-05-20 23:18:23 +00:00
|
|
|
}),
|
2018-02-12 23:23:07 +00:00
|
|
|
wait(throttle),
|
|
|
|
]);
|
|
|
|
} catch (e) {
|
|
|
|
yield e;
|
|
|
|
break;
|
2018-02-14 23:41:07 +00:00
|
|
|
} finally {
|
2020-05-20 23:18:23 +00:00
|
|
|
controller.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
|
|
|
}
|
|
|
|
|
2022-08-26 22:02:12 +00:00
|
|
|
export function watchNonStoreRecords(modelName) {
|
|
|
|
return task(function* (model, asyncCallbackName, throttle = 5000) {
|
|
|
|
assert(
|
|
|
|
'To watch a non-store records, the adapter of the model provided to the watchNonStoreRecords task MUST extend Watchable',
|
|
|
|
this.store.adapterFor(modelName) instanceof Watchable
|
|
|
|
);
|
|
|
|
while (isEnabled && !Ember.testing) {
|
|
|
|
const controller = new AbortController();
|
|
|
|
try {
|
|
|
|
yield model[asyncCallbackName]();
|
|
|
|
yield wait(throttle);
|
|
|
|
} catch (e) {
|
|
|
|
yield e;
|
|
|
|
break;
|
|
|
|
} finally {
|
|
|
|
controller.abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop();
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:23:07 +00:00
|
|
|
export function watchAll(modelName) {
|
2021-12-28 14:45:20 +00:00
|
|
|
return task(function* (throttle = 2000) {
|
2020-05-14 21:36:38 +00:00
|
|
|
assert(
|
|
|
|
'To watch all, the respective adapter MUST extend Watchable',
|
|
|
|
this.store.adapterFor(modelName) instanceof Watchable
|
|
|
|
);
|
2018-07-05 17:17:56 +00:00
|
|
|
while (isEnabled && !Ember.testing) {
|
2020-06-17 08:45:36 +00:00
|
|
|
const controller = new AbortController();
|
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,
|
2020-05-20 23:18:23 +00:00
|
|
|
adapterOptions: { watch: true, abortController: controller },
|
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 {
|
2020-05-20 23:18:23 +00:00
|
|
|
controller.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) {
|
2021-12-28 14:45:20 +00:00
|
|
|
return task(function* (params, throttle = 10000) {
|
2020-05-14 21:36:38 +00:00
|
|
|
assert(
|
|
|
|
'To watch a query, the adapter for the type being queried MUST extend Watchable',
|
|
|
|
this.store.adapterFor(modelName) instanceof Watchable
|
|
|
|
);
|
2020-03-25 12:51:26 +00:00
|
|
|
while (isEnabled && !Ember.testing) {
|
2020-06-17 08:45:36 +00:00
|
|
|
const controller = new AbortController();
|
2020-03-25 12:51:26 +00:00
|
|
|
try {
|
|
|
|
yield RSVP.all([
|
|
|
|
this.store.query(modelName, params, {
|
|
|
|
reload: true,
|
2020-05-20 23:18:23 +00:00
|
|
|
adapterOptions: { watch: true, abortController: controller },
|
2020-03-25 12:51:26 +00:00
|
|
|
}),
|
|
|
|
wait(throttle),
|
|
|
|
]);
|
|
|
|
} catch (e) {
|
|
|
|
yield e;
|
|
|
|
break;
|
|
|
|
} finally {
|
2020-05-20 23:18:23 +00:00
|
|
|
controller.abort();
|
2020-03-25 12:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop();
|
|
|
|
}
|