Slide 1

Slide 1 text

ember-concurrency "an experience report" Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 2

Slide 2 text

ember-concurrency ember-concurrency is an Ember Addon that makes it easy to write concise, robust, and beautiful asynchronous code. --http://ember-concurrency.com/ Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 3

Slide 3 text

tasks and instances aTask = 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.

Slide 4

Slide 4 text

problems ember-concurrency solves, easily Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 5

Slide 5 text

mashing the "submit" button on an update Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 6

Slide 6 text

! Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 7

Slide 7 text

concurrency protocols task(function * () { // ... }).drop() (ember-concurrency calls these "modifiers") Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 8

Slide 8 text

! Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 9

Slide 9 text

displaying a loading spinner Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 10

Slide 10 text

! this.set('isLoading', true); this.xhr = fetch(id).then( success => { this.set('isLoading', false); // ... }, failure => { this.set('isLoading', false); // ... }); Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 11

Slide 11 text

! isLoading: reads('fetchTask.isRunning') Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 12

Slide 12 text

using ember-concurrency to solve other problems Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 13

Slide 13 text

chunking updates to our api Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 14

Slide 14 text

progress const 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.

Slide 15

Slide 15 text

Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 16

Slide 16 text

cancellation aTask.cancelAll(); anInstance.cancel(); Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 17

Slide 17 text

a bigger challenge Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 18

Slide 18 text

In JavaScript, AJAX requests happen concurrently. Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 19

Slide 19 text

Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 20

Slide 20 text

⛈ websocket ping: [-----------------] submit update: [-----------] Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 21

Slide 21 text

☀ Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 22

Slide 22 text

sharing one task task(function * (promissoryThunk) { let result; yield promissoryThunk().then(value => { result = value; }); return result; }).enqueue() Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 23

Slide 23 text

serialized results websocket ping: [--------] submit update: [-------] Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 24

Slide 24 text

⁉ Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 25

Slide 25 text

task groups tasks: taskGroup().enqueue(), handlePing: task(function * () { // ... }).group('tasks'), submitUpdate: task(function * () { // ... }).group('tasks') Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 26

Slide 26 text

ember-concurrency conclusion Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 27

Slide 27 text

Simple things are easy, complex things are possible. Reginald @raganwald Braithwaite, PagerDuty Inc.

Slide 28

Slide 28 text

Reginald @raganwald Braithwaite, PagerDuty Inc.