(type: string | number, listener: Listener, prepend = false)
| 54 | } |
| 55 | |
| 56 | on(type: string | number, listener: Listener, prepend = false): this { |
| 57 | let existing = this.events[type] |
| 58 | if (existing === undefined) { |
| 59 | // Optimize the case of one listener. Don't need the extra array object. |
| 60 | this.events[type] = listener |
| 61 | ++this.eventsCount |
| 62 | } else { |
| 63 | if (typeof existing === 'function') { |
| 64 | // Adding the second element, need to change to array. |
| 65 | existing = this.events[type] = prepend ? [listener, existing] : [existing, listener] |
| 66 | // If we've already got an array, just append. |
| 67 | } else if (prepend) { |
| 68 | existing.unshift(listener) |
| 69 | } else { |
| 70 | existing.push(listener) |
| 71 | } |
| 72 | |
| 73 | // Check for listener leak |
| 74 | const maxCount = this.getMaxListeners() |
| 75 | if (maxCount > 0 && existing.length > maxCount) { |
| 76 | // No error code for this since it is a Warning |
| 77 | // eslint-disable-next-line no-restricted-syntax |
| 78 | const e = new Error( |
| 79 | 'Possible EventEmitter memory leak detected. ' + |
| 80 | existing.length + |
| 81 | ' ' + |
| 82 | String(type) + |
| 83 | ' listeners ' + |
| 84 | 'added. Use emitter.setMaxListeners() to ' + |
| 85 | 'increase limit', |
| 86 | ) |
| 87 | console.warn(e) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return this |
| 92 | } |
| 93 | |
| 94 | off(type: string | number, listener: Listener): this { |
| 95 | const list = this.events[type] |
no test coverage detected