Skip to content

Commit

Permalink
clean up onEnd when process ends
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Mar 12, 2021
1 parent bff801c commit 698e78e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 4 additions & 8 deletions packages/jest-worker/src/Farm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export default class Farm {
// retaine args for the closure.
((
args: Array<unknown>,
resolve: null | ((value: unknown) => void),
reject: null | ((reason?: any) => void),
resolve: (value: unknown) => void,
reject: (reason?: any) => void,
) => {
const computeWorkerKey = this._computeWorkerKey;
const request: ChildMessage = [CHILD_MESSAGE_CALL, false, method, args];
Expand All @@ -94,14 +94,10 @@ export default class Farm {
const onEnd: OnEnd = (error: Error | null, result: unknown) => {
customMessageListeners.clear();
if (error) {
reject?.(error);
reject(error);
} else {
resolve?.(result);
resolve(result);
}

// Delete reference to the Promise so its result can be garbage
// collected after calling this method.
reject = resolve = null;
};

const task = {onCustomMessage, onEnd, onStart, request};
Expand Down
10 changes: 8 additions & 2 deletions packages/jest-worker/src/workers/NodeThreadsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,21 @@ export default class ExperimentalWorker implements WorkerInterface {
send(
request: ChildMessage,
onProcessStart: OnStart,
onProcessEnd: OnEnd,
onProcessEnd: OnEnd | null,
onCustomMessage: OnCustomMessage,
): void {
onProcessStart(this);
this._onProcessEnd = (...args) => {
// Clean the request to avoid sending past requests to workers that fail
// while waiting for a new request (timers, unhandled rejections...)
this._request = null;
return onProcessEnd(...args);

const res = onProcessEnd?.(...args);

// Clean up the reference so related closures can be garbage collected.
onProcessEnd = null;

return res;
};

this._onCustomMessage = (...arg) => onCustomMessage(...arg);
Expand Down

0 comments on commit 698e78e

Please sign in to comment.