44 */
55
66import { Wizard , WizardOptions } from '../wizards/wizard'
7+ import { Prompter } from './prompter'
78import { WizardPrompter } from './wizardPrompter'
89import { createHash } from 'crypto'
910
@@ -17,41 +18,46 @@ export abstract class NestedWizard<T> extends Wizard<T> {
1718 */
1819 private wizardInstances : Map < string , any > = new Map ( )
1920
20- protected constructor ( options : WizardOptions < T > ) {
21+ public constructor ( options ? : WizardOptions < T > ) {
2122 super ( options )
2223 }
2324
2425 /**
25- * Creates or retrieves a memoized wizard prompter instance
26+ * Creates a prompter for a wizard instance with memoization.
2627 *
27- * @param {new (...args: any[]) => T } constructor - The constructor function for creating the wizard instance
28- * @param {...any[] } args - Arguments to pass to the constructor
29- * @returns {WizardPrompter<T> } A wrapped wizard to be used as prompter in parent wizard class
28+ * @template TWizard - The type of wizard, must extend Wizard<TState>
29+ * @template TState - The type of state managed by the wizard
3030 *
31- * @remarks
32- * This method uses memoization to cache wizard instances based on their constructor
33- * name and arguments, allowing for restoring wizard state for back button.
31+ * @param wizardClass - The wizard class constructor
32+ * @param args - Constructor arguments for the wizard instance
33+ *
34+ * @returns A wizard prompter to be used as prompter
3435 *
3536 * @example
36- * this.createWizardPrompter(
37- * TemplateParametersWizard,
38- * template!.uri,
39- * samSyncUrl,
40- * syncMementoRootKey
41- * ),
37+ * // Create a prompter for SyncWizard
38+ * const prompter = this.createWizardPrompter<SyncWizard, SyncParams>(
39+ * SyncWizard,
40+ * template.uri,
41+ * syncUrl
42+ * )
43+ *
44+ * @remarks
45+ * - Instances are memoized using a SHA-256 hash of the wizard class name and arguments
46+ * - The same wizard instance is reused for identical constructor parameters for restoring wizard prompter
47+ * states during back button click event
4248 */
43- protected createWizardPrompter < T extends Wizard < any > > (
44- constructor : new ( ...args : any [ ] ) => T ,
45- ...args : ConstructorParameters < new ( ...args : any [ ] ) => T >
46- ) : WizardPrompter < T > {
49+ protected createWizardPrompter < TWizard extends Wizard < TState > , TState > (
50+ wizardClass : new ( ...args : any [ ] ) => TWizard ,
51+ ...args : ConstructorParameters < new ( ...args : any [ ] ) => TWizard >
52+ ) : Prompter < TState > {
4753 const memoizeKey = createHash ( 'sha256' )
48- . update ( constructor . name + JSON . stringify ( args ) )
54+ . update ( wizardClass . name + JSON . stringify ( args ) )
4955 . digest ( 'hex' )
5056
5157 if ( ! this . wizardInstances . get ( memoizeKey ) ) {
52- this . wizardInstances . set ( memoizeKey , new constructor ( ...args ) )
58+ this . wizardInstances . set ( memoizeKey , new wizardClass ( ...args ) )
5359 }
5460
55- return new WizardPrompter ( this . wizardInstances . get ( memoizeKey ) )
61+ return new WizardPrompter ( this . wizardInstances . get ( memoizeKey ) ) as Prompter < TState >
5662 }
5763}
0 commit comments