generated from ryansonshine/typescript-npm-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use named export instead of default export
1 parent
96c3de9
commit f598fa7
Showing
4 changed files
with
52 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export type EventCallback<T = void> = (...args: Array<any>) => T; | ||
export type Subscriptions = Map< | ||
string, | ||
Array<{ id: symbol; callback: EventCallback }> | ||
>; | ||
export type Unsubscribe = { unsubscribe: () => void }; | ||
|
||
export class EventEmitter { | ||
subscriptions: Subscriptions; | ||
|
||
constructor() { | ||
this.subscriptions = new Map(); | ||
} | ||
|
||
/** | ||
* @param {string} eventName | ||
* @param {EventCallback} callback | ||
* @return {Unsubscribe} | ||
*/ | ||
subscribe(eventName: string, callback: EventCallback): Unsubscribe { | ||
const id = Symbol(callback.toString()); | ||
this.subscriptions.set( | ||
eventName, | ||
this.subscriptions.has(eventName) | ||
? [...(this.subscriptions.get(eventName) || []), { id, callback }] | ||
: [{ id, callback }] | ||
); | ||
|
||
return { | ||
unsubscribe: () => | ||
this.subscriptions.set( | ||
eventName, | ||
(this.subscriptions.get(eventName) || []).filter( | ||
({ id: subscriptionId }) => subscriptionId !== id | ||
) | ||
), | ||
}; | ||
} | ||
|
||
/** | ||
* @param {string} eventName | ||
* @param {Array<any>} args | ||
*/ | ||
emit(eventName: string, ...args: Array<any>): void { | ||
(this.subscriptions.get(eventName) || []).forEach(({ callback }) => | ||
callback(...args) | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,49 +1 @@ | ||
export type EventCallback<T = void> = (...args: Array<any>) => T; | ||
export type Subscriptions = Map< | ||
string, | ||
Array<{ id: symbol; callback: EventCallback }> | ||
>; | ||
export type Unsubscribe = { unsubscribe: () => void }; | ||
|
||
export default class EventEmitter { | ||
subscriptions: Subscriptions; | ||
|
||
constructor() { | ||
this.subscriptions = new Map(); | ||
} | ||
|
||
/** | ||
* @param {string} eventName | ||
* @param {EventCallback} callback | ||
* @return {Unsubscribe} | ||
*/ | ||
subscribe(eventName: string, callback: EventCallback): Unsubscribe { | ||
const id = Symbol(callback.toString()); | ||
this.subscriptions.set( | ||
eventName, | ||
this.subscriptions.has(eventName) | ||
? [...(this.subscriptions.get(eventName) || []), { id, callback }] | ||
: [{ id, callback }] | ||
); | ||
|
||
return { | ||
unsubscribe: () => | ||
this.subscriptions.set( | ||
eventName, | ||
(this.subscriptions.get(eventName) || []).filter( | ||
({ id: subscriptionId }) => subscriptionId !== id | ||
) | ||
), | ||
}; | ||
} | ||
|
||
/** | ||
* @param {string} eventName | ||
* @param {Array<any>} args | ||
*/ | ||
emit(eventName: string, ...args: Array<any>): void { | ||
(this.subscriptions.get(eventName) || []).forEach(({ callback }) => | ||
callback(...args) | ||
); | ||
} | ||
} | ||
export * from './EventEmitter'; |
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