Skip to content

Commit

Permalink
chore: Revert Mitt due to breaking changes (#5952)
Browse files Browse the repository at this point in the history
Replacing the Node EventEmitter with Mitt caused more problems than
anticipated for end users due to the API differences and the amount of
people who relied on the EventEmitter API. In hindsight this clearly
should have been explored more and then released as a breaking v4.

This commit rolls us back to the built in Node EventEmitter library
which we can release to get everyone back on stable builds. We can then
consider our approach to migrating to Mitt and when we do do that we can
release it as a breaking change and properly document the migration
strategy and approach.
  • Loading branch information
jackfranklin committed Jun 2, 2020
1 parent 81e3248 commit 309d811
Show file tree
Hide file tree
Showing 16 changed files with 23 additions and 202 deletions.
37 changes: 0 additions & 37 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,6 @@
* [coverage.stopCSSCoverage()](#coveragestopcsscoverage)
* [coverage.stopJSCoverage()](#coveragestopjscoverage)
- [class: TimeoutError](#class-timeouterror)
- [class: EventEmitter](#class-eventemitter)
* [eventEmitter.emit(event, [eventData])](#eventemitteremitevent-eventdata)
* [eventEmitter.listenerCount(event)](#eventemitterlistenercountevent)
* [eventEmitter.off(event, handler)](#eventemitteroffevent-handler)
* [eventEmitter.on(event, handler)](#eventemitteronevent-handler)
* [eventEmitter.once(event, handler)](#eventemitteronceevent-handler)
* [eventEmitter.removeListener(event, handler)](#eventemitterremovelistenerevent-handler)
<!-- GEN:stop -->

### Overview
Expand Down Expand Up @@ -3942,35 +3935,6 @@ reported.

TimeoutError is emitted whenever certain operations are terminated due to timeout, e.g. [page.waitForSelector(selector[, options])](#pagewaitforselectorselector-options) or [puppeteer.launch([options])](#puppeteerlaunchoptions).

### class: EventEmitter

A small EventEmitter class backed by [Mitt](https://github.com/developit/mitt/).

#### eventEmitter.emit(event, [eventData])
- `event` <[string]|[symbol]> the event to trigger.
- `eventData` <[Object]> additional data to send with the event.

#### eventEmitter.listenerCount(event)
- `event` <[string]|[symbol]> the event to check for listeners.
- returns: <[number]> the number of listeners for the given event.

#### eventEmitter.off(event, handler)
- `event` <[string]|[symbol]> the event to remove the handler from.
- `handler` <[Function]> the event listener that will be removed.

#### eventEmitter.on(event, handler)
- `event` <[string]|[symbol]> the event to add the handler to.
- `handler` <[Function]> the event listener that will be added.

#### eventEmitter.once(event, handler)
- `event` <[string]|[symbol]> the event to add the handler to.
- `handler` <[Function]> the event listener that will be added.

#### eventEmitter.removeListener(event, handler)
- `event` <[string]|[symbol]> the event to remove the handler from.
- `handler` <[Function]> the event listener that will be removed.

This method is identical to `off` and maintained for compatibility with Node's EventEmitter. We recommend using `off` by default.


[AXNode]: #accessibilitysnapshotoptions "AXNode"
Expand Down Expand Up @@ -4020,5 +3984,4 @@ This method is identical to `off` and maintained for compatibility with Node's E
[selector]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors "selector"
[stream.Readable]: https://nodejs.org/api/stream.html#stream_class_stream_readable "stream.Readable"
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String"
[symbol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type "Symbol"
[xpath]: https://developer.mozilla.org/en-US/docs/Web/XPath "xpath"
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"extract-zip": "^2.0.0",
"https-proxy-agent": "^4.0.0",
"mime": "^2.0.3",
"mitt": "^2.0.1",
"progress": "^2.0.1",
"proxy-from-env": "^1.0.0",
"rimraf": "^3.0.2",
Expand Down
2 changes: 1 addition & 1 deletion src/Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { helper, assert } from './helper';
import { Target } from './Target';
import { EventEmitter } from './EventEmitter';
import * as EventEmitter from 'events';
import { Events } from './Events';
import Protocol from './protocol';
import { Connection } from './Connection';
Expand Down
8 changes: 4 additions & 4 deletions src/BrowserFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import * as childProcess from 'child_process';
import * as https from 'https';
import * as http from 'http';

import extractZip from 'extract-zip';
import debug from 'debug';
import removeRecursive from 'rimraf';
import * as extractZip from 'extract-zip';
import * as debug from 'debug';
import * as removeRecursive from 'rimraf';
import * as URL from 'url';
import ProxyAgent from 'https-proxy-agent';
import * as ProxyAgent from 'https-proxy-agent';
import { getProxyForUrl } from 'proxy-from-env';

import { helper, assert } from './helper';
Expand Down
4 changes: 2 additions & 2 deletions src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
import { assert } from './helper';
import { Events } from './Events';
import debug from 'debug';
import * as debug from 'debug';
const debugProtocol = debug('puppeteer:protocol');

import Protocol from './protocol';
import type { ConnectionTransport } from './ConnectionTransport';
import { EventEmitter } from './EventEmitter';
import * as EventEmitter from 'events';

interface ConnectionCallback {
resolve: Function;
Expand Down
61 changes: 0 additions & 61 deletions src/EventEmitter.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { EventEmitter } from './EventEmitter';
import * as EventEmitter from 'events';
import { helper, assert, debugError } from './helper';
import { Events } from './Events';
import { ExecutionContext, EVALUATION_SCRIPT_URL } from './ExecutionContext';
Expand Down
2 changes: 1 addition & 1 deletion src/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { EventEmitter } from './EventEmitter';
import * as EventEmitter from 'events';
import { helper, assert, debugError } from './helper';
import Protocol from './protocol';
import { Events } from './Events';
Expand Down
2 changes: 1 addition & 1 deletion src/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import * as fs from 'fs';
import { EventEmitter } from './EventEmitter';
import * as EventEmitter from 'events';
import * as mime from 'mime';
import { Events } from './Events';
import { Connection, CDPSession } from './Connection';
Expand Down
2 changes: 1 addition & 1 deletion src/WebSocketTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import NodeWebSocket from 'ws';
import * as NodeWebSocket from 'ws';
import type { ConnectionTransport } from './ConnectionTransport';

export class WebSocketTransport implements ConnectionTransport {
Expand Down
2 changes: 1 addition & 1 deletion src/WebWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { EventEmitter } from './EventEmitter';
import { EventEmitter } from 'events';
import { debugError } from './helper';
import { ExecutionContext } from './ExecutionContext';
import { JSHandle } from './JSHandle';
Expand Down
1 change: 0 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module.exports = {
Dialog: require('./Dialog').Dialog,
ElementHandle: require('./JSHandle').ElementHandle,
ExecutionContext: require('./ExecutionContext').ExecutionContext,
EventEmitter: require('./EventEmitter').EventEmitter,
FileChooser: require('./FileChooser').FileChooser,
Frame: require('./FrameManager').Frame,
JSHandle: require('./JSHandle').JSHandle,
Expand Down
11 changes: 5 additions & 6 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
* limitations under the License.
*/
import { TimeoutError } from './Errors';
import debug from 'debug';
import * as debug from 'debug';
import * as fs from 'fs';
import { CDPSession } from './Connection';
import { promisify } from 'util';
import Protocol from './protocol';
import type { CommonEventEmitter } from './EventEmitter';

const openAsync = promisify(fs.open);
const writeAsync = promisify(fs.write);
Expand Down Expand Up @@ -132,13 +131,13 @@ function installAsyncStackHooks(classType: AnyClass): void {
}

export interface PuppeteerEventListener {
emitter: CommonEventEmitter;
emitter: NodeJS.EventEmitter;
eventName: string | symbol;
handler: (...args: any[]) => void;
}

function addEventListener(
emitter: CommonEventEmitter,
emitter: NodeJS.EventEmitter,
eventName: string | symbol,
handler: (...args: any[]) => void
): PuppeteerEventListener {
Expand All @@ -148,7 +147,7 @@ function addEventListener(

function removeEventListeners(
listeners: Array<{
emitter: CommonEventEmitter;
emitter: NodeJS.EventEmitter;
eventName: string | symbol;
handler: (...args: any[]) => void;
}>
Expand All @@ -167,7 +166,7 @@ function isNumber(obj: unknown): obj is number {
}

async function waitForEvent<T extends any>(
emitter: CommonEventEmitter,
emitter: NodeJS.EventEmitter,
eventName: string | symbol,
predicate: (event: T) => boolean,
timeout: number,
Expand Down
4 changes: 2 additions & 2 deletions src/launcher/BrowserRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

import debug from 'debug';
import * as debug from 'debug';

import removeFolder from 'rimraf';
import * as removeFolder from 'rimraf';
import * as childProcess from 'child_process';
import { helper, assert, debugError } from '../helper';
import type { LaunchOptions } from './LaunchOptions';
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"outDir": "./lib",
"target": "ESNext",
"moduleResolution": "node",
"module": "CommonJS",
"esModuleInterop": true
"module": "CommonJS"
},
"include": [
"src"
Expand Down
83 changes: 3 additions & 80 deletions utils/doclint/check_public_api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,31 +180,6 @@ const expectedNonExistingMethods = new Map([
['Page', new Set(['emulateMedia'])],
]);

/* Methods that are defined in code but are not documented */
const expectedNotFoundMethods = new Map([
/* all the methods from our EventEmitter that we don't document for each subclass */
[
'Browser',
new Set(['emit', 'listenerCount', 'off', 'on', 'once', 'removeListener']),
],
[
'BrowserContext',
new Set(['emit', 'listenerCount', 'off', 'on', 'once', 'removeListener']),
],
[
'CDPSession',
new Set(['emit', 'listenerCount', 'off', 'on', 'once', 'removeListener']),
],
[
'Page',
new Set(['emit', 'listenerCount', 'off', 'on', 'once', 'removeListener']),
],
[
'WebWorker',
new Set(['emit', 'listenerCount', 'off', 'on', 'once', 'removeListener']),
],
]);

/**
* @param {!Documentation} actual
* @param {!Documentation} expected
Expand All @@ -230,24 +205,14 @@ function compareDocumentations(actual, expected) {
const methodDiff = diff(actualMethods, expectedMethods);

for (const methodName of methodDiff.extra) {
const nonExistingMethodsForClass = expectedNonExistingMethods.get(
className
);
if (
nonExistingMethodsForClass &&
nonExistingMethodsForClass.has(methodName)
)
const missingMethodsForClass = expectedNonExistingMethods.get(className);
if (missingMethodsForClass && missingMethodsForClass.has(methodName))
continue;

errors.push(`Non-existing method found: ${className}.${methodName}()`);
}

for (const methodName of methodDiff.missing) {
const missingMethodsForClass = expectedNotFoundMethods.get(className);
if (missingMethodsForClass && missingMethodsForClass.has(methodName))
continue;
for (const methodName of methodDiff.missing)
errors.push(`Method not found: ${className}.${methodName}()`);
}

for (const methodName of methodDiff.equal) {
const actualMethod = actualClass.methods.get(methodName);
Expand Down Expand Up @@ -647,48 +612,6 @@ function compareDocumentations(actual, expected) {
expectedName: 'VisionDeficiency',
},
],
[
'Method EventEmitter.emit() event',
{
actualName: 'string|symbol',
expectedName: 'Object',
},
],
[
'Method EventEmitter.listenerCount() event',
{
actualName: 'string|symbol',
expectedName: 'Object',
},
],
[
'Method EventEmitter.off() event',
{
actualName: 'string|symbol',
expectedName: 'Object',
},
],
[
'Method EventEmitter.on() event',
{
actualName: 'string|symbol',
expectedName: 'Object',
},
],
[
'Method EventEmitter.once() event',
{
actualName: 'string|symbol',
expectedName: 'Object',
},
],
[
'Method EventEmitter.removeListener() event',
{
actualName: 'string|symbol',
expectedName: 'Object',
},
],
]);

const expectedForSource = expectedNamingMismatches.get(source);
Expand Down

0 comments on commit 309d811

Please sign in to comment.