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

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

    if (this.#numberOfPendingAsyncResponses++ === 0) {
      // On the next lines, the main thread will await a response from the worker thread that might

Review comment (suggestion):
nit (whitespace)

```suggestion
    this.waitForWorker();

    MessageChannel ??= require('internal/worker/io').MessageChannel;

    const {
      port1: fromHooksThread,
      port2: toHooksThread,
    } = new MessageChannel();
```

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

    return fromHooksThread;
  }