Skip to content

Commit

Permalink
fix: use named export instead of default export
Browse files Browse the repository at this point in the history
bencelaszlo committed Sep 25, 2024
1 parent 96c3de9 commit f598fa7
Showing 4 changed files with 52 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ npm install chiisai-event-emitter
## Usage

```ts
import eventEmitter from 'chiisai-event-emitter';
import { EventEmitter } from 'chiisai-event-emitter';

const eventEmitter = new EventEmitter();

49 changes: 49 additions & 0 deletions src/EventEmitter.ts
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)
);
}
}
50 changes: 1 addition & 49 deletions src/index.ts
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';
2 changes: 1 addition & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import EventEmitter from '../src';
import { EventEmitter } from '../src';

describe('eventEmitter', () => {
it('should add callback to subscriptions if it has no subscriptions', () => {

0 comments on commit f598fa7

Please sign in to comment.