Skip to content

Instantly share code, notes, and snippets.

@akatov
Created October 24, 2017 09:15
Show Gist options
  • Save akatov/c75ddf545a79b1c313d19a5354b9bcbc to your computer and use it in GitHub Desktop.
Save akatov/c75ddf545a79b1c313d19a5354b9bcbc to your computer and use it in GitHub Desktop.
ember-concurrency experiments
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';
const WAIT_HERE_FOREVER = Ember.RSVP.defer().promise;
export default Ember.Controller.extend({
count: 0,
mostRecent: null,
myTask: task(function * () {
try {
this.incrementProperty('count');
yield timeout(1000);
console.log('rest of try');
} catch (e) {
console.log('catch');
throw e;
} finally {
// finally blocks always get called,
// even when the task is being canceled
console.log('finally');
this.decrementProperty('count');
}
}),
actions: {
performTask() {
let task = this.get('myTask');
let taskInstance = task.perform();
this.set('mostRecent', taskInstance);
},
cancelAll() {
this.get('myTask').cancelAll();
},
cancelMostRecent() {
this.get('mostRecent').cancel();
},
}
});
import Ember from 'ember';
import { task, taskGroup, timeout } from 'ember-concurrency';
function * taskFn() {
try {
console.log('starting try');
console.log('yeah');
yield timeout(2000);
console.log('finishing try');
} catch(e) {
console.log('catch');
throw e;
} finally {
console.log('finally');
}
}
export default Ember.Controller.extend({
chores: taskGroup().restartable(),
mowLawn: task(taskFn).group('chores'),
doDishes: task(taskFn).group('chores'),
changeDiapers: task(taskFn).group('chores'),
tasks: Ember.computed(function() {
return [
this.get('mowLawn'),
this.get('doDishes'),
this.get('changeDiapers'),
];
}),
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('task-groups');
this.route('cancellation');
});
export default Router;
<h5>Running tasks: {{count}}</h5>
<button {{action 'performTask'}}>
Perform Task
</button>
{{#if count}}
<button {{action 'cancelAll'}}>
Cancel All
</button>
{{/if}}
{{#if mostRecent.isRunning}}
<button {{action 'cancelMostRecent'}}>
Cancel Most Recent
</button>
{{/if}}
{{link-to "cancellation" "cancellation"}}
{{link-to "task-groups" "task-groups"}}
{{#each tasks as |task|}}
<button disabled={{task.isRunning}}
onclick={{perform task}}>{{task.name}}</button>
{{/each}}
<h5>Chores group state: {{chores.state}}</h5>
<h5>
Most Recent Chore:
{{#with chores.last as |taskInstance|}}
{{taskInstance.task.name}} ({{taskInstance.state}})
{{/with}}
</h5>
{
"version": "0.12.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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1",
"ember-concurrency": "0.8.10"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment