Dead-simple multi-threading support for Ember.js applications.
ember-multithread
is an Ember-CLI addon. You can install it via:
ember install ember-multithread
You can create WorkerProperty
in your controllers, routes or components.
import Ember from 'ember';
import {worker} from 'ember-multithread';
export default Ember.Controller.extend({
calculatePi: worker(function(iteration) {
let pi = 0;
let n = 1;
for (let i = 0; i <= iteration; i++) {
pi = pi + (4 / n) - (4 / (n + 2));
n = n + 4;
}
return pi;
}),
actions: {
calculate() {
this.get('calculatePi').perform(500000000).then(result=> {
console.log(result);
});
}
}
});
This may cause race conditions if there are multiple works running simultaneously in one controller.
TODO
TODO
ember-multithread
can create the number of navigator.hardwardConcurrency
or 4 workers to do certain array operations.
NOTE: The worker function should not have side effects.
import Ember from 'ember';
import {worker} from 'ember-multithread';
export default Ember.Controller.extend({
mapTestWorker: worker(function(item) {
return item + 1;
}),
reduceTestWorker: worker(function(previousValue, currentValue) {
return previousValue + currentValue;
}),
filterTestWorker: worker(function(item) {
return item > 5;
}),
actions: {
mapTest() {
const array = Array(20000).fill(0);
this.get('mapTestWorker').map(array).then(result => {
console.log(result);
});
},
reduceTest() {
const array = Array(20000).fill(1);
this.get('reduceTestWorker').reduce(array).then(result => {
console.log(result);
});
},
filterTest() {
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
this.get('filterTestWorker').filter(array).then(result => {
console.log(result);
});
}
}
});
TODO
Thanks ember-concurrency and parallel.js for the inspiration.