Skip to content

Commit 6cba1e6

Browse files
committed
Rename to prependListener/prependOnceListener
1 parent 6e49e43 commit 6cba1e6

File tree

4 files changed

+63
-61
lines changed

4 files changed

+63
-61
lines changed

doc/api/events.markdown

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -314,42 +314,6 @@ console.log(myErr.eventNames());
314314
// Prints ['foo', 'bar', Symbol('symbol')]
315315
```
316316

317-
### emitter.firstOn(eventName, listener)
318-
319-
* `eventName` {string|Symbol} The name of the event.
320-
* `listener` {Function} The callback function
321-
322-
Adds the `listener` function to the *beginning* of the listeners array for the
323-
event named `eventName`. No checks are made to see if the `listener` has
324-
already been added. Multiple calls passing the same combination of `eventName`
325-
and `listener` will result in the `listener` being added, and called, multiple
326-
times.
327-
328-
```js
329-
server.firstOn('connection', (stream) => {
330-
console.log('someone connected!');
331-
});
332-
```
333-
334-
Returns a reference to the `EventEmitter` so calls can be chained.
335-
336-
### emitter.firstOnce(eventName, listener)
337-
338-
* `eventName` {string|Symbol} The name of the event.
339-
* `listener` {Function} The callback function
340-
341-
Adds a **one time** `listener` function for the event named `eventName` to the
342-
beginning of the listeners array. This listener is invoked only the next time
343-
`eventName` is triggered, after which it is removed.
344-
345-
```js
346-
server.firstOnce('connection', (stream) => {
347-
console.log('Ah, we have our first user!');
348-
});
349-
```
350-
351-
Returns a reference to the `EventEmitter` so calls can be chained.
352-
353317
### emitter.getMaxListeners()
354318

355319
Returns the current max listener value for the `EventEmitter` which is either
@@ -394,13 +358,13 @@ server.on('connection', (stream) => {
394358
Returns a reference to the `EventEmitter` so calls can be chained.
395359

396360
By default, event listeners are invoked in the order they are added. The
397-
`emitter.firstOn()` method can be used as an alternative to add the event
398-
listener to the beginning of the listeners array.
361+
`emitter.prependListener()` method can be used as an alternative to add the
362+
event listener to the beginning of the listeners array.
399363

400364
```js
401365
const myEE = new EventEmitter();
402366
myEE.on('foo', () => console.log('a'));
403-
myEE.firstOn('foo', () => console.log('b'));
367+
myEE.prependListener('foo', () => console.log('b'));
404368
myEE.emit('foo');
405369
// Prints:
406370
// b
@@ -425,19 +389,55 @@ server.once('connection', (stream) => {
425389
Returns a reference to the `EventEmitter` so calls can be chained.
426390

427391
By default, event listeners are invoked in the order they are added. The
428-
`emitter.firstOnce()` method can be used as an alternative to add the event
429-
listener to the beginning of the listeners array.
392+
`emitter.prependOnceListener()` method can be used as an alternative to add the
393+
event listener to the beginning of the listeners array.
430394

431395
```js
432396
const myEE = new EventEmitter();
433397
myEE.once('foo', () => console.log('a'));
434-
myEE.firstOnce('foo', () => console.log('b'));
398+
myEE.prependOnceListener('foo', () => console.log('b'));
435399
myEE.emit('foo');
436400
// Prints:
437401
// b
438402
// a
439403
```
440404

405+
### emitter.prependListener(eventName, listener)
406+
407+
* `eventName` {string|Symbol} The name of the event.
408+
* `listener` {Function} The callback function
409+
410+
Adds the `listener` function to the *beginning* of the listeners array for the
411+
event named `eventName`. No checks are made to see if the `listener` has
412+
already been added. Multiple calls passing the same combination of `eventName`
413+
and `listener` will result in the `listener` being added, and called, multiple
414+
times.
415+
416+
```js
417+
server.prependListener('connection', (stream) => {
418+
console.log('someone connected!');
419+
});
420+
```
421+
422+
Returns a reference to the `EventEmitter` so calls can be chained.
423+
424+
### emitter.prependOnceListener(eventName, listener)
425+
426+
* `eventName` {string|Symbol} The name of the event.
427+
* `listener` {Function} The callback function
428+
429+
Adds a **one time** `listener` function for the event named `eventName` to the
430+
beginning of the listeners array. This listener is invoked only the next time
431+
`eventName` is triggered, after which it is removed.
432+
433+
```js
434+
server.prependOnceListener('connection', (stream) => {
435+
console.log('Ah, we have our first user!');
436+
});
437+
```
438+
439+
Returns a reference to the `EventEmitter` so calls can be chained.
440+
441441
### emitter.removeAllListeners([eventName])
442442

443443
Removes all listeners, or those of the specified `eventName`.

lib/_stream_readable.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ var StringDecoder;
1212

1313
util.inherits(Readable, Stream);
1414

15-
const has_firston = typeof EE.prototype.firstOn === 'function';
15+
const has_firston = typeof EE.prototype.prependListener === 'function';
1616

17-
function firstOn(emitter, event, fn) {
17+
function prependListener(emitter, event, fn) {
1818
if (has_firston)
19-
return emitter.firstOn(event, fn);
19+
return emitter.prependListener(event, fn);
2020

2121
// This is a brutally ugly hack to make sure that our error handler
2222
// is attached before any userland ones. NEVER DO THIS. This is here
2323
// only because this code needs to continue to work with older versions
24-
// of Node.js that do not include the firstOn() method. The goal is to
25-
// eventually remove this hack.
24+
// of Node.js that do not include the prependListener() method. The goal
25+
// is to eventually remove this hack.
2626
if (!emitter._events || !emitter._events[event])
2727
emitter.on(event, fn);
2828
else if (Array.isArray(emitter._events[event]))
@@ -579,7 +579,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
579579
}
580580

581581
// Make sure our error handler is attached before userland ones.
582-
firstOn(dest, 'error', onerror);
582+
prependListener(dest, 'error', onerror);
583583

584584
// Both close and finish should trigger unpipe, but only once.
585585
function onclose() {

lib/events.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,10 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {
272272

273273
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
274274

275-
EventEmitter.prototype.firstOn = function firstOn(type, listener) {
276-
return _addListener(this, type, listener, true);
277-
};
275+
EventEmitter.prototype.prependListener =
276+
function prependListener(type, listener) {
277+
return _addListener(this, type, listener, true);
278+
};
278279

279280
function _onceWrap(target, type, listener) {
280281
var fired = false;
@@ -296,12 +297,13 @@ EventEmitter.prototype.once = function once(type, listener) {
296297
return this;
297298
};
298299

299-
EventEmitter.prototype.firstOnce = function firstOnce(type, listener) {
300-
if (typeof listener !== 'function')
301-
throw new TypeError('"listener" argument must be a function');
302-
this.firstOn(type, _onceWrap(this, type, listener));
303-
return this;
304-
};
300+
EventEmitter.prototype.prependOnceListener =
301+
function prependOnceListener(type, listener) {
302+
if (typeof listener !== 'function')
303+
throw new TypeError('"listener" argument must be a function');
304+
this.prependListener(type, _onceWrap(this, type, listener));
305+
return this;
306+
};
305307

306308
// emits a 'removeListener' event iff the listener was removed
307309
EventEmitter.prototype.removeListener =

test/parallel/test-event-emitter-prepend.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ var m = 0;
1010
myEE.on('foo', common.mustCall(() => assert.equal(m, 2)));
1111

1212
// This one comes second.
13-
myEE.firstOn('foo', common.mustCall(() => assert.equal(m++, 1)));
13+
myEE.prependListener('foo', common.mustCall(() => assert.equal(m++, 1)));
1414

1515
// This one comes first.
16-
myEE.firstOnce('foo', common.mustCall(() => assert.equal(m++, 0)));
16+
myEE.prependOnceListener('foo', common.mustCall(() => assert.equal(m++, 0)));
1717

1818
myEE.emit('foo');
1919

2020

21-
// Test fail-back if firstOn is undefined
21+
// Test fail-back if prependListener is undefined
2222
const stream = require('stream');
2323
const util = require('util');
2424

25-
delete EventEmitter.prototype.firstOn;
25+
delete EventEmitter.prototype.prependListener;
2626

2727
function Writable() {
2828
this.writable = true;

0 commit comments

Comments
 (0)