@pqstudio/pq_events
v2.0.30
Published
pq_events
Downloads
26
Readme
pq_events
事件实现
Usage
使用装饰器方式
@PQEventSubscriber
class EventBus implements PQIEventDispatcher {
//#region 实现PQIEventDispatcher
public on!: <T, U>(type: string, handler: CallBackHandler<T>, caller: U, priority?: number, once?: number) => void;
public off!: <T, U>(type: string, handler: CallBackHandler<T>, caller: U) => void;
public emit!: <U>(type: string, data: U) => void;
public dispatch!: <U>(type: string, data: U) => void;
//#endregion 实现PQIEventDispatcher
@addPQEventListener("message")
public onMessage(event: PQEvent<unknown>): void {
expect(event.data).toBe(1);
}
@addPQEventListener(new A())
public onMesssage1(event: PQEvent<number>): void {
expect(event.data).toBe(1);
expect((<A><unknown>expect(event.source)).a).toBe(1);
}
@addPQEventListener(ia)
public onMesssage2(event: PQEvent<number>): void {
expect(event.data).toBe(1);
expect((<A><unknown>expect(event.source)).a).toBe(2);
}
}
使用继承方式
class EventBus1 extends PQEventDispatcher() implements PQIEventDispatcher {
//#region 实现PQIEventDispatcher
public on!: <T, U>(type: string, handler: CallBackHandler<T>, caller: U, priority?: number, once?: number) => void;
public off!: <T, U>(type: string, handler: CallBackHandler<T>, caller: U) => void;
public emit!: <U>(type: string, data: U) => void;
public dispatch!: <U>(type: string, data: U) => void;
//#endregion 实现PQIEventDispatcher
@addPQEventListener("message")
public onMessage(event: PQEvent<unknown>): void {
expect(event.data).toBe(1);
}
}