Skip to content

Commit 2487db5

Browse files
committed
Wait for the framework to enable dynamic root components
1 parent b05ed6d commit 2487db5

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/Components/Web.JS/src/Rendering/JSRootComponents.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ let nextPendingDynamicRootComponentIdentifier = 0;
77
type ComponentParameters = object | null | undefined;
88

99
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+
1016
let jsComponentParametersByIdentifier: JSComponentParametersByIdentifier;
1117

1218
// These are the public APIs at Blazor.rootComponents.*
@@ -20,6 +26,9 @@ export const RootComponentsFunctions = {
2026
const containerIdentifier = pendingRootComponentContainerNamePrefix + (++nextPendingDynamicRootComponentIdentifier).toString();
2127
pendingRootComponentContainers.set(containerIdentifier, toElement);
2228

29+
// Wait until the C# runtime has initialized the root components.
30+
await initializerPromise;
31+
2332
// Instruct .NET to add and render the new root component
2433
const componentId = await getRequiredManager().invokeMethodAsync<number>(
2534
'AddRootComponent', componentIdentifier, containerIdentifier);
@@ -121,7 +130,10 @@ export function enableJSRootComponents(
121130
if (manager) {
122131
// This will only happen in very nonstandard cases where someone has multiple hosts.
123132
// 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+
}
125137
}
126138

127139
manager = managerInstance;
@@ -137,6 +149,9 @@ export function enableJSRootComponents(
137149
initializerFunc(componentIdentifier, parameters);
138150
}
139151
}
152+
if(initializationCallbacks.initialized){
153+
initializationCallbacks.initialized();
154+
}
140155
}
141156

142157
function getRequiredManager(): DotNet.DotNetObject {

0 commit comments

Comments
 (0)