diff --git a/src/pubsub/index.js b/src/pubsub/index.js index e4d83be1a..4fd93c13d 100644 --- a/src/pubsub/index.js +++ b/src/pubsub/index.js @@ -1,7 +1,9 @@ 'use strict' const debug = require('debug') -const { EventEmitter } = require('events') +/** @typedef {import('../types').EventEmitterFactory} Events */ +/** @type Events */ +const EventEmitter = require('events') const errcode = require('err-code') const { pipe } = require('it-pipe') diff --git a/src/pubsub/peer-streams.js b/src/pubsub/peer-streams.js index d238bb54d..feedca1c7 100644 --- a/src/pubsub/peer-streams.js +++ b/src/pubsub/peer-streams.js @@ -1,6 +1,8 @@ 'use strict' -const { EventEmitter } = require('events') +/** @typedef {import('../types').EventEmitterFactory} Events */ +/** @type Events */ +const EventEmitter = require('events') const lp = require('it-length-prefixed') diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 000000000..554dfdfa3 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,18 @@ +export interface EventEmitterFactory { + new(): EventEmitter; +} + +export interface EventEmitter { + addListener(event: string | symbol, listener: (...args: any[]) => void); + on(event: string | symbol, listener: (...args: any[]) => void); + once(event: string | symbol, listener: (...args: any[]) => void); + removeListener(event: string | symbol, listener: (...args: any[]) => void); + off(event: string | symbol, listener: (...args: any[]) => void); + removeAllListeners(event?: string | symbol); + setMaxListeners(n: number); + getMaxListeners(): number; + listeners(event: string | symbol): Function[]; // eslint-disable-line @typescript-eslint/ban-types + rawListeners(event: string | symbol): Function[]; // eslint-disable-line @typescript-eslint/ban-types + emit(event: string | symbol, ...args: any[]): boolean; + listenerCount(event: string | symbol): number; +}