-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.ts
378 lines (355 loc) · 9.76 KB
/
index.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import {
decoratorWithParams,
DecoratorDescriptor
} from '@ember-decorators/utils/decorator';
import { assert } from '@ember/debug';
import {
task as createTaskProperty,
taskGroup as createTaskGroupProperty,
TaskProperty,
TaskGroupProperty,
Task,
GeneratorFn
} from 'ember-concurrency';
export { default as lastValue } from './last-value';
type TaskOptions = {
[option in keyof TaskProperty]?: Parameters<TaskProperty[option]> extends [
infer P
]
? P
: true;
};
type TaskGroupOptions = {
[option in keyof TaskGroupProperty]?: Parameters<
TaskGroupProperty[option]
> extends [infer P]
? P
: true;
};
type Decorator = (
...args: Parameters<MethodDecorator>
) => Exclude<ReturnType<MethodDecorator>, void>;
type ObjectValues<O> = O extends { [s: string]: infer V } ? V : never;
/**
* This utility function assures compatibility with the Ember object model style
* and initializer syntax required by Babel 6.
*
* For native classes using the method shorthand style (TypeScript & Babel 7),
* this function will access the `value`. For legacy code it will get the value
* from the initializer.
*
* // Ember object model
* export default EmberObject.extend({
* @task
* someTask: function*() {}
* });
*
* // Class syntax with initializers
* export default class extends EmberObject {
* @task
* someTask = function*() {}
* }
*
* @param desc
* @returns {object|null}
* @private
*/
function extractValue(desc: DecoratorDescriptor): any {
if (typeof desc.initializer === 'function') {
return desc.initializer.call(null);
}
if (typeof desc.get === 'function') {
return desc.get.call(null);
}
if (desc.value) {
return desc.value;
}
}
/**
* Takes a `PropertyDescriptor` and turns it into an ember-concurrency
* `TaskProperty`.
*
* @param desc
* @returns {TaskProperty}
* @private
*/
function createTaskFromDescriptor(desc: DecoratorDescriptor) {
const value = extractValue(desc);
assert(
'ember-concurrency-decorators: Can only decorate a generator function as a task or an object with a generator method `perform` as an encapsulated task.',
typeof value === 'function' ||
(typeof value === 'object' && typeof value.perform === 'function')
);
return (createTaskProperty(value) as unknown) as TaskProperty;
}
/**
* Takes a `PropertyDescriptor` and turns it into an ember-concurrency
* `TaskGroupProperty`.
*
* @param desc
* @returns {TaskGroupProperty}
* @private
*/
function createTaskGroupFromDescriptor(_desc: DecoratorDescriptor) {
return createTaskGroupProperty();
}
/**
* Applies the `options` provided using the chaining API on the given `task`.
*
* @param options
* @param {TaskProperty|TaskGroupProperty} task
* @private
*/
function applyOptions(
options: TaskGroupOptions,
taskProperty: TaskGroupProperty
): TaskGroupProperty & Decorator;
function applyOptions(
options: TaskOptions,
taskProperty: TaskProperty
): TaskProperty & Decorator {
return Object.entries(options).reduce(
(
// eslint-disable-next-line no-shadow
taskProperty,
[key, value]: [
keyof typeof options,
ObjectValues<Required<typeof options>>
]
) => {
assert(
`ember-concurrency-decorators: Option '${key}' is not a valid function`,
typeof taskProperty[key] === 'function'
);
if (value === true) {
return (taskProperty[key] as () => typeof taskProperty)();
}
return (taskProperty[key] as (o: typeof value) => typeof taskProperty)(
value
);
},
taskProperty
// The CP decorator gets executed in `createDecorator`
) as TaskProperty & Decorator;
}
/**
* Creates a decorator function that transforms the decorated property using the
* given `propertyCreator` and accepts an optional user provided options hash,
* that that will be merged with the `baseOptions`.
*
* @param {function} propertyCreator
* @param {object} [baseOptions={}]
* @private
*/
const createDecorator = (
propertyCreator: (
desc: DecoratorDescriptor
) => TaskProperty | TaskGroupProperty,
baseOptions: TaskOptions | TaskGroupOptions = {}
) =>
decoratorWithParams<[typeof baseOptions?], object>(
(target, key, desc, [userOptions] = []) => {
const { initializer, value } = desc;
delete desc.initializer;
delete desc.value;
return applyOptions(
Object.assign({}, baseOptions, userOptions),
propertyCreator({ ...desc, initializer, value })
)(target, key, desc);
}
) as (PropertyDecorator &
((options: Record<string, any>) => PropertyDecorator));
/**
* Turns the decorated generator function into a task.
*
* Optionally takes a hash of options that will be applied as modifiers to the
* task. For instance `maxConcurrency`, `on`, `group` or `keepLatest`.
*
* ```js
* import EmberObject from '@ember/object';
* import { task } from 'ember-concurrency-decorators';
*
* class extends EmberObject {
* @task
* *plainTask() {}
*
* @task({ maxConcurrency: 5, keepLatest: true, cancelOn: 'click' })
* *taskWithModifiers() {}
* }
* ```
*
* @function
* @param {object?} [options={}]
* @return {TaskProperty}
*/
const taskDecorator = createDecorator(createTaskFromDescriptor);
export function task<Args extends any[], R>(
taskFn: GeneratorFn<Args, R>
): Task<Args, Exclude<R, Promise<any>>>;
export function task<Args extends any[], R>(encapsulatedTask: {
perform: GeneratorFn<Args, R>;
}): Task<Args, Exclude<R, Promise<any>>>;
export function task(options: TaskOptions): PropertyDecorator;
export function task(
target: Record<string, any>,
propertyKey: string | symbol
): void;
export function task<Args extends any[], R>(
...args:
| [GeneratorFn<Args, R>]
| [{ perform: GeneratorFn<Args, R> }]
| [TaskOptions]
| [Record<string, any>, string | symbol]
): Task<Args, Exclude<R, Promise<any>>> | PropertyDecorator | void {
const [firstParam] = args;
if (
typeof firstParam === 'function' ||
(typeof firstParam === 'object' &&
// @ts-ignore
typeof firstParam.perform === 'function')
)
// @ts-ignore
return firstParam;
// @ts-ignore
return taskDecorator(...args);
}
/**
* Turns the decorated generator function into a task and applies the
* `restartable` modifier.
*
* Optionally takes a hash of further options that will be applied as modifiers
* to the task.
*
* @function
* @param {object?} [options={}]
* @return {TaskProperty}
*/
export const restartableTask = createDecorator(createTaskFromDescriptor, {
restartable: true
});
/**
* Turns the decorated generator function into a task and applies the
* `drop` modifier.
*
* Optionally takes a hash of further options that will be applied as modifiers
* to the task.
*
* @function
* @param {object?} [options={}]
* @return {TaskProperty}
*/
export const dropTask = createDecorator(createTaskFromDescriptor, {
drop: true
});
/**
* Turns the decorated generator function into a task and applies the
* `keepLatest` modifier.
*
* Optionally takes a hash of further options that will be applied as modifiers
* to the task.
*
* @function
* @param {object?} [options={}]
* @return {TaskProperty}
*/
export const keepLatestTask = createDecorator(createTaskFromDescriptor, {
keepLatest: true
});
/**
* Turns the decorated generator function into a task and applies the
* `enqueue` modifier.
*
* Optionally takes a hash of further options that will be applied as modifiers
* to the task.
*
* @function
* @param {object?} [options={}]
* @return {TaskProperty}
*/
export const enqueueTask = createDecorator(createTaskFromDescriptor, {
enqueue: true
});
/**
* Turns the decorated property into a task group.
*
* Optionally takes a hash of options that will be applied as modifiers to the
* task group. For instance `maxConcurrency` or `keepLatest`.
*
* ```js
* import EmberObject from '@ember/object';
* import { task taskGroup } from 'ember-concurrency-decorators';
*
* class extends EmberObject {
* @taskGroup({ maxConcurrency: 5 }) someTaskGroup;
*
* @task({ group: 'someTaskGroup' })
* *someTask() {}
*
* @task({ group: 'someTaskGroup' })
* *anotherTask() {}
* }
* ```
*
* @function
* @param {object?} [options={}]
* @return {TaskGroupProperty}
*/
export const taskGroup = createDecorator(createTaskGroupFromDescriptor);
/**
* Turns the decorated property into a task group and applies the
* `restartable` modifier.
*
* Optionally takes a hash of further options that will be applied as modifiers
* to the task group.
*
* @function
* @param {object?} [options={}]
* @return {TaskGroupProperty}
*/
export const restartableTaskGroup = createDecorator(
createTaskGroupFromDescriptor,
{ restartable: true }
);
/**
* Turns the decorated property into a task group and applies the
* `drop` modifier.
*
* Optionally takes a hash of further options that will be applied as modifiers
* to the task group.
*
* @function
* @param {object?} [options={}]
* @return {TaskGroupProperty}
*/
export const dropTaskGroup = createDecorator(createTaskGroupFromDescriptor, {
drop: true
});
/**
* Turns the decorated property into a task group and applies the
* `keepLatest` modifier.
*
* Optionally takes a hash of further options that will be applied as modifiers
* to the task group.
*
* @function
* @param {object?} [options={}]
* @return {TaskGroupProperty}
*/
export const keepLatestTaskGroup = createDecorator(
createTaskGroupFromDescriptor,
{ keepLatest: true }
);
/**
* Turns the decorated property into a task group and applies the
* `enqueue` modifier.
*
* Optionally takes a hash of further options that will be applied as modifiers
* to the task group.
*
* @function
* @param {object?} [options={}]
* @return {TaskGroupProperty}
*/
export const enqueueTaskGroup = createDecorator(createTaskGroupFromDescriptor, {
enqueue: true
});