@@ -23,6 +23,7 @@ import { CircuitDotNetCallDispatcher } from './Platform/Circuits/CircuitDotNetCa
2323
2424let renderingFailed = false ;
2525let started = false ;
26+ let circuitActive = false ;
2627let startCircuitPromise : Promise < boolean > | undefined ;
2728let connection : HubConnection ;
2829let circuit : CircuitDescriptor ;
@@ -113,6 +114,7 @@ export function startCircuit(components: RootComponentManager<ServerComponentDes
113114 throw new Error ( 'Cannot start the circuit until Blazor Server has started.' ) ;
114115 }
115116
117+ circuitActive = true ;
116118 startCircuitPromise ??= ( async ( ) => {
117119 const appState = discoverPersistedState ( document ) ;
118120 renderQueue = new RenderQueue ( logger ) ;
@@ -132,29 +134,21 @@ export function startCircuit(components: RootComponentManager<ServerComponentDes
132134 return startCircuitPromise ;
133135}
134136
135- export function hasStartedServer ( ) : boolean {
136- return started ;
137- }
138-
139- export function isCircuitActive ( ) : boolean {
140- return startCircuitPromise !== undefined ;
141- }
142-
143- export function attachCircuitAfterRenderCallback ( callback : typeof afterRenderCallback ) {
144- if ( afterRenderCallback ) {
145- throw new Error ( 'A Blazor Server after render batch callback was already attached.' ) ;
137+ export async function disposeCircuit ( ) {
138+ if ( ! circuitActive ) {
139+ return ;
146140 }
147141
148- afterRenderCallback = callback ;
149- }
142+ circuitActive = false ;
150143
151- export function disposeCircuit ( ) {
152- if ( startCircuitPromise === undefined ) {
144+ await startCircuitPromise ;
145+
146+ if ( circuitActive ) {
147+ // A call to 'startCircuit' was made while we were waiting to dispose the circuit.
148+ // Therefore, we should abort the disposal.
153149 return ;
154150 }
155151
156- startCircuitPromise = undefined ;
157-
158152 // We dispose the .NET dispatcher to prevent it from being used in the future.
159153 // This avoids cases where, for example, .NET object references from a
160154 // disconnected circuit start pointing to .NET objects for a new circuit.
@@ -163,6 +157,25 @@ export function disposeCircuit() {
163157 connection . stop ( ) ;
164158
165159 detachWebRendererInterop ( WebRendererId . Server ) ;
160+
161+ // Setting this to undefined allows a new circuit to be started in the future.
162+ startCircuitPromise = undefined ;
163+ }
164+
165+ export function hasStartedServer ( ) : boolean {
166+ return started ;
167+ }
168+
169+ export function isCircuitActive ( ) : boolean {
170+ return circuitActive ;
171+ }
172+
173+ export function attachCircuitAfterRenderCallback ( callback : typeof afterRenderCallback ) {
174+ if ( afterRenderCallback ) {
175+ throw new Error ( 'A Blazor Server after render batch callback was already attached.' ) ;
176+ }
177+
178+ afterRenderCallback = callback ;
166179}
167180
168181async function initializeConnection ( logger : Logger , circuit : CircuitDescriptor ) : Promise < HubConnection > {
0 commit comments