An informal talk given at the EmberTO meetup in June, 2017
ember-concurrency"an experience report"Reginald @raganwald Braithwaite, PagerDuty Inc.
View Slide
ember-concurrencyember-concurrency is an Ember Addon that makes it easy to writeconcise, robust, and beautiful asynchronous code.--http://ember-concurrency.com/Reginald @raganwald Braithwaite, PagerDuty Inc.
tasks and instancesaTask = task(function * () {const { geolocation, store } = this.get(...);const coords = yield geolocation.getCoords();const result = yield store.getNearbyStores(coords);this.set('result', result);});anInstance = someTask.perform();Reginald @raganwald Braithwaite, PagerDuty Inc.
problems ember-concurrencysolves, easilyReginald @raganwald Braithwaite, PagerDuty Inc.
mashing the "submit" button on an updateReginald @raganwald Braithwaite, PagerDuty Inc.
!Reginald @raganwald Braithwaite, PagerDuty Inc.
concurrency protocolstask(function * () {// ...}).drop()(ember-concurrency calls these "modifiers")Reginald @raganwald Braithwaite, PagerDuty Inc.
displaying a loading spinnerReginald @raganwald Braithwaite, PagerDuty Inc.
!this.set('isLoading', true);this.xhr = fetch(id).then(success => {this.set('isLoading', false);// ...},failure => {this.set('isLoading', false);// ...});Reginald @raganwald Braithwaite, PagerDuty Inc.
!isLoading: reads('fetchTask.isRunning')Reginald @raganwald Braithwaite, PagerDuty Inc.
using ember-concurrency to solveother problemsReginald @raganwald Braithwaite, PagerDuty Inc.
chunking updates to our apiReginald @raganwald Braithwaite, PagerDuty Inc.
progressconst chunks = _.chunk(incidents, CHUNK_SIZE);let done = 0;this.set('done', done);for (const theseIncidents of chunks) {yield this.resolve(theseIncidents);done = done + theseIncidents.length);this.set('done', done);}Reginald @raganwald Braithwaite, PagerDuty Inc.
Reginald @raganwald Braithwaite, PagerDuty Inc.
cancellationaTask.cancelAll();anInstance.cancel();Reginald @raganwald Braithwaite, PagerDuty Inc.
a bigger challengeReginald @raganwald Braithwaite, PagerDuty Inc.
In JavaScript, AJAX requestshappen concurrently.Reginald @raganwald Braithwaite, PagerDuty Inc.
⛈websocket ping: [-----------------]submit update: [-----------]Reginald @raganwald Braithwaite, PagerDuty Inc.
☀Reginald @raganwald Braithwaite, PagerDuty Inc.
sharing one tasktask(function * (promissoryThunk) {let result;yield promissoryThunk().then(value => {result = value;});return result;}).enqueue()Reginald @raganwald Braithwaite, PagerDuty Inc.
serialized resultswebsocket ping: [--------]submit update: [-------]Reginald @raganwald Braithwaite, PagerDuty Inc.
⁉Reginald @raganwald Braithwaite, PagerDuty Inc.
task groupstasks: taskGroup().enqueue(),handlePing: task(function * () {// ...}).group('tasks'),submitUpdate: task(function * () {// ...}).group('tasks')Reginald @raganwald Braithwaite, PagerDuty Inc.
ember-concurrencyconclusionReginald @raganwald Braithwaite, PagerDuty Inc.
Simple things are easy, complex things arepossible.Reginald @raganwald Braithwaite, PagerDuty Inc.