messageParent.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _types() {
  7. const data = require('../types');
  8. _types = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. /**
  14. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  15. *
  16. * This source code is licensed under the MIT license found in the
  17. * LICENSE file in the root directory of this source tree.
  18. */
  19. const isWorkerThread = () => {
  20. try {
  21. // `Require` here to support Node v10
  22. const {isMainThread, parentPort} = require('worker_threads');
  23. return !isMainThread && parentPort;
  24. } catch {
  25. return false;
  26. }
  27. };
  28. const messageParent = (message, parentProcess = process) => {
  29. try {
  30. if (isWorkerThread()) {
  31. // `Require` here to support Node v10
  32. const {parentPort} = require('worker_threads');
  33. parentPort.postMessage([_types().PARENT_MESSAGE_CUSTOM, message]);
  34. } else if (typeof parentProcess.send === 'function') {
  35. parentProcess.send([_types().PARENT_MESSAGE_CUSTOM, message]);
  36. }
  37. } catch {
  38. throw new Error('"messageParent" can only be used inside a worker');
  39. }
  40. };
  41. var _default = messageParent;
  42. exports.default = _default;