-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
[Part 1] Individual Test Case Report - Support custom messages #10293
Changes from 20 commits
a513489
21d22ed
42f9a0f
544a0b8
99d1da4
7dedac3
9896107
f09d012
8ae2f42
d3499e7
f821a69
b3a4177
8b90953
7ad1c5c
403dc35
9bb4c4d
a78f4d5
feb63cf
2f261df
5666ef2
45492fa
0167abc
c562f9f
0fe27e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,10 @@ import { | |
CHILD_MESSAGE_CALL, | ||
ChildMessage, | ||
FarmOptions, | ||
OnCustomMessage, | ||
OnEnd, | ||
OnStart, | ||
PromiseWithCustomMessage, | ||
QueueChildMessage, | ||
QueueItem, | ||
WorkerInterface, | ||
|
@@ -44,41 +46,65 @@ export default class Farm { | |
} | ||
} | ||
|
||
doWork(method: string, ...args: Array<any>): Promise<unknown> { | ||
return new Promise((resolve, reject) => { | ||
const computeWorkerKey = this._computeWorkerKey; | ||
const request: ChildMessage = [CHILD_MESSAGE_CALL, false, method, args]; | ||
doWork( | ||
method: string, | ||
...args: Array<any> | ||
): PromiseWithCustomMessage<unknown> { | ||
const customMessageListeners: Set<OnCustomMessage> = new Set(); | ||
|
||
const addCustomMessageListener = (listener: OnCustomMessage) => { | ||
customMessageListeners.add(listener); | ||
return () => { | ||
// Check if the following check is required | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't need to loop, just call the |
||
customMessageListeners.forEach(customListener => { | ||
if (customListener === listener) { | ||
customMessageListeners.delete(customListener); | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
let worker: WorkerInterface | null = null; | ||
let hash: string | null = null; | ||
const onCustomMessage: OnCustomMessage = message => { | ||
customMessageListeners.forEach(listener => listener(message)); | ||
}; | ||
|
||
if (computeWorkerKey) { | ||
hash = computeWorkerKey.call(this, method, ...args); | ||
worker = hash == null ? null : this._cacheKeys[hash]; | ||
} | ||
const promise: PromiseWithCustomMessage<unknown> = new Promise( | ||
(resolve, reject) => { | ||
const computeWorkerKey = this._computeWorkerKey; | ||
const request: ChildMessage = [CHILD_MESSAGE_CALL, false, method, args]; | ||
|
||
const onStart: OnStart = (worker: WorkerInterface) => { | ||
if (hash != null) { | ||
this._cacheKeys[hash] = worker; | ||
let worker: WorkerInterface | null = null; | ||
let hash: string | null = null; | ||
|
||
if (computeWorkerKey) { | ||
hash = computeWorkerKey.call(this, method, ...args); | ||
worker = hash == null ? null : this._cacheKeys[hash]; | ||
} | ||
}; | ||
|
||
const onEnd: OnEnd = (error: Error | null, result: unknown) => { | ||
if (error) { | ||
reject(error); | ||
const onStart: OnStart = (worker: WorkerInterface) => { | ||
if (hash != null) { | ||
this._cacheKeys[hash] = worker; | ||
} | ||
}; | ||
|
||
const onEnd: OnEnd = (error: Error | null, result: unknown) => { | ||
customMessageListeners.clear(); | ||
return error ? reject(error) : resolve(result); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep |
||
}; | ||
|
||
const task = {onCustomMessage, onEnd, onStart, request}; | ||
|
||
if (worker) { | ||
this._enqueue(task, worker.getWorkerId()); | ||
} else { | ||
resolve(result); | ||
this._push(task); | ||
} | ||
}; | ||
}, | ||
); | ||
|
||
const task = {onEnd, onStart, request}; | ||
promise.UNSTABLE_onCustomMessage = addCustomMessageListener; | ||
|
||
if (worker) { | ||
this._enqueue(task, worker.getWorkerId()); | ||
} else { | ||
this._push(task); | ||
} | ||
}); | ||
return promise; | ||
} | ||
|
||
private _getNextTask(workerId: number): QueueChildMessage | null { | ||
|
@@ -114,7 +140,13 @@ export default class Farm { | |
task.request[1] = true; | ||
|
||
this._lock(workerId); | ||
this._callback(workerId, task.request, task.onStart, onEnd); | ||
this._callback( | ||
workerId, | ||
task.request, | ||
task.onStart, | ||
onEnd, | ||
task.onCustomMessage, | ||
); | ||
|
||
return this; | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,9 +11,11 @@ import Farm from './Farm'; | |||||
import type { | ||||||
FarmOptions, | ||||||
PoolExitResult, | ||||||
PromiseWithCustomMessage, | ||||||
WorkerPoolInterface, | ||||||
WorkerPoolOptions, | ||||||
} from './types'; | ||||||
import _messageParent from './workers/messageParent'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
function getExposedMethods( | ||||||
workerPath: string, | ||||||
|
@@ -146,3 +148,6 @@ export default class JestWorker { | |||||
return this._workerPool.end(); | ||||||
} | ||||||
} | ||||||
|
||||||
export type {PromiseWithCustomMessage}; | ||||||
export const messageParent = _messageParent; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okayy, I think I missed these changes which I committed at a later stage in the previous PR; when I split it. Sorry for the repeat suggestions 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries!