Skip to content

Instantly share code, notes, and snippets.

@jenweber
Last active April 21, 2020 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jenweber/7765753fe8715adbdb0c7675fe7f17d7 to your computer and use it in GitHub Desktop.
Save jenweber/7765753fe8715adbdb0c7675fe7f17d7 to your computer and use it in GitHub Desktop.
ember-concurrency demo
import Ember from 'ember';
import { task, timeout } from 'ember-concurrency';
export default Ember.Component.extend({
notify: Ember.inject.service('notify'),
actions: {
startECLoop: function() {
this.get('emberConcurrencyLoop').perform();
},
notUsingECLoop: function() {
this.get('notify').info('NOT using ember-concurrency', {
classNames: ['not-using-ec']
})
setInterval(function(){
this.get('notify').info('NOT using ember-concurrency', {
classNames: ['not-using-ec']
})
}.bind(this), 3000);
},
clickEC: function() { this.get('emberConcurrencyEvent').perform()
},
clickRegular: function() {
this.get('notify').info('NOT using ember-concurrency', {
classNames: ['not-using-ec']
})
}
},
emberConcurrencyLoop: task(function * () {
while(true) {
this.get('notify').info('Yay, ember-concurrency task :)', {
classNames: ['using-ec']
})
yield timeout(3000)
}
}).drop(),
emberConcurrencyEvent: task(function * () {
this.get('notify').info('Yay, ember-concurrency task :)', {
classNames: ['using-ec']
})
yield timeout(3000)
}).drop(),
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Simple Ember Concurrency Demo',
init: function() {
this.transitionToRoute('with-concurrency');
return this._super(...arguments);
},
});
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: 'none',
rootURL: config.rootURL
});
Router.map(function() {
this.route('with-concurrency');
this.route('home');
});
export default Router;
import Ember from 'ember';
export default Ember.Route.extend({
});
import Ember from 'ember';
export default Ember.Route.extend({
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 16pt;
}
.not-using-ec {
background-color: lightsalmon;
font-weight: bold;
}
.using-ec {
background-color: lightskyblue;
font-weight: bold;
}
button {
font-size:16px;
}
<h1>{{appName}}</h1>
{{outlet}}
<br>
<br>
Try rapidly clicking each of these buttons. Ember-concurrency helps prevent a pileup of alerts.
<br />
<button class="using-ec" {{action 'clickEC'}}>Use ember-concurrency</button>
<button class="not-using-ec" {{action 'clickRegular'}}>DON'T use it</button>
<br />
<br />
<div>Now, click one or both of these once to start some loops:</div>
<button class="using-ec" {{action 'startECLoop'}}>Use ember-concurrency</button>
<button class="not-using-ec" {{action 'notUsingECLoop'}}>DON'T use it</button>
{{ember-notify closeAfter=2500}}
Are any alerts still running?
<br />
{{link-to 'Go back and see what happens' 'with-concurrency'}}
{{ember-notify closeAfter=2500}}
{{with-concurrency}}
<br />
Then, after you start a loop, see what happens when you leave this route and go to a new one:
{{link-to 'Trigger Transition' 'home'}}
{
"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.3",
"ember-notify":"5.2.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment