@@ -7,7 +7,7 @@ import * as vscode from 'vscode'
7
7
import { getLogger } from '../logger'
8
8
import { localize } from '../utilities/vsCodeUtils'
9
9
10
- import { CloudFormationTemplateRegistry } from '../fs/templateRegistry'
10
+ import { AsyncCloudFormationTemplateRegistry , CloudFormationTemplateRegistry } from '../fs/templateRegistry'
11
11
import { getIdeProperties } from '../extensionUtilities'
12
12
import { NoopWatcher } from '../fs/watchedFiles'
13
13
import { createStarterTemplateFile } from './cloudformation'
@@ -35,11 +35,8 @@ export const devfileExcludePattern = /.*devfile\.(yaml|yml)/i
35
35
export async function activate ( extensionContext : vscode . ExtensionContext ) : Promise < void > {
36
36
try {
37
37
const registry = new CloudFormationTemplateRegistry ( )
38
- globals . templateRegistry = registry
39
- await registry . addExcludedPattern ( devfileExcludePattern )
40
- await registry . addExcludedPattern ( templateFileExcludePattern )
41
- await registry . addWatchPattern ( templateFileGlobPattern )
42
- await registry . watchUntitledFiles ( )
38
+ extensionContext . subscriptions . push ( registry )
39
+ setTemplateRegistryInGlobals ( registry )
43
40
} catch ( e ) {
44
41
vscode . window . showErrorMessage (
45
42
localize (
@@ -48,15 +45,49 @@ export async function activate(extensionContext: vscode.ExtensionContext): Promi
48
45
getIdeProperties ( ) . codelenses
49
46
)
50
47
)
51
- getLogger ( ) . error ( 'Failed to activate template registry' , e )
48
+ getLogger ( ) . error ( 'Failed to activate template registry: %s ' , e )
52
49
// This prevents us from breaking for any reason later if it fails to load. Since
53
50
// Noop watcher is always empty, we will get back empty arrays with no issues.
54
51
globals . templateRegistry = new NoopWatcher ( ) as unknown as CloudFormationTemplateRegistry
55
52
}
56
53
// If setting it up worked, add it to subscriptions so it is cleaned up at exit
57
54
extensionContext . subscriptions . push (
58
- globals . templateRegistry ,
59
55
Commands . register ( 'aws.cloudFormation.newTemplate' , ( ) => createStarterTemplateFile ( false ) ) ,
60
56
Commands . register ( 'aws.sam.newTemplate' , ( ) => createStarterTemplateFile ( true ) )
61
57
)
62
58
}
59
+
60
+ /**
61
+ * Sets the `templateRegistry` property in the `globals` variable,
62
+ * where the value of the property depends on whether the registry
63
+ * is fully set up.
64
+ *
65
+ * This function exists to resolve the registry setup taking a long time
66
+ * and slowing down the extension starting up.
67
+ */
68
+ function setTemplateRegistryInGlobals ( registry : CloudFormationTemplateRegistry ) {
69
+ const registrySetupFunc = async ( registry : CloudFormationTemplateRegistry ) => {
70
+ await registry . addExcludedPattern ( devfileExcludePattern )
71
+ await registry . addExcludedPattern ( templateFileExcludePattern )
72
+ await registry . addWatchPattern ( templateFileGlobPattern )
73
+ await registry . watchUntitledFiles ( )
74
+ }
75
+
76
+ const asyncRegistry = new AsyncCloudFormationTemplateRegistry ( registry , registrySetupFunc )
77
+
78
+ Object . defineProperty ( globals , 'templateRegistry' , {
79
+ set ( newInstance : CloudFormationTemplateRegistry ) {
80
+ this . cfnInstance = newInstance
81
+ } ,
82
+ get ( ) {
83
+ // This condition handles testing scenarios where we may have
84
+ // already set a mock object before activation.
85
+ // Though in prod nothing should be calling this 'set' function.
86
+ if ( this . cfnInstance ) {
87
+ return this . cfnInstance
88
+ }
89
+
90
+ return asyncRegistry . getInstance ( )
91
+ } ,
92
+ } )
93
+ }
0 commit comments