Disconnect circuit on `beforeunload` event (#23224)

This commit is contained in:
Safia Abdalla 2020-06-24 11:35:28 -07:00 committed by GitHub
parent f6a6e4bd07
commit 4465a8efc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

File diff suppressed because one or more lines are too long

View File

@ -56,16 +56,18 @@ async function boot(userOptions?: Partial<CircuitStartOptions>): Promise<void> {
return true;
};
window.addEventListener(
'unload',
() => {
let disconnectSent = false;
const cleanup = () => {
if (!disconnectSent) {
const data = new FormData();
const circuitId = circuit.circuitId!;
data.append('circuitId', circuitId);
navigator.sendBeacon('_blazor/disconnect', data);
},
false
);
disconnectSent = navigator.sendBeacon('_blazor/disconnect', data);
}
};
window.addEventListener('beforeunload', cleanup, { capture: false, once: true });
window.addEventListener('unload', cleanup, { capture: false, once: true });
window['Blazor'].reconnect = reconnect;