@@ -7,21 +7,51 @@ import { Wizard, WizardOptions } from '../wizards/wizard'
77import { WizardPrompter } from './wizardPrompter'
88import { createHash } from 'crypto'
99
10+ /**
11+ * An abstract class that extends the base Wizard class plus the ability to
12+ * use other wizard classes as prompters
13+ */
1014export abstract class NestedWizard < T > extends Wizard < T > {
11- // Map to store wizard instances
15+ /**
16+ * Map to store memoized wizard instances using SHA-256 hashed keys
17+ */
1218 private wizardInstances : Map < string , any > = new Map ( )
1319
1420 protected constructor ( options : WizardOptions < T > ) {
1521 super ( options )
1622 }
1723
18- protected createWizardPrompter < T > ( constructor : new ( ...args : any [ ] ) => T , ...args : any [ ] ) : WizardPrompter < T > {
24+ /**
25+ * Creates or retrieves a memoized wizard prompter instance
26+ *
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
30+ *
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.
34+ *
35+ * @example
36+ * this.createWizardPrompter(
37+ * TemplateParametersWizard,
38+ * template!.uri,
39+ * samSyncUrl,
40+ * syncMementoRootKey
41+ * ),
42+ */
43+ protected createWizardPrompter < T extends Wizard < any > > (
44+ constructor : new ( ...args : any [ ] ) => T ,
45+ ...args : ConstructorParameters < new ( ...args : any [ ] ) => T >
46+ ) : WizardPrompter < T > {
1947 const memoizeKey = createHash ( 'sha256' )
2048 . update ( constructor . name + JSON . stringify ( args ) )
2149 . digest ( 'hex' )
22- if ( ! this . wizardInstances . get ( memoizeKey ) as T ) {
50+
51+ if ( ! this . wizardInstances . get ( memoizeKey ) ) {
2352 this . wizardInstances . set ( memoizeKey , new constructor ( ...args ) )
2453 }
54+
2555 return new WizardPrompter ( this . wizardInstances . get ( memoizeKey ) )
2656 }
2757}
0 commit comments