基于Ctx事件触发器,封装了满足多种需求的事件触发器。
方法
post(data: T): void;
attach<L extends BFS.Evt.Listener<T>(listener: L): L
const ctx = new Ctx();
ctx.attach((name:string)=>{
console.log('name', name);
})
ctx.post('john');
dettach(listener: Listener<T>): void;
const ctx = new Ctx();
const listener = (name:string)=>{
console.log('name', name);
};
ctx.attach(listener)
ctx.post('john');
ctx.dettach(listener)
ctx.post('john');
detachAll(): void;
const ctx = new Ctx();
const listener = (name:string)=>{
console.log('name', name);
};
ctx.attach(listener)
ctx.post('john');
ctx.detachAll()
ctx.post('john');
attach(listener: Listener<T>): Listener<T>
attachOnce(listener: Listener<T>): Listener<T>;
detach(listener: Listener<T>): void
attachCtx(ctx: Ctx<T>): void
detachCtx(ctx: Ctx<T>): void
bindPoster(poster: PostableEvt<T>)
unbindPoster(poster: PostableEvt<T>): void
bindCtx(ctx: Ctx<T>): void
unbindCtx(ctx: Ctx<T>): boolean
方法
Evt<T>.post: (data: T) => void;
Evt<T>.attach: (listener: BFS.Evt.Listener<T, unknown>) => BFS.Evt.Listener<T, unknown>
const { Evt } = bfsprocess.import('evt');
const evt = new Evt();
evt.attach((name)=>{
console.log('接收到了name:', name)
})
evt.post('Tom');
evt.post('Cat');
// 运行结果:
// 接收到了name:Tom
// 接收到了name:Cat
Evt<T>.attachOnce: (listener: BFS.Evt.Listener<T, unknown>) => BFS.Evt.Listener<T, unknown>
const { Evt } = bfsprocess.import('evt');
const evt = new Evt();
evt.attachOnce((name)=>{
console.log('接收到了name:', name)
})
evt.post('Tom');
evt.post('Cat');
// 运行结果:
// 接收到了name:Tom
Evt<T>.attachCtx: (ctx: Ctx<T>) => void
const { Evt, Ctx } = bfsprocess.import('evt');
const ctx = new Ctx();
ctx.attach((data)=>{
console.log(data);
});
evt.attachCtx(ctx);
evt.post('good job');
// 运行结果:
// good job
Evt<T>.detach: (listener: BFS.Evt.Listener<T, unknown>) => void
const { Evt } = bfsprocess.import('evt');
const evt = new Evt();
const listner = (name)=>{
console.log('接收到了name:', name)
}
evt.attach(listner)
evt.post('Tom');
evt.detach(listner);
evt.post('Cat');
// 运行结果:
// 接收到了name:Tom
Evt<T>.detachCtx: (ctx: Ctx<T>) => void
const { Evt, Ctx } = bfsprocess.import('evt');
const evt = new Evt();
const ctx = new Ctx();
ctx.attach((data)=>{
console.log(data);
});
evt.attachCtx(ctx);
evt.post('good job');
evt.detachCtx(ctx);
evt.post('bad job');
// 运行结果:
// good job
Evt<T>.detachAll: () => void;
const { Evt } = bfsprocess.import('evt');
const evt = new Evt();
const listner1 = (name)=>{
console.log('接收到了name1:', name)
}
const listner2 = (name)=>{
console.log('接收到了name2:', name)
}
const listner3 = (name)=>{
console.log('接收到了name3:', name)
}
evt.attach(listner1);
evt.attach(listner2);
evt.attach(listner3);
evt.post('Tom');
evt.detach(listner2);
evt.post('Cat');
evt.detachAll();
evt.post('Tony');
// 运行结果:
// 接收到了name1: Tom
// 接收到了name2: Tom
// 接收到了name3: Tom
// 接收到了name1: Cat
// 接收到了name3: Cat
Evt<T>.pause: () => void;
Evt<T>.resume: () => void;
const { Evt } = bfsprocess.import('evt');
evt.attachCtx((data)=>{
console.log(data);
});
evt.pause();
evt.post('good job');
evt.post('badjob');
evt.resume(); // 重新发送缓存中的消息
evt.pause(); // 清空队列中缓存的消息
evt.resume();// 重新发送缓存中的消息,实际上这里队列中的消息已经为空,不会发出任何消息
// 运行结果:
// good job
// bad job
// good job
// bad job
waitFor<T>(from: BFS.Evt.AttachOnlyEvtBase<T>, listener: BFS.Evt.Listener<T, boolean>): Promise<T>
const evt = new Evt();
(async()=>{
try {
setTimeout(()=>{
evt.post('finish')
}, 2000)
await waitFor(evt.attacher, (data) => data === 'finish');
} finally {
console.log('has finished')
}
})()
// 运行结果:
// 两秒钟后打印:has finishd
namespace BFS.Evt {
type Listener<T, R = unknown> = (data: T) => R;
interface Ctx<T> {
attach<L extends Listener<T>>(listener: L): L;
detach(listener: Listener<T>): void;
detachAll(): void;
post(data: T): void;
}
interface AttachOnlyEvtBase<T> {
attach(listener: Listener<T>): Listener<T>;
attachOnce(listener: Listener<T>): Listener<T>;
detach(listener: Listener<T>): void;
attachCtx(ctx: Ctx<T>): void;
detachCtx(ctx: Ctx<T>): void;
detachAll(): void;
}
interface AttachOnlyEvt<T> extends AttachOnlyEvtBase<T> {
bindPoster(poster: PostableEvt<T>): void;
unbindPoster(poster: PostableEvt<T>): void;
}
interface PostableEvtBase<T> {
pause(): void;
resume(): void;
post(data: T): void;
}
interface PostableEvt<T> extends PostableEvtBase<T> {
bindCtx(ctx: Ctx<T>): void;
unbindCtx(ctx: Ctx<T>): void;
}
}