Skip to content

Commit

Permalink
fix: replace array -> set for customMessageListeners
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravhiremath committed Jun 19, 2020
1 parent ee481d5 commit 432bbce
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/jest-worker/src/Farm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ export default class Farm {
method: string,
...args: Array<any>
): PromiseWithCustomMessage<unknown> {
let customMessageListeners: Array<OnCustomMessage> = [];
const customMessageListeners: Set<OnCustomMessage> = new Set();

const addCustomMessageListener = (listener: OnCustomMessage) => {
customMessageListeners.push(listener);
customMessageListeners.add(listener);
return () => {
customMessageListeners = customMessageListeners.filter(
customListener => customListener !== listener,
);
// Check if the following check is required
customMessageListeners.forEach(customListener => {
if (customListener === listener) {
customMessageListeners.delete(customListener);
}
});
};
};

Expand All @@ -84,8 +87,10 @@ export default class Farm {
}
};

const onEnd: OnEnd = (error: Error | null, result: unknown) =>
error ? reject(error) : resolve(result);
const onEnd: OnEnd = (error: Error | null, result: unknown) => {
customMessageListeners.clear();
return error ? reject(error) : resolve(result);
};

const task = {onCustomMessage, onEnd, onStart, request};

Expand Down

0 comments on commit 432bbce

Please sign in to comment.