Repository: puppeteer/puppeteer
File: packages/puppeteer-core/src/cdp/IsolatedWorld.ts (TypeScript)

Code before:
        this.#frameOrWorker.clearDocumentHandle();
      }
    });
    this.#context = context;
    this.#emitter.emit('context', context);
    void this.taskManager.rerunAll();
  }

  hasContext(): boolean {
    return !!this.#context;
  }

  #executionContext(): ExecutionContext|undefined {
    if (this.disposed) {
      throw new Error(
        `Execution context is not available in detached frame "${this.environment.url()}" (are you trying to evaluate?)`
      );
    }
    return this.#context;
  }

  async #waitForExecutionContext(): Promise<ExecutionContext> {
    const result = await firstValueFrom(
      fromEmitterEvent(this.#emitter, 'context').pipe(
        raceWith(
          fromEmitterEvent(this.#emitter, 'disposed').pipe(
            map(() => {
              // The message has to match the CDP message expected by the WaitTask class.
              throw new Error('Execution context was destroyed');
            })
          )
        )
      )
    );
    return result;
  }

  async evaluateHandle<
    Params extends unknown[],
    Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
  >(
    pageFunction: Func | string,
    ...args: Params
  ): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
    pageFunction = withSourcePuppeteerURLIfNone(
      this.evaluateHandle.name,
      pageFunction
    );
    let context = this.#executionContext();
    if (!context) {
      context = await this.#waitForExecutionContext();

Review comment (suggestion):
We can do something like bellow so that we don't need to check everywhere if the context already exits
```suggestion
     merge(
     fromEmitterEvent(),
     from(this.#context)
     ).pipe(
     filter(Boolean),
     raceWith()
     )
```

Code after:
        this.#frameOrWorker.clearDocumentHandle();
      }
    });
    this.#context = context;
    this.#emitter.emit('context', context);
    void this.taskManager.rerunAll();
  }

  hasContext(): boolean {
    return !!this.#context;
  }

  #executionContext(): ExecutionContext | undefined {
    if (this.disposed) {
      throw new Error(
        `Execution context is not available in detached frame "${this.environment.url()}" (are you trying to evaluate?)`
      );
    }
    return this.#context;
  }

  /**
   * Waits for the next context to be set on the isolated world.
   */
  async #waitForExecutionContext(): Promise<ExecutionContext> {
    const result = await firstValueFrom(
      fromEmitterEvent(this.#emitter, 'context').pipe(
        raceWith(
          fromEmitterEvent(this.#emitter, 'disposed').pipe(
            map(() => {
              // The message has to match the CDP message expected by the WaitTask class.
              throw new Error('Execution context was destroyed');
            })
          )
        )
      )
    );
    return result;
  }

  async evaluateHandle<
    Params extends unknown[],
    Func extends EvaluateFunc<Params> = EvaluateFunc<Params>,
  >(
    pageFunction: Func | string,
    ...args: Params
  ): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
    pageFunction = withSourcePuppeteerURLIfNone(
      this.evaluateHandle.name,
      pageFunction
    );