-
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.
* refacotr: implement sequence runner * chore: ignore lint dist * refactor: redue lines * refactor: fix import * refactor: detect length * refactor: reduce conjoined event emit
- Loading branch information
Showing
8 changed files
with
88 additions
and
49 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
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
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,45 @@ | ||
import type { EventHandlerSignature } from "modules/types.ts"; | ||
|
||
/** | ||
* Run handlers in sequence. | ||
*/ | ||
export class SequenceRunner { | ||
/** | ||
* Create a new instance of the SequenceRunner. | ||
* @param handlers The handlers to run. | ||
*/ | ||
constructor(private handlers: EventHandlerSignature<any>[]) { | ||
this.handlers = handlers; | ||
} | ||
|
||
/** | ||
* Wait for the handler to finish before moving to the next handler. | ||
* @param pointer The current handler index. | ||
* @param profile The handler profile. | ||
* @param args The arguments to pass to the handlers. | ||
*/ | ||
private asyncExec( | ||
pointer: number, | ||
profile: EventHandlerSignature<any>, | ||
...args: any[] | ||
): Promise<void> { | ||
return Promise.resolve(profile.handler(...args)).then(() => | ||
this.exec(pointer + 1, ...args) | ||
); | ||
} | ||
|
||
/** | ||
* Execute the handlers in sequence. | ||
* @param pointer The current handler index. | ||
* @param args The arguments to pass to the handlers. | ||
*/ | ||
exec(pointer: number = 0, ...args: any[]): void | Promise<void> { | ||
const profile = this.handlers[pointer]; | ||
if (!profile) return; | ||
if (profile.options?.async) { | ||
return this.asyncExec(pointer, profile, ...args); | ||
} | ||
profile.handler(...args); | ||
return this.exec(pointer + 1, ...args); | ||
} | ||
} |
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