@@ -28,16 +28,17 @@ let connection: HubConnection;
2828let circuit : CircuitDescriptor ;
2929let dotNetDispatcher : CircuitDotNetCallDispatcher ;
3030let dispatcher : DotNet . ICallDispatcher ;
31- let userOptions : Partial < CircuitStartOptions > | undefined ;
31+ let renderQueue : RenderQueue ;
32+ let options : CircuitStartOptions ;
3233let logger : ConsoleLogger ;
3334let afterRenderCallback : ( ( ) => void ) | undefined ;
3435
3536export function setCircuitOptions ( circuitUserOptions ?: Partial < CircuitStartOptions > ) {
36- if ( userOptions ) {
37+ if ( options ) {
3738 throw new Error ( 'Circuit options have already been configured.' ) ;
3839 }
3940
40- userOptions = circuitUserOptions ;
41+ options = resolveOptions ( circuitUserOptions ) ;
4142}
4243
4344export async function startServer ( components : RootComponentManager < ServerComponentDescriptor > ) : Promise < void > {
@@ -48,7 +49,6 @@ export async function startServer(components: RootComponentManager<ServerCompone
4849 started = true ;
4950
5051 // Establish options to be used
51- const options = resolveOptions ( userOptions ) ;
5252 logger = new ConsoleLogger ( options . logLevel ) ;
5353
5454 const jsInitializer = await fetchAndInvokeInitializers ( options ) ;
@@ -59,7 +59,7 @@ export async function startServer(components: RootComponentManager<ServerCompone
5959 return false ;
6060 }
6161
62- const reconnection = existingConnection || await initializeConnection ( options , logger , circuit ) ;
62+ const reconnection = existingConnection || await initializeConnection ( logger , circuit ) ;
6363 if ( ! ( await circuit . reconnect ( reconnection ) ) ) {
6464 logger . log ( LogLevel . Information , 'Reconnection attempt to the circuit was rejected by the server. This may indicate that the associated state is no longer available on the server.' ) ;
6565 return false ;
@@ -114,13 +114,13 @@ export function startCircuit(components: RootComponentManager<ServerComponentDes
114114 }
115115
116116 startCircuitPromise ??= ( async ( ) => {
117- const options = resolveOptions ( userOptions ) ;
118117 const appState = discoverPersistedState ( document ) ;
118+ renderQueue = new RenderQueue ( logger ) ;
119119 circuit = new CircuitDescriptor ( components , appState || '' ) ;
120120 dotNetDispatcher = new CircuitDotNetCallDispatcher ( ( ) => connection ) ;
121121 dispatcher = DotNet . attachDispatcher ( dotNetDispatcher ) ;
122122
123- const initialConnection = await initializeConnection ( options , logger , circuit ) ;
123+ const initialConnection = await initializeConnection ( logger , circuit ) ;
124124 const circuitStarted = await circuit . startCircuit ( initialConnection ) ;
125125 if ( ! circuitStarted ) {
126126 logger . log ( LogLevel . Error , 'Failed to start the circuit.' ) ;
@@ -165,7 +165,7 @@ export function disposeCircuit() {
165165 detachWebRendererInterop ( WebRendererId . Server ) ;
166166}
167167
168- async function initializeConnection ( options : CircuitStartOptions , logger : Logger , circuit : CircuitDescriptor ) : Promise < HubConnection > {
168+ async function initializeConnection ( logger : Logger , circuit : CircuitDescriptor ) : Promise < HubConnection > {
169169 const hubProtocol = new MessagePackHubProtocol ( ) ;
170170 ( hubProtocol as unknown as { name : string } ) . name = 'blazorpack' ;
171171
@@ -196,7 +196,6 @@ async function initializeConnection(options: CircuitStartOptions, logger: Logger
196196 dispatcher . supplyDotNetStream ( streamId , readableStream ) ;
197197 } ) ;
198198
199- const renderQueue = new RenderQueue ( logger ) ;
200199 newConnection . on ( 'JS.RenderBatch' , async ( batchId : number , batchData : Uint8Array ) => {
201200 logger . log ( LogLevel . Debug , `Received render batch with id ${ batchId } and ${ batchData . byteLength } bytes.` ) ;
202201 await renderQueue . processBatch ( batchId , batchData , newConnection ) ;
@@ -205,7 +204,7 @@ async function initializeConnection(options: CircuitStartOptions, logger: Logger
205204
206205 newConnection . on ( 'JS.EndLocationChanging' , Blazor . _internal . navigationManager . endLocationChanging ) ;
207206
208- newConnection . onclose ( error => ! renderingFailed && options . reconnectionHandler ! . onConnectionDown ( options . reconnectionOptions , error ) ) ;
207+ newConnection . onclose ( error => isCircuitActive ( ) && ! renderingFailed && options . reconnectionHandler ! . onConnectionDown ( options . reconnectionOptions , error ) ) ;
209208 newConnection . on ( 'JS.Error' , error => {
210209 renderingFailed = true ;
211210 unhandledError ( newConnection , error , logger ) ;
0 commit comments