forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageParent.ts
34 lines (32 loc) 路 1011 Bytes
/
messageParent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {isMainThread, parentPort} from 'worker_threads';
import {PARENT_MESSAGE_CUSTOM} from '../types';
import {withoutCircularRefs} from './withoutCircularRefs';
export default function messageParent(
message: unknown,
parentProcess = process,
): void {
if (!isMainThread && parentPort != null) {
parentPort.postMessage([PARENT_MESSAGE_CUSTOM, message]);
} else if (typeof parentProcess.send === 'function') {
try {
parentProcess.send([PARENT_MESSAGE_CUSTOM, message]);
} catch (e: any) {
if (/circular structure/.test(e?.message)) {
parentProcess.send([
PARENT_MESSAGE_CUSTOM,
withoutCircularRefs(message),
]);
} else {
throw e;
}
}
} else {
throw new Error('"messageParent" can only be used inside a worker');
}
}