-
-
Save machty/a05231bf2878f8c874d9f3199f4be57c to your computer and use it in GitHub Desktop.
e-c status trackers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { task, timeout, all } from 'ember-concurrency'; | |
const { Controller, get, set } = Ember; | |
const TaskTracker = Ember.ObjectProxy.extend({ | |
task: null, | |
error: null | |
}); | |
function randomOutcome() { | |
if (Math.random() >= 0.5) { | |
throw new Error('fake error'); | |
} | |
} | |
export default Controller.extend({ | |
sampleData: [ | |
Ember.Object.create({id: 1, word: 'foo'}), | |
Ember.Object.create({id: 2, word: 'bar'}), | |
Ember.Object.create({id: 3, word: 'baz'}), | |
Ember.Object.create({id: 4, word: 'foobar'}), | |
Ember.Object.create({id: 5, word: 'foobaz'}), | |
Ember.Object.create({id: 6, word: 'bazfoo'}) | |
], | |
childTask: task(function * (tracker) { | |
try { | |
yield timeout(500); | |
randomOutcome(); | |
} catch (error) { | |
set(tracker, 'error', error); | |
} | |
}).maxConcurrency(2).enqueue(), | |
parentTask: task(function * () { | |
let childTask = get(this, 'childTask'); | |
let data = get(this, 'sampleData'); | |
let trackers = []; | |
let childTasks = []; | |
for (let sample of data) { | |
let tracker = TaskTracker.create({content: sample}); | |
let taskInstance = childTask.perform(tracker); | |
set(tracker, 'task', taskInstance); | |
trackers.push(tracker); | |
childTasks.push(taskInstance); | |
} | |
set(this, 'trackers', trackers); | |
set(this, 'errors', null); | |
set(this, 'status', 'Waiting for child tasks'); | |
// This is not waiting here!!! WHY?!?!?! | |
yield all(childTasks); | |
// We get here BEFORE childTasks are done! WTF?! | |
set(this, 'status', 'All child tasks are done'); | |
set(this, 'errors', trackers.mapBy('error').compact()); | |
}).drop() | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.10.5", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.6.0", | |
"ember-data": "2.6.0", | |
"ember-template-compiler": "2.6.0", | |
"ember-testing": "2.6.0" | |
}, | |
"addons": { | |
"ember-concurrency": "0.7.10" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment