-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: start and stop error callback (#316)
* fix: ensure start and stop callbacks are called
- Loading branch information
Showing
4 changed files
with
62 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
const once = require('once') | ||
|
||
/** | ||
* Registers `handler` to each event in `events`. The `handler` | ||
* will only be called for the first event fired, at which point | ||
* the `handler` will be removed as a listener. | ||
* | ||
* Ensures `handler` is only called once. | ||
* | ||
* @example | ||
* // will call `callback` when `start` or `error` is emitted by `this` | ||
* emitFirst(this, ['error', 'start'], callback) | ||
* | ||
* @private | ||
* @param {EventEmitter} emitter The emitter to listen on | ||
* @param {Array<string>} events The events to listen for | ||
* @param {function(*)} handler The handler to call when an event is triggered | ||
* @returns {void} | ||
*/ | ||
function emitFirst (emitter, events, handler) { | ||
handler = once(handler) | ||
events.forEach((e) => { | ||
emitter.once(e, (...args) => { | ||
events.forEach((ev) => { | ||
emitter.removeListener(ev, handler) | ||
}) | ||
handler.apply(emitter, args) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports.emitFirst = emitFirst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters