-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: correct conjoined name * refactor * refactor * refactor
- Loading branch information
Showing
4 changed files
with
140 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { EventName, PendingConjoinEvent } from "modules/types.ts"; | ||
|
||
/** | ||
* A queue for conjoined events. | ||
*/ | ||
export class ConjoinQueue { | ||
/** | ||
* Create a new instance of the ConjoinQueue. | ||
* @param queue The pending conjoin events. | ||
*/ | ||
constructor(private queue: PendingConjoinEvent[] = []) { | ||
this.queue = queue; | ||
} | ||
|
||
/** | ||
* Consume the conjoined event. | ||
* @param event The event name. | ||
* @returns The fulfilled events and idle events. | ||
*/ | ||
consume(event: EventName) { | ||
const fulfill: EventName[] = []; | ||
const idle: PendingConjoinEvent[] = []; | ||
|
||
for (const pending of this.queue) { | ||
const found = pending.conjoined.indexOf(event); | ||
if (found === -1) { | ||
idle.push(pending); | ||
} else { | ||
pending.conjoined.splice(found, 1); | ||
if (pending.conjoined.length === 0) { | ||
fulfill.push(pending.event); | ||
} else { | ||
idle.push(pending); | ||
} | ||
} | ||
} | ||
|
||
return { fulfill, idle }; | ||
} | ||
|
||
/** | ||
* Enqueue a pending conjoin event. | ||
* @param event The pending conjoin event. | ||
*/ | ||
enqueue(event: PendingConjoinEvent) { | ||
this.queue.push(event); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { | ||
EventHandlerSignature, | ||
EventName, | ||
RegisteredHandlers, | ||
} from "modules/types.ts"; | ||
|
||
import { SequenceRunner } from "modules/runners/sequence.ts"; | ||
|
||
/** | ||
* Run handlers each in series. | ||
*/ | ||
export class SeriesRunner { | ||
/** | ||
* Create a new instance of the SeriesRunner. | ||
* @param handlers The handlers with series name to run. | ||
*/ | ||
constructor(private handlers: RegisteredHandlers) { | ||
this.handlers = handlers; | ||
} | ||
|
||
/** | ||
* Remove a handler. | ||
* @param key The event name. | ||
* @param profile The handler profile. | ||
*/ | ||
private remove(key: EventName, profile: EventHandlerSignature<any>): void { | ||
const handlers = this.handlers.get(key); | ||
if (!handlers) return; | ||
const idx = handlers.findIndex((h) => h.handler === profile.handler); | ||
if (idx !== -1) handlers.splice(idx, 1); | ||
} | ||
|
||
/** | ||
* Execute the handlers in series. | ||
* @param series The series of event names. | ||
* @param idx The current event index. | ||
*/ | ||
exec(series: EventName[], idx = 0): void | Promise<void> { | ||
const key = series[idx]; | ||
if (!key) return; | ||
|
||
const handlers = this.handlers.get(key)?.slice() || []; | ||
for (const p of handlers.filter((p) => !!p.options?.once)) { | ||
this.remove(key, p); | ||
} | ||
if (!handlers.length) return; | ||
|
||
const result = new SequenceRunner(handlers).exec(0); | ||
if (result) { | ||
return result.then(() => this.exec(series, idx + 1)); | ||
} | ||
return this.exec(series, idx + 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters