1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 19:04:46 +00:00

refactor(packages/stdlib): add symbol in EventsRecord

This commit is contained in:
2024-10-05 05:44:35 +07:00
parent 00fd5846aa
commit 11c099ab4a
2 changed files with 14 additions and 2 deletions

View File

@@ -2,9 +2,12 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';
import { PubSub } from './index';
describe('pubsub', () => {
const event3 = Symbol('event3');
let eventBus: PubSub<{
event1: (arg: string) => void;
event2: () => void
event2: () => void;
[event3]: () => void;
}>;
beforeEach(() => {
@@ -32,6 +35,15 @@ describe('pubsub', () => {
expect(listener2).toHaveBeenCalledWith('Hello');
});
it('emit symbol event', () => {
const listener = vi.fn();
eventBus.on(event3, listener);
eventBus.emit(event3);
expect(listener).toHaveBeenCalled();
});
it('add a one-time listener and emit an event', () => {
const listener = vi.fn();

View File

@@ -1,5 +1,5 @@
export type Subscriber = (...args: any[]) => void;
export type EventsRecord = Record<string, Subscriber>;
export type EventsRecord = Record<string | symbol, Subscriber>;
/**
* Simple PubSub implementation