Last active
October 19, 2016 06:42
-
-
Save pnw/b513c408645a53a885da491651391beb to your computer and use it in GitHub Desktop.
Ember Concurrency Queue
This file contains 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} from 'ember-concurrency' | |
const { get, set, inject } = Ember | |
export default Ember.Controller.extend({ | |
queueService: inject.service(), | |
init () { | |
this._super(...arguments) | |
set(this, 'logs', []) | |
}, | |
log (type, message) { | |
get(this, 'logs').pushObject({type, message}) | |
}, | |
task1: task(function * () { | |
this.log('queue', 'task 1') | |
yield get(this, 'queueService').queue1.perform(() => { | |
this.log('start', 'task 1') | |
return timeout(2000 + 2000 * Math.random()) | |
}) | |
this.log('done', 'task 1') | |
}).enqueue(), | |
task2: task(function * () { | |
this.log('queue', 'task 2') | |
yield get(this, 'queueService').queue1.perform(() => { | |
this.log('start', 'task 2') | |
return timeout(2000 + 2000 * Math.random()) | |
}) | |
this.log('done', 'task 2') | |
}).drop(), | |
task3: task(function * () { | |
this.log('queue', 'task 3') | |
yield get(this, 'queueService').queue1.perform(() => { | |
this.log('start', 'task 3') | |
return timeout(2000 + 2000 * Math.random()) | |
}) | |
this.log('done', 'task 3') | |
}).drop(), | |
task4: task(function * () { | |
this.log('queue', 'task 4') | |
yield get(this, 'queueService').queue1.perform(() => { | |
this.log('start', 'task 4') | |
return timeout(2000 + 2000 * Math.random()) | |
}) | |
this.log('done', 'task 4') | |
}).drop(), | |
task5: task(function * () { | |
this.log('queue', 'task 5') | |
yield get(this, 'queueService').queue1.perform(() => { | |
this.log('start', 'task 5') | |
return timeout(2000 + 2000 * Math.random()) | |
}) | |
this.log('done', 'task 5') | |
}).drop(), | |
actions: { | |
queueAll () { | |
this.task1.perform() | |
this.task2.perform() | |
this.task3.perform() | |
this.task4.perform() | |
this.task5.perform() | |
}, | |
clearLogs () { | |
set(this, 'logs', []) | |
} | |
} | |
}) |
This file contains 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} from 'ember-concurrency' | |
export default Ember.Service.extend({ | |
queue1: task(function * (callback) { | |
try { | |
yield callback() | |
} catch (e) { | |
// prevent the queue from blowing up - is this necessary? | |
// TODO: can I yield the error here? | |
} | |
}).maxConcurrency(3).enqueue() | |
}) |
This file contains 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
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
.queue { | |
color: black; | |
} | |
.start { | |
color: coral; | |
} | |
.done { | |
color: blue; | |
} |
This file contains 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.1", | |
"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.1", | |
"ember-template-compiler": "2.6.0" | |
}, | |
"addons": { | |
"ember-concurrency": "latest" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment