Last change
on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, '__esModule', {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = messageParent;
|
---|
7 |
|
---|
8 | var _types = require('../types');
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
---|
12 | *
|
---|
13 | * This source code is licensed under the MIT license found in the
|
---|
14 | * LICENSE file in the root directory of this source tree.
|
---|
15 | */
|
---|
16 | const isWorkerThread = (() => {
|
---|
17 | try {
|
---|
18 | // `Require` here to support Node v10
|
---|
19 | const {isMainThread, parentPort} = require('worker_threads');
|
---|
20 |
|
---|
21 | return !isMainThread && parentPort != null;
|
---|
22 | } catch {
|
---|
23 | return false;
|
---|
24 | }
|
---|
25 | })();
|
---|
26 |
|
---|
27 | function messageParent(message, parentProcess = process) {
|
---|
28 | if (isWorkerThread) {
|
---|
29 | // `Require` here to support Node v10
|
---|
30 | const {parentPort} = require('worker_threads'); // ! is safe due to `null` check in `isWorkerThread`
|
---|
31 |
|
---|
32 | parentPort.postMessage([_types.PARENT_MESSAGE_CUSTOM, message]);
|
---|
33 | } else if (typeof parentProcess.send === 'function') {
|
---|
34 | parentProcess.send([_types.PARENT_MESSAGE_CUSTOM, message]);
|
---|
35 | } else {
|
---|
36 | throw new Error('"messageParent" can only be used inside a worker');
|
---|
37 | }
|
---|
38 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.