Skip to content

Commit bb30566

Browse files
committed
add concateTasks in utils
1 parent 0ff36c8 commit bb30566

File tree

5 files changed

+86
-5
lines changed

5 files changed

+86
-5
lines changed

README.md

+50-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,25 @@ Send request in controllers
1414
1515
export default Controller.extend({
1616
ecajax: Ember.inject.service(),
17+
18+
slice: task(function* (beef) {
19+
console.log('slicing beef');
20+
return beef;
21+
}),
22+
23+
pack: task(function* (beef) {
24+
console.log('packing beef');
25+
}),
26+
1727
actions: {
1828
request() {
19-
let url = '/api/v1/foo';
2029
let ecajax = this.get('ecajax');
2130
2231
// every request returns a TaskInstance
23-
let promise = ecajax.request(url);
32+
let beef = ecajax.request('/api/v1/beef').catch(err => console.log('error!;));
2433
25-
promise.then(data => this.set('data', data)
26-
.catch(error => throw 'something is wrong!'));
34+
// concat the taskInstances and tasks
35+
ecajax.concat(beef, slice, pack);
2736
}
2837
}
2938
})
@@ -136,6 +145,43 @@ Get the task with a name in the service.
136145
</button>
137146
```
138147

148+
### utils
149+
- `concatTasks`
150+
151+
Concatenate Task or TaskInstance in a row.
152+
153+
```
154+
import Ember from 'ember';
155+
import { task } from 'ember-concurrency';
156+
import { concatTasks } from 'ember-concurrency-ajax/utils';
157+
158+
export default Ember.Controller.extend({
159+
ecajax: inject.service(),
160+
tasks: computed.alias('ecajax.tasks'),
161+
result: Ember.A(),
162+
concatTasks: concatTasks,
163+
164+
slice: task(function* (val) {
165+
yield console.log('slicing beef', val);
166+
return val;
167+
}),
168+
169+
pack: task(function* (val) {
170+
yield console.log('packing beef', val);
171+
}),
172+
173+
init() {
174+
this._super(...arguments);
175+
let ecajax = this.get('ecajax');
176+
177+
this.get('concatTasks').perform(
178+
ecajax.request('/beef.json'),
179+
this.get('slice'),
180+
this.get('pack')
181+
);
182+
},
183+
});
184+
```
139185

140186
[build-status-img]: https://travis-ci.org/eguitarz/ember-concurrency-ajax.svg?branch=master
141187
[build-status-link]: https://travis-ci.org/eguitarz/ember-concurrency-ajax

addon/utils.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { task } from 'ember-concurrency';
2+
import { Task } from 'ember-concurrency/-task-property';
3+
4+
let concatTasks = task(function* (...items) {
5+
let lastValue;
6+
for(let i=0; i<items.length; i++) {
7+
let task = items[i];
8+
if (task instanceof Task) {
9+
lastValue = yield task.perform(lastValue);
10+
} else {
11+
lastValue = yield task;
12+
}
13+
}
14+
});
15+
16+
export { concatTasks };

app/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from 'ember-concurrency-ajax/utils';

tests/dummy/app/ajax-test/controller.js

+18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
import Ember from 'ember';
2+
import { task } from 'ember-concurrency';
3+
import { concatTasks } from 'ember-concurrency-ajax/utils';
24

35
const { computed, Controller, inject } = Ember;
46

57
export default Controller.extend({
68
ecajax: inject.service(),
79
tasks: computed.alias('ecajax.tasks'),
810
result: Ember.A(),
11+
concatTasks: concatTasks,
12+
13+
slice: task(function* (val) {
14+
yield console.log('slicing beef', val);
15+
return val;
16+
}),
17+
18+
pack: task(function* (val) {
19+
yield console.log('packing beef', val);
20+
}),
921

1022
init() {
1123
this._super(...arguments);
1224
let ecajax = this.get('ecajax');
1325
ecajax.createTask({ name: 'enqueue', policy: 'enqueue'});
1426
ecajax.createTask({ name: 'drop', policy: 'drop'});
1527
ecajax.createTask({ name: 'restartable', policy: 'restartable', maxConcurrency: 5 });
28+
29+
this.get('concatTasks').perform(
30+
ecajax.request('/beef.json'),
31+
this.get('slice'),
32+
this.get('pack')
33+
);
1634
},
1735

1836
actions: {

tests/dummy/mirage/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default function() {
1212
this.namespace = ''; // make this `/api`, for example, if your API is namespaced
1313
this.timing = 2000; // delay for each request, automatically set to 0 during testing
1414

15-
this.get('/test.json', () => {
15+
this.get('/*.json', () => {
1616
let num = Math.random() * 100|0;
1717
return `{"foo": ${num}}`;
1818
});

0 commit comments

Comments
 (0)