mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 02:44:45 +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';
|
import { PubSub } from './index';
|
||||||
|
|
||||||
describe('pubsub', () => {
|
describe('pubsub', () => {
|
||||||
|
const event3 = Symbol('event3');
|
||||||
|
|
||||||
let eventBus: PubSub<{
|
let eventBus: PubSub<{
|
||||||
event1: (arg: string) => void;
|
event1: (arg: string) => void;
|
||||||
event2: () => void
|
event2: () => void;
|
||||||
|
[event3]: () => void;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -32,6 +35,15 @@ describe('pubsub', () => {
|
|||||||
expect(listener2).toHaveBeenCalledWith('Hello');
|
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', () => {
|
it('add a one-time listener and emit an event', () => {
|
||||||
const listener = vi.fn();
|
const listener = vi.fn();
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
export type Subscriber = (...args: any[]) => void;
|
export type Subscriber = (...args: any[]) => void;
|
||||||
export type EventsRecord = Record<string, Subscriber>;
|
export type EventsRecord = Record<string | symbol, Subscriber>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple PubSub implementation
|
* Simple PubSub implementation
|
||||||
|
|||||||
Reference in New Issue
Block a user