Skip to content

Commit

Permalink
feat: allowDuplicateHandlers
Browse files Browse the repository at this point in the history
added options to allow duplicated handler
  • Loading branch information
simonecorsi committed Dec 17, 2021
1 parent 9dfeede commit f0c71cb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type FineOptions = {
timeout: number;
events: ProcessEvents;
unref: boolean;
allowDuplicateHandlers: boolean;
};
/**
* @param {number} timeout
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ const DEFAULT_EVENTS = [
* @prop {number} timeout
* @prop {ProcessEvents} events
* @prop {boolean} unref
* @prop {boolean} allowDuplicateHandlers
*/
const DEFAULT_OPTS = {
timeout: 2000,
events: DEFAULT_EVENTS,
unref: false,
allowDuplicateHandlers: true,
};

/**
Expand Down Expand Up @@ -65,9 +67,10 @@ function fine(callbacks = [], opts = {}) {
validateParameters(options.timeout, options.events, callbacks);

for (const event of options.events) {
if (process.listenerCount(event) > 0) {
if (!options.allowDuplicateHandlers && process.listenerCount(event) > 0) {
throw new Error(`A ${event} handler is already registered`);
}

process.once(event, () => {
const code = event.match("^SIG") ? 0 : 1;

Expand Down

0 comments on commit f0c71cb

Please sign in to comment.