Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue with Worker.prototype.send #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/relieve/workers/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,24 @@ const tasks = new Map()
* @return {Promise} resolves when every task received the message
*/
Worker.prototype.send = function(...args) {
let stack = []
const stack = []

for(let task of tasks.values()) {
for(const task of tasks.values()) {
stack.push(new Promise((resolve, reject) => {
let a = args.slice(0) //clone arguments
a.push(resolve) //adds the resolve callback
task.send.apply(task, a)
const ready = () => {
task.send.apply(task, args)
// resolve after the task has sent the message
.then(resolve)
.catch(reject)
}

// if the task is already running then run the function
if (task.running) {
ready()
} else {
// if it's not running yet then run it when the task starts
task.once('start', ready)
}
}))
}

Expand Down