-
Notifications
You must be signed in to change notification settings - Fork 2k
/
watch.js
161 lines (152 loc) · 4.44 KB
/
watch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
// @ts-check
import Ember from 'ember';
import { get } from '@ember/object';
import { assert } from '@ember/debug';
import RSVP from 'rsvp';
import { task } from 'ember-concurrency';
import { AbortController } from 'fetch';
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;
/**
* @typedef watchRecordOptions
* @property {boolean} [shouldSurfaceErrors=false] - If true, the task will throw errors instead of yielding them.
*/
/**
* @param {string} modelName - The name of the model to watch.
* @param {watchRecordOptions} [options]
*/
export function watchRecord(modelName, { shouldSurfaceErrors = false } = {}) {
return task(function* (id, throttle = 2000) {
assert(
'To watch a record, the record adapter MUST extend Watchable',
this.store.adapterFor(modelName) instanceof Watchable
);
if (typeof id === 'object') {
id = get(id, 'id');
}
while (isEnabled && !Ember.testing) {
const controller = new AbortController();
try {
yield RSVP.all([
this.store.findRecord(modelName, id, {
reload: true,
adapterOptions: { watch: true, abortController: controller },
}),
wait(throttle),
]);
} catch (e) {
if (shouldSurfaceErrors) {
throw e;
}
yield e;
break;
} finally {
controller.abort();
}
}
}).drop();
}
export function watchRelationship(relationshipName, replace = false) {
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();
try {
yield RSVP.all([
this.store
.adapterFor(model.constructor.modelName)
.reloadRelationship(model, relationshipName, {
watch: true,
abortController: controller,
replace,
}),
wait(throttle),
]);
} catch (e) {
yield e;
break;
} finally {
controller.abort();
}
}
}).drop();
}
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();
}
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();
try {
yield RSVP.all([
this.store.findAll(modelName, {
reload: true,
adapterOptions: { watch: true, abortController: controller },
}),
wait(throttle),
]);
} catch (e) {
yield e;
break;
} finally {
controller.abort();
}
}
}).drop();
}
export function watchQuery(modelName) {
return task(function* (params, throttle = 2000 /*options = {}*/) {
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,
adapterOptions: { watch: true, abortController: controller },
}),
wait(throttle),
]);
} catch (e) {
yield e;
break;
} finally {
controller.abort();
}
}
}).drop();
}