Skip to content

Commit 2b9c8c4

Browse files
authored
Merge pull request #357 from chancancode/types
Add type definition based on API docs
2 parents 613dc18 + c1fb0fc commit 2b9c8c4

22 files changed

+5031
-8
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@ jobs:
5454
- env: EMBER_TRY_SCENARIO=ember-lts-2.18
5555
- env: EMBER_TRY_SCENARIO=ember-lts-3.4
5656
- env: EMBER_TRY_SCENARIO=ember-lts-3.8
57+
- env: EMBER_TRY_SCENARIO=ember-lts-3.12
58+
- env: EMBER_TRY_SCENARIO=ember-lts-3.16
5759
- env: EMBER_TRY_SCENARIO=ember-release
5860
- env: EMBER_TRY_SCENARIO=ember-beta
5961
- env: EMBER_TRY_SCENARIO=ember-canary
6062
- env: EMBER_TRY_SCENARIO=ember-default-with-jquery
63+
- env: EMBER_TRY_SCENARIO=typescript-3.7
64+
before_script: find tests/types -type f -name '*.ts' | xargs sed -i 's/@ts-expect-error/@ts-ignore/g'
65+
- env: EMBER_TRY_SCENARIO=typescript-3.9
6166

6267
script:
6368
- node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO

addon/-task-group.js

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,165 @@ import TaskStateMixin from './-task-state-mixin';
55
import { propertyModifiers } from './-property-modifiers-mixin';
66
import { gte } from 'ember-compatibility-helpers';
77

8+
/**
9+
* "Task Groups" provide a means for applying
10+
* task modifiers to groups of tasks. Once a {@linkcode Task} is declared
11+
* as part of a group task, modifiers like `drop()` or `restartable()`
12+
* will no longer affect the individual `Task`. Instead those
13+
* modifiers can be applied to the entire group.
14+
*
15+
* ```js
16+
* import { task, taskGroup } from 'ember-concurrency';
17+
*
18+
* export default Controller.extend({
19+
* chores: taskGroup().drop(),
20+
*
21+
* mowLawn: task(taskFn).group('chores'),
22+
* doDishes: task(taskFn).group('chores'),
23+
* changeDiapers: task(taskFn).group('chores')
24+
* });
25+
* ```
26+
*
27+
*
28+
* <style>
29+
* .ignore-this--this-is-here-to-hide-constructor,
30+
* #TaskGroup{ display: none }
31+
* </style>
32+
*
33+
* @class TaskGroup
34+
*/
835
export const TaskGroup = EmberObject.extend(TaskStateMixin, {
36+
/**
37+
* `true` if any current task instances are running.
38+
*
39+
* @memberof TaskGroup
40+
* @member {boolean} isRunning
41+
* @instance
42+
* @readOnly
43+
*/
44+
45+
/**
46+
* `true` if any future task instances are queued.
47+
*
48+
* @memberof TaskGroup
49+
* @member {boolean} isQueued
50+
* @instance
51+
* @readOnly
52+
*/
53+
54+
/**
55+
* `true` if the task group is not in the running or queued state.
56+
*
57+
* @memberof TaskGroup
58+
* @member {boolean} isIdle
59+
* @instance
60+
* @readOnly
61+
*/
62+
63+
/**
64+
* The current state of the task group: `"running"`, `"queued"` or `"idle"`.
65+
*
66+
* @memberof TaskGroup
67+
* @member {string} state
68+
* @instance
69+
* @readOnly
70+
*/
71+
72+
/**
73+
* The most recently started task instance.
74+
*
75+
* @memberof TaskGroup
76+
* @member {TaskInstance} last
77+
* @instance
78+
* @readOnly
79+
*/
80+
81+
/**
82+
* The most recent task instance that is currently running.
83+
*
84+
* @memberof TaskGroup
85+
* @member {TaskInstance} lastRunning
86+
* @instance
87+
* @readOnly
88+
*/
89+
90+
/**
91+
* The most recently performed task instance.
92+
*
93+
* @memberof TaskGroup
94+
* @member {TaskInstance} lastPerformed
95+
* @instance
96+
* @readOnly
97+
*/
98+
99+
/**
100+
* The most recent task instance that succeeded.
101+
*
102+
* @memberof TaskGroup
103+
* @member {TaskInstance} lastSuccessful
104+
* @instance
105+
* @readOnly
106+
*/
107+
108+
/**
109+
* The most recently completed task instance.
110+
*
111+
* @memberof TaskGroup
112+
* @member {TaskInstance} lastComplete
113+
* @instance
114+
* @readOnly
115+
*/
116+
117+
/**
118+
* The most recent task instance that errored.
119+
*
120+
* @memberof TaskGroup
121+
* @member {TaskInstance} lastErrored
122+
* @instance
123+
* @readOnly
124+
*/
125+
126+
/**
127+
* The most recently canceled task instance.
128+
*
129+
* @memberof TaskGroup
130+
* @member {TaskInstance} lastCanceled
131+
* @instance
132+
* @readOnly
133+
*/
134+
135+
/**
136+
* The most recent task instance that is incomplete.
137+
*
138+
* @memberof TaskGroup
139+
* @member {TaskInstance} lastIncomplete
140+
* @instance
141+
* @readOnly
142+
*/
143+
144+
/**
145+
* The number of times this task group has been performed.
146+
*
147+
* @memberof TaskGroup
148+
* @member {number} performCount
149+
* @instance
150+
* @readOnly
151+
*/
152+
153+
/**
154+
* Cancels all running or queued `TaskInstance`s for this task group.
155+
* If you're trying to cancel a specific TaskInstance (rather
156+
* than all of the instances running under this task group) call
157+
* `.cancel()` on the specific TaskInstance.
158+
*
159+
* @method cancelAll
160+
* @memberof TaskGroup
161+
* @param {Object} [options]
162+
* @param {string} [options.reason=.cancelAll() was explicitly called on the Task] - a descriptive reason the task group was cancelled
163+
* @param {boolean} [options.resetState] - if true, will clear the task group state (`last*` and `performCount` properties will be set to initial values)
164+
* @instance
165+
*/
166+
9167
isTaskGroup: true,
10168

11169
toString() {
@@ -17,6 +175,33 @@ export const TaskGroup = EmberObject.extend(TaskStateMixin, {
17175
isQueued: false,
18176
});
19177

178+
/**
179+
* "Task Groups" provide a means for applying
180+
* task modifiers to groups of tasks. Once a {@linkcode Task} is declared
181+
* as part of a group task, modifiers like `drop()` or `restartable()`
182+
* will no longer affect the individual `Task`. Instead those
183+
* modifiers can be applied to the entire group.
184+
*
185+
* ```js
186+
* import { task, taskGroup } from 'ember-concurrency';
187+
*
188+
* export default Controller.extend({
189+
* chores: taskGroup().drop(),
190+
*
191+
* mowLawn: task(taskFn).group('chores'),
192+
* doDishes: task(taskFn).group('chores'),
193+
* changeDiapers: task(taskFn).group('chores')
194+
* });
195+
* ```
196+
*
197+
*
198+
* <style>
199+
* .ignore-this--this-is-here-to-hide-constructor,
200+
* #TaskGroupProperty{ display: none }
201+
* </style>
202+
*
203+
* @class TaskGroupProperty
204+
*/
20205
export let TaskGroupProperty;
21206

22207
if (gte('3.10.0-alpha.1')) {
@@ -25,4 +210,88 @@ if (gte('3.10.0-alpha.1')) {
25210
TaskGroupProperty = class extends _ComputedProperty {};
26211
}
27212

213+
/**
214+
* Configures the task group to cancel old currently task
215+
* instances to make room for a new one to perform. Sets
216+
* default maxConcurrency to 1.
217+
*
218+
* [See the Live Example](/#/docs/examples/route-tasks/1)
219+
*
220+
* @method restartable
221+
* @memberof TaskGroupProperty
222+
* @instance
223+
*/
224+
225+
/**
226+
* Configures the task group to run task instances
227+
* one-at-a-time in the order they were `.perform()`ed.
228+
* Sets default maxConcurrency to 1.
229+
*
230+
* @method enqueue
231+
* @memberof TaskGroupProperty
232+
* @instance
233+
*/
234+
235+
/**
236+
* Configures the task group to immediately cancel (i.e.
237+
* drop) any task instances performed when the task group
238+
* is already running at maxConcurrency. Sets default
239+
* maxConcurrency to 1.
240+
*
241+
* @method drop
242+
* @memberof TaskGroupProperty
243+
* @instance
244+
*/
245+
246+
/**
247+
* Configures the task group to drop all but the most
248+
* recently performed {@linkcode TaskInstance }.
249+
*
250+
* @method keepLatest
251+
* @memberof TaskGroupProperty
252+
* @instance
253+
*/
254+
255+
/**
256+
* Sets the maximum number of task instances that are
257+
* allowed to run in this task group at the same time.
258+
* By default, with no task modifiers applied, this number
259+
* is Infinity (there is no limit to the number of tasks
260+
* that can run at the same time).
261+
* {@linkcode TaskGroupProperty#restartable .restartable()},
262+
* {@linkcode TaskGroupProperty#enqueue .enqueue()}, and
263+
* {@linkcode TaskGroupProperty#drop .drop()} set the
264+
* default maxConcurrency to 1, but you can override this
265+
* value to set the maximum number of concurrently running
266+
* tasks to a number greater than 1.
267+
*
268+
* [See the AJAX Throttling example](/#/docs/examples/ajax-throttling)
269+
*
270+
* The example below uses a task group with `maxConcurrency(3)`
271+
* to limit the number of concurrent AJAX requests (for anyone
272+
* using tasks in this group) to 3.
273+
*
274+
* ```js
275+
* ajax: taskGroup().maxConcurrency(3),
276+
*
277+
* doSomeAjax: task(function * (url) {
278+
* return Ember.$.getJSON(url).promise();
279+
* }).group('ajax'),
280+
*
281+
* doSomeAjax: task(function * (url) {
282+
* return Ember.$.getJSON(url).promise();
283+
* }).group('ajax'),
284+
*
285+
* elsewhere() {
286+
* this.get('doSomeAjax').perform("http://www.example.com/json");
287+
* },
288+
* ```
289+
*
290+
* @method maxConcurrency
291+
* @memberof TaskGroupProperty
292+
* @param {Number} n The maximum number of concurrently running tasks
293+
* @instance
294+
*/
295+
296+
28297
objectAssign(TaskGroupProperty.prototype, propertyModifiers);

0 commit comments

Comments
 (0)