Repository: nodejs/node
File: lib/internal/modules/esm/hooks.js (JavaScript)

Code before:
  waitForWorker() {
    // There is one Hooks instance for each worker thread. But only one of these Hooks instances
    // has an InternalWorker. That was the Hooks instance created for the main thread.
    // It means for all Hooks instances that are not on the main thread => they are ready because they
    // delegate to the single InternalWorker anyway.
    if (!isMainThread) {
      return;
    }

    if (!this.#isReady) {
      const { kIsOnline } = require('internal/worker');
      if (!this.#worker[kIsOnline]) {
        debug('wait for signal from worker');
        AtomicsWait(this.#lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 0);
        const response = this.#worker.receiveMessageSync();
        if (response == null || response.message.status === 'exit') { return; }

        // ! This line catches initialization errors in the worker thread.
        this.#unwrapMessage(response);
      }

      this.#isReady = true;
    }
  }

  #postMessageToWorker(method, type, transferList, ...args) {
    this.waitForWorker();
    MessageChannel ??= require('internal/worker/io').MessageChannel;
    const { port1: fromHooksThread, port2: toHooksThread } = new MessageChannel();

    // Pass work to the worker.
    debug(`post ${type} message to worker`, { method, args, transferList });
    const usedTransferList = [toHooksThread];
    if (transferList) {
      ArrayPrototypePushApply(usedTransferList, transferList);
    }
    this.#portToHooksThread.postMessage(
      { __proto__: null, method, args, lock: this.#lock, port: toHooksThread }, usedTransferList);

    return fromHooksThread;
  }

  /**
   * Invoke a remote method asynchronously.
   * @param {string} method Method to invoke
   * @param {any[]} [transferList] Objects in `args` to be transferred
   * @param  {any[]} args Arguments to pass to `method`
   * @returns {Promise<any>}
   */
  async makeAsyncRequest(method, transferList, ...args) {
    const fromHooksThread = this.#postMessageToWorker(method, 'Async', transferList, ...args);

Review comment (suggestion):
```suggestion
  #postMessageToWorker(method, type, transferList, args) {
```

Code after:
    // There is one Hooks instance for each worker thread. But only one of these Hooks instances
    // has an InternalWorker. That was the Hooks instance created for the main thread.
    // It means for all Hooks instances that are not on the main thread => they are ready because they
    // delegate to the single InternalWorker anyway.
    if (!isMainThread) {
      return;
    }

    if (!this.#isReady) {
      const { kIsOnline } = require('internal/worker');
      if (!this.#worker[kIsOnline]) {
        debug('wait for signal from worker');
        AtomicsWait(this.#lock, WORKER_TO_MAIN_THREAD_NOTIFICATION, 0);
        const response = this.#worker.receiveMessageSync();
        if (response == null || response.message.status === 'exit') { return; }

        // ! This line catches initialization errors in the worker thread.
        this.#unwrapMessage(response);
      }

      this.#isReady = true;
    }
  }

  #postMessageToWorker(method, type, transferList, args) {
    this.waitForWorker();

    MessageChannel ??= require('internal/worker/io').MessageChannel;

    const {
      port1: fromHooksThread,
      port2: toHooksThread,
    } = new MessageChannel();

    // Pass work to the worker.
    debug(`post ${type} message to worker`, { method, args, transferList });
    const usedTransferList = [toHooksThread];
    if (transferList) {
      ArrayPrototypePushApply(usedTransferList, transferList);
    }

    this.#portToHooksThread.postMessage(
      {
        __proto__: null,
        args,
        lock: this.#lock,
        method,
        port: toHooksThread,
      },
      usedTransferList,
    );