-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathindex.js
148 lines (135 loc) · 4.06 KB
/
index.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
import Ember from 'ember';
import { computed } from '@ember/object';
import { timeout, forever } from './utils';
import { Task, TaskProperty } from './-task-property';
import { didCancel } from './-task-instance';
import { TaskGroup, TaskGroupProperty } from './-task-group';
import { all, allSettled, hash, race } from './-cancelable-promise-helpers';
import { waitForQueue, waitForEvent, waitForProperty } from './-wait-for';
import { resolveScheduler } from './-property-modifiers-mixin';
import { gte } from 'ember-compatibility-helpers';
function _computed(fn) {
if (gte('3.10.0')) {
let cp = function(proto, key) {
if (cp.setup !== undefined) {
cp.setup(proto, key);
}
return computed(fn)(...arguments);
};
Ember._setComputedDecorator(cp);
return cp;
} else {
return computed(fn);
}
}
/**
* A Task is a cancelable, restartable, asynchronous operation that
* is driven by a generator function. Tasks are automatically canceled
* when the object they live on is destroyed (e.g. a Component
* is unrendered).
*
* To define a task, use the `task(...)` function, and pass in
* a generator function, which will be invoked when the task
* is performed. The reason generator functions are used is
* that they (like the proposed ES7 async-await syntax) can
* be used to elegantly express asynchronous, cancelable
* operations.
*
* You can also define an
* <a href="/#/docs/encapsulated-task">Encapsulated Task</a>
* by passing in an object that defined a `perform` generator
* function property.
*
* The following Component defines a task called `myTask` that,
* when performed, prints a message to the console, sleeps for 1 second,
* prints a final message to the console, and then completes.
*
* ```js
* import { task, timeout } from 'ember-concurrency';
* export default Component.extend({
* myTask: task(function * () {
* console.log("Pausing for a second...");
* yield timeout(1000);
* console.log("Done!");
* })
* });
* ```
*
* ```hbs
* <button {{action myTask.perform}}>Perform Task</button>
* ```
*
* By default, tasks have no concurrency constraints
* (multiple instances of a task can be running at the same time)
* but much of a power of tasks lies in proper usage of Task Modifiers
* that you can apply to a task.
*
* @param {function} generatorFunction the generator function backing the task.
* @returns {TaskProperty}
*/
export function task(taskFn) {
let tp = _computed(function(_propertyName) {
tp.taskFn.displayName = `${_propertyName} (task)`;
return Task.create({
fn: tp.taskFn,
context: this,
_origin: this,
_taskGroupPath: tp._taskGroupPath,
_scheduler: resolveScheduler(tp, this, TaskGroup),
_propertyName,
_debug: tp._debug,
_hasEnabledEvents: tp._hasEnabledEvents,
});
});
tp.taskFn = taskFn;
Object.setPrototypeOf(tp, TaskProperty.prototype);
return tp;
}
/**
* "Task Groups" provide a means for applying
* task modifiers to groups of tasks. Once a {@linkcode Task} is declared
* as part of a group task, modifiers like `drop()` or `restartable()`
* will no longer affect the individual `Task`. Instead those
* modifiers can be applied to the entire group.
*
* ```js
* import { task, taskGroup } from 'ember-concurrency';
*
* export default Controller.extend({
* chores: taskGroup().drop(),
*
* mowLawn: task(taskFn).group('chores'),
* doDishes: task(taskFn).group('chores'),
* changeDiapers: task(taskFn).group('chores')
* });
* ```
*
* @returns {TaskGroup}
*/
export function taskGroup(taskFn) {
let tp = _computed(function(_propertyName) {
return TaskGroup.create({
fn: tp.taskFn,
context: this,
_origin: this,
_taskGroupPath: tp._taskGroupPath,
_scheduler: resolveScheduler(tp, this, TaskGroup),
_propertyName,
});
});
tp.taskFn = taskFn;
Object.setPrototypeOf(tp, TaskGroupProperty.prototype);
return tp;
}
export {
all,
allSettled,
didCancel,
hash,
race,
timeout,
waitForQueue,
waitForEvent,
waitForProperty,
forever,
};