Skip to content

Instantly share code, notes, and snippets.

@pnw
Last active October 19, 2016 06:42
Show Gist options
  • Save pnw/b513c408645a53a885da491651391beb to your computer and use it in GitHub Desktop.
Save pnw/b513c408645a53a885da491651391beb to your computer and use it in GitHub Desktop.
Ember Concurrency Queue
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', [])
}
}
})
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()
})
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
.queue {
color: black;
}
.start {
color: coral;
}
.done {
color: blue;
}
<h1>{{appName}}</h1>
<br>
<button onclick={{perform task1}}>Run Task 1</button>
{{if task1.isRunning 'Running'}}
<br>
<button onclick={{perform task2}}>Run Task 2</button>
{{if task2.isRunning 'Running'}}
<br>
<button onclick={{perform task3}}>Run Task 3</button>
{{if task3.isRunning 'Running'}}
<br>
<button onclick={{perform task4}}>Run Task 4</button>
{{if task4.isRunning 'Running'}}
<br>
<button onclick={{perform task5}}>Run Task 6</button>
{{if task5.isRunning 'Running'}}
<br>
<button {{action 'queueAll'}}>Run All</button>
<h3>Logs</h3>
<button {{action 'clearLogs'}}>Clear Logs</button>
<ol id="task-output">
{{#each logs as |logItem|}}
<li class="{{logItem.type}}">{{logItem.type}} {{logItem.message}}</li>
{{/each}}
</ol>
{
"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