Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Emitter of a healthy mind #267

Open
tshemsedinov opened this issue Dec 10, 2024 · 7 comments
Open

Create Emitter of a healthy mind #267

tshemsedinov opened this issue Dec 10, 2024 · 7 comments
Assignees

Comments

@tshemsedinov
Copy link
Member

tshemsedinov commented Dec 10, 2024

type Listener = (data: unknown) => void;
type EventName = string | symbol;

export class Emitter {
  emit(eventName: EventName, data: unknown): Promise<void>;
  on(eventName: EventName, listener: Listener): void;
  once(eventName: EventName, listener: Listener): void;
  off(eventName: EventName, listener?: Listener): void;
  toPromise(eventName: EventName): Promise<unknown>;
  toAsyncIterable(eventName: EventName): AsyncIterable<unknown>;
  clear(eventName?: EventName): void;
  listeners(eventName?: EventName): Listener[];
  listenerCount(eventName?: EventName): number;
  eventNames(): EventName[];
}

Iteration example:

const ee = new Emitter();

for await (const user of ee.toAsyncIterable('user')) {
  console.log({ user });
}
  1. ee.toAsyncIterable('user') will return new AsyncIterable('user')
  2. for/await will call asyncIterable[Symbol.asyncIterator] will return AsyncIterator
  3. asyncIterator.next will return AsyncIteratorStep { done, value }
tshemsedinov added a commit that referenced this issue Dec 11, 2024
GoodLuckMister added a commit to GoodLuckMister/metautil that referenced this issue Dec 20, 2024
GoodLuckMister added a commit to GoodLuckMister/metautil that referenced this issue Dec 20, 2024
@GoodLuckMister
Copy link

GoodLuckMister commented Dec 20, 2024

Hi ! Could I complete this issue ?

@tshemsedinov
Copy link
Member Author

Yes, this is just a draft, it might be useful, but it needs some work, maybe the contracts are not convenient, maybe the implementation can be optimized, more tests, use cases, docs, etc.

@tshemsedinov
Copy link
Member Author

@GoodLuckMister You can do that

GoodLuckMister added a commit to GoodLuckMister/metautil that referenced this issue Dec 24, 2024
serrhiy added a commit to serrhiy/metautil that referenced this issue Jan 24, 2025
@tshemsedinov
Copy link
Member Author

tshemsedinov commented Jan 28, 2025

Example 1:

const ee = new Emitter();
for await (const chunk of ee.toIterator('chunk')) {
  console.log({ chunk });
}

Example 2:

const ee = new Emitter();
const readable = Readable.from(ee);
readable.pipe(target);

@tshemsedinov tshemsedinov changed the title Create EventEmitter of a healthy mind Create Emitter of a healthy mind Jan 30, 2025
@tshemsedinov
Copy link
Member Author

@GoodLuckMister @serrhiy I'd prefer you will work together. Please rename our EventEmitter to Emitter to emplasis its alternative implementation and incompatibility while stay close related abstraction.

@tshemsedinov
Copy link
Member Author

@GoodLuckMister @serrhiy Please note than we move from multiple arguments in event (...args: unknown[]) to single one (data: unknown)

@serrhiy
Copy link

serrhiy commented Jan 31, 2025

@GoodLuckMister email me (listed in my profile) so we can coordinate actions

serrhiy added a commit to GoodLuckMister/metautil that referenced this issue Jan 31, 2025
const emitExpect = ['await emit 1', 'await emit 2', 'await emit 3'];
emitExpect.forEach((e) => ee.on('name5', () => e));
const emitResult = await ee.emit('name5');
assert.deepStrictEqual(emitResult, emitExpect);

Emmiter's 'emit' methos does not return value and resolves when all async functions finish.
metarhia#267
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants