-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* increase delayMs in generate.json for better demos * initial commit * bump version
- Loading branch information
1 parent
cd841bd
commit 6225553
Showing
8 changed files
with
237 additions
and
5 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"delayMs": 10, | ||
"delayMs": 250, | ||
"generated":"${[1..10]~>$generate(delayMs)}" | ||
} |
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,67 @@ | ||
import TemplateProcessor from "./TemplateProcessor.js"; | ||
|
||
/** | ||
* Enum representing the various states of the lifecycle. | ||
* This is used to track different phases during the operation of the system. | ||
*/ | ||
export enum LifecycleState { | ||
/** | ||
* The state representing the start of the initialization process. | ||
*/ | ||
StartInitialize = 'StartInitialize', | ||
|
||
/** | ||
* The state before temporary variables are removed from the system. | ||
*/ | ||
PreTmpVarRemoval = 'PreTmpVarRemoval', | ||
|
||
/** | ||
* The state when the system has been fully initialized and is ready for use. | ||
*/ | ||
Initialized = 'Initialized', | ||
|
||
/** | ||
* The state when the process to close the system begins. | ||
*/ | ||
StartClose = 'StartClose', | ||
|
||
/** | ||
* The state when the system has fully closed and is no longer operational. | ||
*/ | ||
Closed = 'Closed', | ||
} | ||
|
||
/** | ||
* Callback type definition for functions that handle lifecycle transitions. | ||
* | ||
* This type represents an asynchronous function that will be called whenever the | ||
* lifecycle state changes. It receives the new lifecycle state and a `TemplateProcessor` | ||
* instance for processing. | ||
* | ||
* @param state - The new lifecycle state that the system has transitioned to. | ||
* @param templateProcessor - The `TemplateProcessor` instance to be used for handling the state transition. | ||
* | ||
* @returns A `Promise<void>` indicating the asynchronous operation is complete. | ||
*/ | ||
export type LifecycleCallback = (state: LifecycleState, templateProcessor: TemplateProcessor) => Promise<void>; | ||
|
||
/** | ||
* Interface for managing lifecycle callbacks. | ||
*/ | ||
export interface LifecycleOwner { | ||
/** | ||
* Registers a lifecycle callback for a specific lifecycle state. | ||
* @param state The lifecycle state to register the callback for. | ||
* @param cbFn The callback function to execute when the lifecycle state is triggered. | ||
*/ | ||
setLifecycleCallback(state: LifecycleState, cbFn: LifecycleCallback): void; | ||
|
||
/** | ||
* Removes a specific lifecycle callback or all callbacks for a lifecycle state. | ||
* @param state The lifecycle state to remove the callback from. | ||
* @param cbFn The specific callback function to remove. If not provided, all callbacks for the state will be removed. | ||
*/ | ||
removeLifecycleCallback(state: LifecycleState, cbFn?: LifecycleCallback): void; | ||
} | ||
|
||
|
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,72 @@ | ||
import { LifecycleState, LifecycleCallback, LifecycleOwner } from './Lifecycle.js'; | ||
import TemplateProcessor from "./TemplateProcessor.js"; | ||
|
||
|
||
/** | ||
* Class for managing lifecycle callbacks. | ||
*/ | ||
export class LifecycleManager implements LifecycleOwner { | ||
private lifecycleCallbacks: Map<LifecycleState, Set<LifecycleCallback>>; | ||
private templateProcessor: TemplateProcessor; | ||
|
||
constructor(templateProcessor:TemplateProcessor) { | ||
this.lifecycleCallbacks = new Map(); | ||
this.templateProcessor = templateProcessor; | ||
} | ||
|
||
/** | ||
* Registers a lifecycle callback for a specific lifecycle state. | ||
* @param state The lifecycle state to register the callback for. | ||
* @param cbFn The callback function to execute when the lifecycle state is triggered. | ||
*/ | ||
setLifecycleCallback(state: LifecycleState, cbFn: LifecycleCallback) { | ||
this.templateProcessor.logger.debug(`Lifecycle callback set on state: ${state}`); | ||
let callbacks = this.lifecycleCallbacks.get(state); | ||
if (!callbacks) { | ||
callbacks = new Set(); | ||
this.lifecycleCallbacks.set(state, callbacks); | ||
} | ||
callbacks.add(cbFn); | ||
} | ||
|
||
/** | ||
* Removes a specific lifecycle callback or all callbacks for a lifecycle state. | ||
* @param state The lifecycle state to remove the callback from. | ||
* @param cbFn The specific callback function to remove. If not provided, all callbacks for the state will be removed. | ||
*/ | ||
removeLifecycleCallback(state: LifecycleState, cbFn?: LifecycleCallback) { | ||
this.templateProcessor.logger.debug(`Lifecycle callback removed from state: ${state}`); | ||
if (cbFn) { | ||
const callbacks = this.lifecycleCallbacks.get(state); | ||
if (callbacks) { | ||
callbacks.delete(cbFn); | ||
} | ||
} else { | ||
this.lifecycleCallbacks.delete(state); | ||
} | ||
} | ||
|
||
/** | ||
* Calls all lifecycle callbacks registered for a specific lifecycle state. | ||
* @param state The lifecycle state to trigger callbacks for. | ||
*/ | ||
async runCallbacks(state: LifecycleState) { | ||
this.templateProcessor.logger.debug(`Calling lifecycle callbacks for state: ${state}`); | ||
const callbacks = this.lifecycleCallbacks.get(state); | ||
if (callbacks) { | ||
const promises = Array.from(callbacks).map(cbFn => | ||
Promise.resolve().then(() => cbFn(state, this.templateProcessor)) | ||
); | ||
|
||
try { | ||
await Promise.all(promises); | ||
} catch (error: any) { | ||
this.templateProcessor.logger.error(`Error in lifecycle callback at state ${state}: ${error.message}`); | ||
} | ||
} | ||
} | ||
|
||
clear(){ | ||
this.lifecycleCallbacks.clear(); | ||
} | ||
} |
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