Skip to content

Instantly share code, notes, and snippets.

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 nightire/2f8dfcc0308d7304fc7fadef36e7f77b to your computer and use it in GitHub Desktop.
Save nightire/2f8dfcc0308d7304fc7fadef36e7f77b to your computer and use it in GitHub Desktop.
Event stream based task
import Ember from 'ember';
import { task, waitForEvent } from 'ember-concurrency';
const { Evented, Controller } = Ember;
export default Controller.extend(Evented, {
myTask: task(function* () {
while (true) {
let payload = yield waitForEvent(this, 'message');
this.get('messages').pushObject(payload);
}
}),
init() {
this._super(...arguments);
this.set('messages', []);
this.get('myTask').perform();
setInterval(() => {
let randomLetters = [1, 2, 3, 4]
.map(() => String.fromCharCode(Math.floor(Math.random() * 26) + 65))
.join('');
this.trigger('message', randomLetters);
}, 1000);
}
});
<ul>
{{#each messages as |message|}}
<li>{{message}}</li>
{{else}}
<li>No messages</li>
{{/each}}
</ul>
{
"version": "0.15.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "3.2.0",
"ember-concurrency": "0.8.19"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment