Repository: puppeteer/puppeteer
File: packages/puppeteer-core/src/common/bidi/Browser.ts (TypeScript)

Code before:
      'browsingContext.navigationStarted',
      this.#onContextNavigation
    );
  }

  #onContextNavigation = (event: Bidi.BrowsingContext.NavigationInfo) => {
    const context = this.#connection.getBrowsingContext(event.context);
    context.url = event.url;
    const target = this.#targets.get(event.context);
    if (target) {
      this.emit(BrowserEmittedEvents.TargetChanged, target);
      target
        .browserContext()
        .emit(BrowserContextEmittedEvents.TargetChanged, target);
    }
  };

  #onContextCreated = (
    event: Bidi.BrowsingContext.ContextCreatedEvent['params']
  ) => {
    const context = new BrowsingContext(this.#connection, event);
    this.#connection.registerBrowsingContexts(context);
    // TODO: once more browsing context types are supported, this should be
    // updated to support those. Currently, all top-level contexts are treated
    // as pages.
    const browserContexts = this.browserContexts();
    const target = !context.parent
      ? new BiDiPageTarget(
          browserContexts[browserContexts.length - 1]!,
          context
        )
      : new BiDiTarget(browserContexts[browserContexts.length - 1]!, context);
    this.#targets.set(event.context, target);

    this.emit(BrowserEmittedEvents.TargetCreated, target);
    target
      .browserContext()
      .emit(BrowserContextEmittedEvents.TargetCreated, target);

    if (context.parent) {
      const topLevel = this.#connection.getTopLevelContext(context.parent);
      topLevel.emit(BrowsingContextEmittedEvents.Created, context);
    }
  };

  async #getTree(): Promise<void> {
    const {result} = await this.#connection.send('browsingContext.getTree', {});
    for (const context of result.contexts) {
      this.#onContextCreated(context);
    }
  }

Review comment (suggestion):
Let's get the BrowserContext here and use `.at(-1)` that way it shorter

Code after:

  #onContextNavigation(event: Bidi.BrowsingContext.NavigationInfo) {
    const context = this.#connection.getBrowsingContext(event.context);
    context.url = event.url;
    const target = this.#targets.get(event.context);
    if (target) {
      this.emit(BrowserEmittedEvents.TargetChanged, target);
      target
        .browserContext()
        .emit(BrowserContextEmittedEvents.TargetChanged, target);
    }
  }

  #onContextCreated(event: Bidi.BrowsingContext.ContextCreatedEvent['params']) {
    const context = new BrowsingContext(this.#connection, event);
    this.#connection.registerBrowsingContexts(context);
    // TODO: once more browsing context types are supported, this should be
    // updated to support those. Currently, all top-level contexts are treated
    // as pages.
    const browserContext = this.browserContexts().at(-1);
    if (!browserContext) {
      throw new Error('Missing browser contexts');
    }
    const target = !context.parent
      ? new BiDiPageTarget(browserContext, context)
      : new BiDiTarget(browserContext, context);
    this.#targets.set(event.context, target);

    this.emit(BrowserEmittedEvents.TargetCreated, target);
    target
      .browserContext()
      .emit(BrowserContextEmittedEvents.TargetCreated, target);

    if (context.parent) {
      const topLevel = this.#connection.getTopLevelContext(context.parent);
      topLevel.emit(BrowsingContextEmittedEvents.Created, context);
    }
  }

  async #getTree(): Promise<void> {
    const {result} = await this.#connection.send('browsingContext.getTree', {});
    for (const context of result.contexts) {
      this.#onContextCreated(context);
    }
  }

  async #onContextDestroyed(
    event: Bidi.BrowsingContext.ContextDestroyedEvent['params']
  ) {
    const context = this.#connection.getBrowsingContext(event.context);
    const topLevelContext = this.#connection.getTopLevelContext(event.context);