Skip to content

Commit

Permalink
fix: event emitter types with local types (#864)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Jan 18, 2021
1 parent 77e8273 commit 6c41e30
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/connection-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const mergeOptions = require('merge-options')
const LatencyMonitor = require('./latency-monitor')
const retimer = require('retimer')

const { EventEmitter } = require('events')
/** @typedef {import('../types').EventEmitterFactory} Events */
/** @type Events */
const EventEmitter = require('events')

const PeerId = require('peer-id')

Expand Down
4 changes: 3 additions & 1 deletion src/connection-manager/latency-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

/* global window */
const globalThis = require('ipfs-utils/src/globalthis')
const { EventEmitter } = require('events')
/** @typedef {import('../types').EventEmitterFactory} Events */
/** @type Events */
const EventEmitter = require('events')
const VisibilityChangeEmitter = require('./visibility-change-emitter')
const debug = require('debug')('latency-monitor:LatencyMonitor')

Expand Down
4 changes: 3 additions & 1 deletion src/connection-manager/visibility-change-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/
'use strict'

const { EventEmitter } = require('events')
/** @typedef {import('../types').EventEmitterFactory} Events */
/** @type Events */
const EventEmitter = require('events')

const debug = require('debug')('latency-monitor:VisibilityChangeEmitter')

Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const debug = require('debug')
const log = Object.assign(debug('libp2p'), {
error: debug('libp2p:err')
})
const { EventEmitter } = require('events')
/** @typedef {import('./types').EventEmitterFactory} Events */
/** @type Events */
const EventEmitter = require('events')
const globalThis = require('ipfs-utils/src/globalthis')

const errCode = require('err-code')
Expand Down
4 changes: 3 additions & 1 deletion src/metrics/stats.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @ts-nocheck
'use strict'

const { EventEmitter } = require('events')
/** @typedef {import('../types').EventEmitterFactory} Events */
/** @type Events */
const EventEmitter = require('events')
const Big = require('bignumber.js')
const MovingAverage = require('moving-average')
const retimer = require('retimer')
Expand Down
4 changes: 3 additions & 1 deletion src/peer-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const errcode = require('err-code')

const { EventEmitter } = require('events')
/** @typedef {import('../types').EventEmitterFactory} Events */
/** @type Events */
const EventEmitter = require('events')
const PeerId = require('peer-id')

const AddressBook = require('./address-book')
Expand Down
19 changes: 19 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,22 @@ export type CircuitMessageProto = {
CAN_HOP: CAN_HOP
}
}

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;
}

0 comments on commit 6c41e30

Please sign in to comment.