Last active
October 11, 2019 15:10
-
-
Save iezer/3361c1e38666f6ba697953b333b54457 to your computer and use it in GitHub Desktop.
Ember Concurrency Composable Tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@ember/component' | |
import { inject as service } from '@ember/service'; | |
import { alias } from '@ember/object/computed'; | |
export default Component.extend({ | |
flashMessages: service(), | |
message: alias('flashMessages.message') | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import progressTask from '../tasks/progress'; | |
import { inject as service } from '@ember/service'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
message: null, | |
loading: service(), | |
successful() { | |
return Ember.RSVP.resolve('success!'); | |
}, | |
failure() { | |
return Ember.RSVP.reject({ message: 'failed' }); | |
}, | |
doSuccess: progressTask(function*() { | |
this.set('message', null); | |
const response = yield this.successful(); | |
this.set('message', response) | |
}).drop(), | |
doFail: progressTask(function*() { | |
this.set('message', null); | |
const response = yield this.failure(); | |
this.set('message', response) | |
}).drop() | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { inject as service } from '@ember/service'; | |
function pause(ms) { | |
return new Ember.RSVP.Promise((resolve) => setTimeout(() => resolve(), ms)); | |
} | |
// Implementation without a task | |
export default Ember.Controller.extend({ | |
message: null, | |
flashMessages: service(), | |
loading: service(), | |
doApiCall(promise) { | |
const { flashMessages, loading } = this; | |
this.set('message', null); | |
loading.start(); | |
pause(500) | |
.then(() => { return promise; }) | |
.then(result => { | |
if(this.isDestroyed) { return; } | |
this.set('message', response); | |
}) | |
.catch((e) => { | |
if(this.isDestroyed) { return; } | |
const errorMessage = | |
(e.errors && e.errors.mapBy('detail')) || e.message || 'There was an error'; | |
flashMessages.danger(errorMessage); | |
}) | |
.finally(() => { | |
if(this.isDestroyed) { return; } | |
loading.stop(); | |
}); | |
}, | |
successful() { | |
return Ember.RSVP.resolve('success!'); | |
}, | |
failure() { | |
return Ember.RSVP.reject({ message: 'failed' }); | |
}, | |
actions: { | |
doSuccess() { | |
return this.doApiCall(this.successful()); | |
}, | |
doFail() { | |
return this.doApiCall(this.failure()); | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { later } from '@ember/runloop'; | |
// https://github.com/poteto/ember-cli-flash | |
export default Ember.Service.extend({ | |
message: null, | |
danger(msg) { | |
this.set('message', msg); | |
later(() => this.set('message', null), 1500); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
// http://ricostacruz.com/nprogress/ | |
export default Ember.Service.extend({ | |
isLoading: false, | |
start() { | |
this.set('isLoading', true); | |
}, | |
stop() { | |
this.set('isLoading', false); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { task, timeout } from 'ember-concurrency'; | |
import { getOwner } from '@ember/application'; | |
/* | |
Task wrapper to extract away parsing error messages and showing as flash message. | |
See https://github.com/machty/ember-concurrency/pull/117 | |
*/ | |
export default function errorTask(_definedTaskFn) { | |
let taskProperty = task(_definedTaskFn).drop(); | |
let originalTaskFn = taskProperty.taskFn; | |
taskProperty.taskFn = function*(...args) { | |
const owner = getOwner(this); | |
const flashMessages = owner.lookup('service:flashMessages'); | |
flashMessages.set('message', null); | |
const loading = owner.lookup('service:loading'); | |
loading.start(); | |
try { | |
yield timeout(1000); | |
return yield* originalTaskFn.apply(this, args); | |
} catch (e) { | |
const errorMessage = | |
(e.errors && e.errors.mapBy('detail')) || e.message || 'There was an error'; | |
flashMessages.danger(errorMessage); | |
} finally { | |
loading.stop(); | |
} | |
}; | |
return taskProperty; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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.21" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment