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

Code before:
        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);

    if (this.#numberOfPendingAsyncResponses++ === 0) {
      // On the next lines, the main thread will await a response from the worker thread that might
      // come AFTER the last task in the event loop has run its course and there would be nothing
      // left keeping the thread alive (and once the main thread dies, the whole process stops).
      // However we want to keep the process alive until the worker thread responds (or until the
      // event loop of the worker thread is also empty), so we ref the worker until we get all the
      // responses back.
      if (this.#worker) {
        this.#worker.ref();
      } else {
        this.#portToHooksThread.ref();

Review comment (suggestion):
nit (whitespace & sorting)

```suggestion
    }

    this.#portToHooksThread.postMessage(
      {
        __proto__: null,
        args,
        lock: this.#lock,
        method,
        port: toHooksThread,
      },
      usedTransferList,
    );
```

Code after:
        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,
    );

    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) {