@@ -7,6 +7,12 @@ let nextPendingDynamicRootComponentIdentifier = 0;
7
7
type ComponentParameters = object | null | undefined ;
8
8
9
9
let manager : DotNet . DotNetObject | undefined ;
10
+ let initializationCallbacks : { failedToInitialize ?: ( reason ?: any ) => void , initialized ?: ( ) => void } = { } ;
11
+ let initializerPromise = new Promise < void > ( ( resolve , reject ) => {
12
+ initializationCallbacks . initialized = resolve ;
13
+ initializationCallbacks . failedToInitialize = reject ;
14
+ } ) ;
15
+
10
16
let jsComponentParametersByIdentifier : JSComponentParametersByIdentifier ;
11
17
12
18
// These are the public APIs at Blazor.rootComponents.*
@@ -20,6 +26,9 @@ export const RootComponentsFunctions = {
20
26
const containerIdentifier = pendingRootComponentContainerNamePrefix + ( ++ nextPendingDynamicRootComponentIdentifier ) . toString ( ) ;
21
27
pendingRootComponentContainers . set ( containerIdentifier , toElement ) ;
22
28
29
+ // Wait until the C# runtime has initialized the root components.
30
+ await initializerPromise ;
31
+
23
32
// Instruct .NET to add and render the new root component
24
33
const componentId = await getRequiredManager ( ) . invokeMethodAsync < number > (
25
34
'AddRootComponent' , componentIdentifier , containerIdentifier ) ;
@@ -121,7 +130,10 @@ export function enableJSRootComponents(
121
130
if ( manager ) {
122
131
// This will only happen in very nonstandard cases where someone has multiple hosts.
123
132
// It's up to the developer to ensure that only one of them enables dynamic root components.
124
- throw new Error ( 'Dynamic root components have already been enabled.' ) ;
133
+ const error = new Error ( 'Dynamic root components have already been enabled.' ) ;
134
+ if ( initializationCallbacks . failedToInitialize ) {
135
+ initializationCallbacks . failedToInitialize ( error ) ;
136
+ }
125
137
}
126
138
127
139
manager = managerInstance ;
@@ -137,6 +149,9 @@ export function enableJSRootComponents(
137
149
initializerFunc ( componentIdentifier , parameters ) ;
138
150
}
139
151
}
152
+ if ( initializationCallbacks . initialized ) {
153
+ initializationCallbacks . initialized ( ) ;
154
+ }
140
155
}
141
156
142
157
function getRequiredManager ( ) : DotNet . DotNetObject {
0 commit comments