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

fix(isolate): do not trigger avaliable event when isolated #11

Merged
merged 1 commit into from
Dec 24, 2021
Merged
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
39 changes: 20 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,32 +605,15 @@ class ThreadPool {
trackUnmanagedFds: this.options.trackUnmanagedFds,
})

const { port1, port2 } = new MessageChannel()
const workerInfo = new WorkerInfo(worker, port1, onMessage)
if (this.startingUp) {
// There is no point in waiting for the initial set of Workers to indicate
// that they are ready, we just mark them as such from the start.
workerInfo.markAsReady()
}

const message: StartupMessage = {
filename: this.options.filename,
name: this.options.name,
port: port2,
sharedBuffer: workerInfo.sharedBuffer,
useAtomics: this.options.useAtomics,
}
worker.postMessage(message, [port2])

function onMessage(message: ResponseMessage) {
const onMessage = (message: ResponseMessage) => {
const { taskId, result } = message
// In case of success: Call the callback that was passed to `runTask`,
// remove the `TaskInfo` associated with the Worker, which marks it as
// free again.
const taskInfo = workerInfo.taskInfos.get(taskId)
workerInfo.taskInfos.delete(taskId)

pool.workers.maybeAvailable(workerInfo)
if (!this.options.isolateWorkers) pool.workers.maybeAvailable(workerInfo)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only line changed. The others are dum diff of moving a function to top

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this seems reasonable.


/* istanbul ignore if */
if (taskInfo === undefined) {
Expand All @@ -645,6 +628,24 @@ class ThreadPool {
pool._processPendingMessages()
}

const { port1, port2 } = new MessageChannel()
const workerInfo = new WorkerInfo(worker, port1, onMessage)
if (this.startingUp) {
// There is no point in waiting for the initial set of Workers to indicate
// that they are ready, we just mark them as such from the start.
workerInfo.markAsReady()
}

const message: StartupMessage = {
filename: this.options.filename,
name: this.options.name,
port: port2,
sharedBuffer: workerInfo.sharedBuffer,
useAtomics: this.options.useAtomics,
}

worker.postMessage(message, [port2])

worker.on('message', (message: ReadyMessage) => {
if (message.ready === true) {
if (workerInfo.currentUsage() === 0) {
Expand Down