| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 306c1d3 commit 7931c3b
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | const { | |
| 4 | 4 | ArrayIsArray, | |
| 5 | 5 | ObjectSetPrototypeOf, | |
| 6 | + ReflectOwnKeys, | ||
| 6 | 7 | } = primordials; | |
| 7 | 8 | ||
| 8 | 9 | const EE = require('events'); | |
@@ -93,6 +94,16 @@ Stream.prototype.pipe = function(dest, options) { | |||
| 93 | 94 | return dest; | |
| 94 | 95 | }; | |
| 95 | 96 | ||
| 97 | + Stream.prototype.eventNames = function eventNames() { | ||
| 98 | + const names = []; | ||
| 99 | + for (const key of ReflectOwnKeys(this._events)) { | ||
| 100 | + if (typeof this._events[key] === 'function' || (ArrayIsArray(this._events[key]) && this._events[key].length > 0)) { | ||
| 101 | + names.push(key); | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + return names; | ||
| 105 | + }; | ||
| 106 | + | ||
| 96 | 107 | function prependListener(emitter, event, fn) { | |
| 97 | 108 | // Sadly this is not cacheable as some libraries bundle their own | |
| 98 | 109 | // event emitter implementation with them. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,42 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const { Readable, Writable, Duplex } = require('stream'); | ||
| 6 | + | ||
| 7 | + { | ||
| 8 | + const stream = new Readable(); | ||
| 9 | + assert.strictEqual(stream.eventNames().length, 0); | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + { | ||
| 13 | + const stream = new Readable(); | ||
| 14 | + stream.on('foo', () => {}); | ||
| 15 | + stream.on('data', () => {}); | ||
| 16 | + stream.on('error', () => {}); | ||
| 17 | + assert.deepStrictEqual(stream.eventNames(), ['error', 'data', 'foo']); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + { | ||
| 21 | + const stream = new Writable(); | ||
| 22 | + assert.strictEqual(stream.eventNames().length, 0); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + { | ||
| 26 | + const stream = new Writable(); | ||
| 27 | + stream.on('foo', () => {}); | ||
| 28 | + stream.on('drain', () => {}); | ||
| 29 | + stream.on('prefinish', () => {}); | ||
| 30 | + assert.deepStrictEqual(stream.eventNames(), ['prefinish', 'drain', 'foo']); | ||
| 31 | + } | ||
| 32 | + { | ||
| 33 | + const stream = new Duplex(); | ||
| 34 | + assert.strictEqual(stream.eventNames().length, 0); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + { | ||
| 38 | + const stream = new Duplex(); | ||
| 39 | + stream.on('foo', () => {}); | ||
| 40 | + stream.on('finish', () => {}); | ||
| 41 | + assert.deepStrictEqual(stream.eventNames(), ['finish', 'foo']); | ||
| 42 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments