Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(docs): define events enum for Browser #6165

Merged
merged 1 commit into from Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions new-docs/puppeteer.browser.md
Expand Up @@ -15,6 +15,8 @@ export declare class Browser extends EventEmitter

## Remarks

The Browser class extends from Puppeteer's [EventEmitter](./puppeteer.eventemitter.md) class and will emit various events which are documented in the [BrowserEmittedEvents](./puppeteer.browseremittedevents.md) enum.

The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `Browser` class.

## Example 1
Expand Down
23 changes: 23 additions & 0 deletions new-docs/puppeteer.browseremittedevents.md
@@ -0,0 +1,23 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [puppeteer](./puppeteer.md) &gt; [BrowserEmittedEvents](./puppeteer.browseremittedevents.md)

## BrowserEmittedEvents enum

All the events a [browser instance](./puppeteer.browser.md) may emit.

<b>Signature:</b>

```typescript
export declare const enum BrowserEmittedEvents
```

## Enumeration Members

| Member | Value | Description |
| --- | --- | --- |
| Disconnected | <code>&quot;disconnected&quot;</code> | Emitted when Puppeteer gets disconnected from the Chromium instance. This might happen because of one of the following:<!-- -->- Chromium is closed or crashed<!-- -->- The [browser.disconnect](./puppeteer.browser.disconnect.md) method was called. |
| TargetChanged | <code>&quot;targetchanged&quot;</code> | Emitted when the url of a target changes. Contains a [Target](./puppeteer.target.md) instance. |
| TargetCreated | <code>&quot;targetcreated&quot;</code> | Emitted when a target is created, for example when a new page is opened by [window.open](https://developer.mozilla.org/en-US/docs/Web/API/Window/open) or by [browser.newPage](./puppeteer.browser.newpage.md)<!-- -->Contains a [Target](./puppeteer.target.md) instance. |
| TargetDestroyed | <code>&quot;targetdestroyed&quot;</code> | Emitted when a target is destroyed, for example when a page is closed. Contains a [Target](./puppeteer.target.md) instance. |

1 change: 1 addition & 0 deletions new-docs/puppeteer.md
Expand Up @@ -40,6 +40,7 @@

| Enumeration | Description |
| --- | --- |
| [BrowserEmittedEvents](./puppeteer.browseremittedevents.md) | All the events a [browser instance](./puppeteer.browser.md) may emit. |
| [PageEmittedEvents](./puppeteer.pageemittedevents.md) | All the events that a page instance may emit. |

## Interfaces
Expand Down
69 changes: 61 additions & 8 deletions src/common/Browser.ts
Expand Up @@ -38,10 +38,63 @@ export interface WaitForTargetOptions {
timeout?: number;
}

/**
* All the events a {@link Browser | browser instance} may emit.
*
* @public
*/
export const enum BrowserEmittedEvents {
/**
* Emitted when Puppeteer gets disconnected from the Chromium instance. This
* might happen because of one of the following:
*
* - Chromium is closed or crashed
*
* - The {@link Browser.disconnect | browser.disconnect } method was called.
*/
Disconnected = 'disconnected',

/**
* Emitted when the url of a target changes. Contains a {@link Target} instance.
*
* @remarks
*
* Note that this includes target changes in incognito browser contexts.
*/
TargetChanged = 'targetchanged',

/**
* Emitted when a target is created, for example when a new page is opened by
* {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/open | window.open}
* or by {@link Browser.newPage | browser.newPage}
*
* Contains a {@link Target} instance.
*
* @remarks
*
* Note that this includes target creations in incognito browser contexts.
*/
TargetCreated = 'targetcreated',
/**
* Emitted when a target is destroyed, for example when a page is closed.
* Contains a {@link Target} instance.
*
* @remarks
*
* Note that this includes target destructions in incognito browser contexts.
*/
TargetDestroyed = 'targetdestroyed',
}

/**
* A Browser is created when Puppeteer connects to a Chromium instance, either through
* {@link Puppeteer.launch} or {@link Puppeteer.connect}.
*
* @remarks
*
* The Browser class extends from Puppeteer's {@link EventEmitter} class and will
* emit various events which are documented in the {@link BrowserEmittedEvents} enum.
*
* @example
*
* An example of using a {@link Browser} to create a {@link Page}:
Expand Down Expand Up @@ -142,7 +195,7 @@ export class Browser extends EventEmitter {

this._targets = new Map();
this._connection.on(Events.Connection.Disconnected, () =>
this.emit(Events.Browser.Disconnected)
this.emit(BrowserEmittedEvents.Disconnected)
);
this._connection.on('Target.targetCreated', this._targetCreated.bind(this));
this._connection.on(
Expand Down Expand Up @@ -243,7 +296,7 @@ export class Browser extends EventEmitter {
this._targets.set(event.targetInfo.targetId, target);

if (await target._initializedPromise) {
this.emit(Events.Browser.TargetCreated, target);
this.emit(BrowserEmittedEvents.TargetCreated, target);
context.emit(Events.BrowserContext.TargetCreated, target);
}
}
Expand All @@ -254,7 +307,7 @@ export class Browser extends EventEmitter {
this._targets.delete(event.targetId);
target._closedCallback();
if (await target._initializedPromise) {
this.emit(Events.Browser.TargetDestroyed, target);
this.emit(BrowserEmittedEvents.TargetDestroyed, target);
target
.browserContext()
.emit(Events.BrowserContext.TargetDestroyed, target);
Expand All @@ -270,7 +323,7 @@ export class Browser extends EventEmitter {
const wasInitialized = target._isInitialized;
target._targetInfoChanged(event.targetInfo);
if (wasInitialized && previousURL !== target.url()) {
this.emit(Events.Browser.TargetChanged, target);
this.emit(BrowserEmittedEvents.TargetChanged, target);
target.browserContext().emit(Events.BrowserContext.TargetChanged, target);
}
}
Expand Down Expand Up @@ -361,8 +414,8 @@ export class Browser extends EventEmitter {
if (existingTarget) return existingTarget;
let resolve;
const targetPromise = new Promise<Target>((x) => (resolve = x));
this.on(Events.Browser.TargetCreated, check);
this.on(Events.Browser.TargetChanged, check);
this.on(BrowserEmittedEvents.TargetCreated, check);
this.on(BrowserEmittedEvents.TargetChanged, check);
try {
if (!timeout) return await targetPromise;
return await helper.waitWithTimeout<Target>(
Expand All @@ -371,8 +424,8 @@ export class Browser extends EventEmitter {
timeout
);
} finally {
this.removeListener(Events.Browser.TargetCreated, check);
this.removeListener(Events.Browser.TargetChanged, check);
this.removeListener(BrowserEmittedEvents.TargetCreated, check);
this.removeListener(BrowserEmittedEvents.TargetChanged, check);
}

function check(target: Target): void {
Expand Down