Skip to content

Commit 0119a2e

Browse files
committed
Fixes and improvements
1 parent bad98de commit 0119a2e

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

src/Components/Web.JS/dist/Release/blazor.server.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/dist/Release/blazor.web.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/src/Boot.Server.Common.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ let connection: HubConnection;
2828
let circuit: CircuitDescriptor;
2929
let dotNetDispatcher: CircuitDotNetCallDispatcher;
3030
let dispatcher: DotNet.ICallDispatcher;
31-
let userOptions: Partial<CircuitStartOptions> | undefined;
31+
let renderQueue: RenderQueue;
32+
let options: CircuitStartOptions;
3233
let logger: ConsoleLogger;
3334
let afterRenderCallback: (() => void) | undefined;
3435

3536
export 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

4344
export 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);

src/Components/test/E2ETest/ServerRenderingTests/InteractivityTest.cs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,31 @@ public void Circuit_CanShutDownAndReInitializeMultipleTimes()
714714
}
715715
}
716716

717+
[Fact]
718+
public async Task ReconnectionHandler_DoesNotRun_AfterIntentionallyShuttingDownTheCircuit()
719+
{
720+
Navigate($"{ServerPathBase}/streaming-interactivity");
721+
722+
Browser.Equal("Not streaming", () => Browser.FindElement(By.Id("status")).Text);
723+
724+
Browser.Click(By.Id(AddServerPrerenderedId));
725+
Browser.Equal("True", () => Browser.FindElement(By.Id("is-interactive-0")).Text);
726+
727+
Browser.Click(By.Id("increment-0"));
728+
Browser.Equal("1", () => Browser.FindElement(By.Id("count-0")).Text);
729+
730+
Browser.Click(By.Id("remove-counter-link-0"));
731+
732+
AssertBrowserLogContainsMessage("Connection disconnected.");
733+
AssertBrowserLogDoesNotContainErrors();
734+
ClearBrowserLogs();
735+
736+
// Wait for the reconnection handler to run, if it's going to. Hopefully it doesn't.
737+
await Task.Delay(5000);
738+
739+
AssertBrowserLogDoesNotContainMessage("WebSocket connected to");
740+
}
741+
717742
[Fact]
718743
public void DotNetObjectReference_CannotBeUsed_AfterCircuitShutsDown()
719744
{
@@ -821,12 +846,15 @@ private string InteractiveCallsiteUrl(bool prerender, int? serverIncrement = def
821846
}
822847

823848
private void AssertBrowserLogContainsMessage(string message)
849+
=> Browser.True(() => DoesBrowserLogContainMessage(message));
850+
851+
private void AssertBrowserLogDoesNotContainMessage(string message)
852+
=> Browser.False(() => DoesBrowserLogContainMessage(message));
853+
854+
private bool DoesBrowserLogContainMessage(string message)
824855
{
825-
Browser.True(() =>
826-
{
827-
var entries = Browser.Manage().Logs.GetLog(LogType.Browser);
828-
return entries.Any(entry => entry.Message.Contains(message));
829-
});
856+
var entries = Browser.Manage().Logs.GetLog(LogType.Browser);
857+
return entries.Any(entry => entry.Message.Contains(message));
830858
}
831859

832860
private void AssertBrowserLogDoesNotContainErrors()

0 commit comments

Comments
 (0)