mirror of
https://github.com/motajs/template.git
synced 2026-05-21 18:31:11 +08:00
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { ITrigger, ITriggerCollection, ITriggerHandler } from './types';
|
|
|
|
export class TriggerCollection implements ITriggerCollection {
|
|
/** 当前集合内部维护的触发器列表 */
|
|
private readonly triggerList: ITrigger[];
|
|
|
|
constructor(triggers: Iterable<ITrigger>) {
|
|
this.triggerList = [...triggers];
|
|
}
|
|
|
|
count(): number {
|
|
return this.triggerList.length;
|
|
}
|
|
|
|
async trigger(handler: ITriggerHandler): Promise<void> {
|
|
for (const trigger of this.triggerList) {
|
|
await trigger.trigger(handler);
|
|
}
|
|
}
|
|
|
|
async *triggerIter(
|
|
handler: ITriggerHandler
|
|
): AsyncGenerator<ITrigger, void, ITriggerHandler | null> {
|
|
let currentHandler = handler;
|
|
for (const trigger of this.triggerList) {
|
|
await trigger.trigger(currentHandler);
|
|
const nextHandler = yield trigger;
|
|
if (nextHandler) {
|
|
currentHandler = nextHandler;
|
|
} else {
|
|
currentHandler = handler;
|
|
}
|
|
}
|
|
}
|
|
|
|
iterate(): Iterable<ITrigger> {
|
|
return this.triggerList.values();
|
|
}
|
|
|
|
push(trigger: ITrigger): void {
|
|
this.triggerList.push(trigger);
|
|
}
|
|
|
|
unshift(trigger: ITrigger): void {
|
|
this.triggerList.unshift(trigger);
|
|
}
|
|
|
|
concat(...others: ITriggerCollection[]): ITriggerCollection {
|
|
const merged = [...this.triggerList];
|
|
for (const other of others) {
|
|
merged.push(...other.iterate());
|
|
}
|
|
return new TriggerCollection(merged);
|
|
}
|
|
}
|