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:
@@ -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();
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user