-
Notifications
You must be signed in to change notification settings - Fork 90
/
ember-scheduler.ts
271 lines (243 loc) · 6.67 KB
/
ember-scheduler.ts
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { Promise as EmberPromise } from 'rsvp';
import { join, scheduleOnce } from '@ember/runloop';
import { addObserver } from '@ember/object/observers';
import { computed, set } from '@ember/object';
import ComputedProperty from '@ember/object/computed';
import { gte } from 'ember-compatibility-helpers';
import { spawn, current, stop, logErrors } from './scheduler';
import Ember from 'ember';
import { microwait } from '..';
import { DEBUG } from '@glimmer/env';
export function task(taskFn: (...args: any[]) => Generator) {
let tp = (_computed(function(this: object, propertyName: string) {
return new Task(this, taskFn, tp, propertyName);
}) as unknown) as TaskProperty;
Object.setPrototypeOf(tp, TaskProperty.prototype);
return tp as ((proto: object, key: string) => any) & TaskProperty;
}
function _computed(fn: (this: object, propertyName: string) => Task) {
if (!gte('3.10.0')) {
return computed(fn);
}
let cp = function(proto: object, key: string) {
if ((cp as any).setup !== undefined) {
(cp as any).setup(proto, key);
}
return (computed(fn) as any)(...arguments);
};
(Ember as any)._setClassicDecorator(cp);
return cp;
}
let handlerCounter = 0;
let BaseTaskProperty: { new (): object };
if (gte('3.10.0')) {
BaseTaskProperty = class {};
} else {
BaseTaskProperty = ComputedProperty;
}
type BufferPolicy = (task: Task, priv: TaskPrivate) => Promise<void> | void;
export class TaskProperty extends BaseTaskProperty {
_bufferPolicy: BufferPolicy | undefined;
private _observes: string[] | undefined;
restartable() {
this._bufferPolicy = cancelAllButLast;
return this;
}
drop() {
this._bufferPolicy = drop;
return this;
}
observes(...deps: string[]) {
this._observes = deps;
return this;
}
setup(proto: object, taskName: string) {
// @ts-ignore: depending on the ember version we may or may not have a super
// method.
if (super.setup) {
// @ts-ignore
super.setup(...arguments);
}
if (this._observes) {
let handlerName = `_ember_animated_handler_${handlerCounter++}`;
(proto as any)[handlerName] = function() {
let task = this.get(taskName);
scheduleOnce('actions', task, '_safePerform');
};
for (let i = 0; i < this._observes.length; ++i) {
let name = this._observes[i];
(addObserver as any)(proto, name, null, handlerName);
}
}
}
}
interface TaskPrivate {
context: object;
implementation: (...args: unknown[]) => Generator;
instances: Promise<void>[];
taskProperty: TaskProperty;
name: string;
}
let priv: WeakMap<Task, TaskPrivate> = new WeakMap();
function getPriv(task: Task): TaskPrivate {
return priv.get(task)!;
}
export class Task {
concurrency = 0;
isRunning = false;
constructor(
context: object,
implementation: TaskPrivate['implementation'],
taskProperty: TaskProperty,
name: string,
) {
priv.set(this, {
context,
implementation,
instances: [],
taskProperty,
name,
});
}
perform(...args: unknown[]) {
let self = this;
let privSelf = getPriv(this);
let context = privSelf.context;
let implementation = privSelf.implementation;
let policy = privSelf.taskProperty._bufferPolicy;
if ((context as any).isDestroyed) {
throw new Error(
`Tried to perform task ${privSelf.name} on an already destroyed object`,
);
}
cleanupOnDestroy(context, this);
return spawn(function*() {
if (DEBUG) {
logErrors(error => {
microwait().then(() => {
throw error;
});
});
}
try {
self._addInstance(current()!);
if (policy) {
let maybeWait = policy(self, privSelf);
if (maybeWait) {
yield maybeWait;
}
}
let finalValue = yield* withRunLoop(
implementation.call(context, ...args),
);
return finalValue;
} finally {
join(() => {
self._removeInstance(current()!);
});
}
});
}
cancelAll() {
getPriv(this).instances.forEach(i => stop(i));
}
_addInstance(i: Promise<void>) {
getPriv(this).instances.push(i);
set(this, 'isRunning', true);
set(this, 'concurrency', this.concurrency + 1);
}
_removeInstance(i: Promise<void>) {
let instances = getPriv(this).instances;
instances.splice(instances.indexOf(i), 1);
set(this, 'concurrency', this.concurrency - 1);
set(this, 'isRunning', this.concurrency > 0);
}
_safePerform() {
let { context } = getPriv(this);
if (!(context as any).isDestroyed) {
this.perform();
}
}
}
// cribbed from machty's ember-concurrency
function cleanupOnDestroy(
owner: {
willDestroy?: Function & {
__ember_processes_destroyers__?: (() => void)[];
};
},
object: { cancelAll(): void },
) {
if (!owner.willDestroy) {
// we're running in non Ember object (possibly in a test mock)
return;
}
if (!owner.willDestroy.__ember_processes_destroyers__) {
let oldWillDestroy = owner.willDestroy;
let disposers: (() => void)[] = [];
owner.willDestroy = function() {
for (let i = 0, l = disposers.length; i < l; i++) {
disposers[i]();
}
oldWillDestroy.apply(owner, arguments);
};
/* eslint-disable-next-line @typescript-eslint/camelcase */
owner.willDestroy.__ember_processes_destroyers__ = disposers;
}
owner.willDestroy.__ember_processes_destroyers__.push(() => {
try {
object.cancelAll();
} catch (err) {
if (err.message !== 'TaskCancelation') {
throw err;
}
}
});
}
function cancelAllButLast(_task: Task, privTask: TaskPrivate) {
let instances = privTask.instances;
for (let i = 0; i < instances.length - 1; i++) {
stop(instances[i]);
}
}
function drop(_task: Task, privTask: TaskPrivate) {
let instances = privTask.instances;
for (let i = 1; i < instances.length; i++) {
stop(instances[i]);
}
}
function* withRunLoop(generator: Generator) {
let state!: IteratorResult<any>;
let threw: Error | undefined;
let nextValue: any;
let fulfilled = true;
while (true) {
join(() => {
try {
if (fulfilled) {
state = generator.next(nextValue);
} else {
state = generator.throw(nextValue);
}
} catch (err) {
threw = err;
}
});
if (threw) {
throw threw;
}
if (state.done) {
return state.value;
}
try {
nextValue = yield state.value;
fulfilled = true;
} catch (err) {
nextValue = err;
fulfilled = false;
}
}
}
export function timeout(ms: number) {
return new EmberPromise(resolve => setTimeout(resolve, ms));
}