Skip to content

Commit

Permalink
take only the subset of type event emitting we need
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Aug 31, 2018
1 parent 3fee914 commit 3b16cf8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 95 deletions.
4 changes: 3 additions & 1 deletion lighthouse-core/gather/connections/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const EventEmitter = require('events').EventEmitter;
const log = require('lighthouse-logger');
const LHError = require('../../lib/errors');

// TODO(bckenny): CommandCallback properties should be tied by command type
/**
* @typedef {LH.StrictEventEmitter<{'protocolevent': [LH.Protocol.RawEventMessage]}>} CrdpEventMessageEmitter
* @typedef {{'protocolevent': [LH.Protocol.RawEventMessage]}} ProtocolEventRecord
* @typedef {LH.Protocol.StrictEventEmitter<ProtocolEventRecord>} CrdpEventMessageEmitter
* @typedef {LH.CrdpCommands[keyof LH.CrdpCommands]} CommandInfo
* @typedef {{resolve: function(Promise<CommandInfo['returnType']>): void, method: keyof LH.CrdpCommands}} CommandCallback
*/
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const DEFAULT_NETWORK_QUIET_THRESHOLD = 5000;
const DEFAULT_CPU_QUIET_THRESHOLD = 0;

/**
* @typedef {LH.StrictEventEmitter<LH.CrdpEvents>} CrdpEventEmitter
* @typedef {LH.Protocol.StrictEventEmitter<LH.CrdpEvents>} CrdpEventEmitter
*/

class Driver {
Expand Down
13 changes: 0 additions & 13 deletions third-party/strict-event-emitter-types/LICENSE

This file was deleted.

77 changes: 0 additions & 77 deletions third-party/strict-event-emitter-types/index.d.ts

This file was deleted.

6 changes: 3 additions & 3 deletions typings/externs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import _Crdp from 'devtools-protocol/types/protocol';
import _CrdpMappings from 'devtools-protocol/types/protocol-mapping'
import _StrictEventEmitter from '../third-party/strict-event-emitter-types/index';
import { EventEmitter } from 'events';

declare global {
Expand Down Expand Up @@ -41,6 +40,9 @@ declare global {
T[P];
};

/** Filter to convince the compiler that T is spreadable. */
type IsTuple<T> = T extends any[] ? T : never;

/**
* Exclude void from T
*/
Expand All @@ -58,8 +60,6 @@ declare global {
export import Crdp = _Crdp;
export import CrdpEvents = _CrdpMappings.Events;
export import CrdpCommands = _CrdpMappings.Commands;
export type StrictEventEmitter<TEventRecord, TEmitterType = EventEmitter, TEmitRecord = TEventRecord> =
_StrictEventEmitter<TEmitterType, TEventRecord, TEmitRecord>;

interface ThrottlingSettings {
// simulation settings
Expand Down
21 changes: 21 additions & 0 deletions typings/protocol.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ declare global {
* responses.
*/
export type RawMessage = RawCommandMessage | RawEventMessage;

/**
* A more strictly-typed EventEmitter interface that checks the association
* of event name and listener payload. TEventRecord should be a record mapping
* event names to tuples that can contain zero or more items, which will
* serve as the arguments to event listener callbacks.
* Inspired by work from https://github.com/bterlson/strict-event-emitter-types.
*/
export type StrictEventEmitter<TEventRecord> = {
on<E extends keyof TEventRecord>(event: E, listener: (...args: IsTuple<TEventRecord[E]>) => void): void;

addListener<E extends keyof TEventRecord>(event: E, listener: (...args: IsTuple<TEventRecord[E]>) => void): void;

removeListener<E extends keyof TEventRecord>(event: E, listener: Function): void;

removeAllListeners<E extends keyof TEventRecord>(event?: E): void;

once<E extends keyof TEventRecord>(event: E, listener: (...args: IsTuple<TEventRecord[E]>) => void): void;

emit<E extends keyof TEventRecord>(event: E, ...request: IsTuple<TEventRecord[E]>): void;
}
}
}

Expand Down

0 comments on commit 3b16cf8

Please sign in to comment.