Show confirmation when there are unsaved changes

This commit is contained in:
Eric Zhang 2021-06-03 16:19:00 -05:00
parent efcc9591ff
commit 06445802da
2 changed files with 18 additions and 8 deletions

View file

@ -62,7 +62,7 @@ fn backend() -> BoxedFilter<(impl Reply,)> {
let value = entry.value_mut();
value.last_accessed = Instant::now();
let rustpad = Arc::clone(&value.rustpad);
ws.on_upgrade(move |socket| async move { rustpad.on_connection(socket).await })
ws.on_upgrade(|socket| async move { rustpad.on_connection(socket).await })
},
);

View file

@ -18,6 +18,7 @@ class Rustpad {
private recentFailures: number = 0;
private readonly model: editor.ITextModel;
private readonly onChangeHandle: any;
private readonly beforeUnload: (event: BeforeUnloadEvent) => void;
private readonly tryConnectId: number;
private readonly resetFailuresId: number;
@ -36,15 +37,23 @@ class Rustpad {
this.onChangeHandle = options.editor.onDidChangeModelContent((e) =>
this.onChange(e)
);
this.beforeUnload = (event: BeforeUnloadEvent) => {
if (this.outstanding) {
event.preventDefault();
event.returnValue = "";
} else {
delete event.returnValue;
}
};
window.addEventListener("beforeunload", this.beforeUnload);
const interval = options.reconnectInterval ?? 1000;
this.tryConnect();
const interval = options.reconnectInterval ?? 1000
this.tryConnectId = window.setInterval(
() => this.tryConnect(),
interval
this.tryConnectId = window.setInterval(() => this.tryConnect(), interval);
this.resetFailuresId = window.setInterval(
() => (this.recentFailures = 0),
15 * interval
);
this.resetFailuresId = window.setInterval(() =>
this.recentFailures = 0
, 15 * interval);
}
/** Destroy this Rustpad instance and close any sockets. */
@ -52,6 +61,7 @@ class Rustpad {
window.clearInterval(this.tryConnectId);
window.clearInterval(this.resetFailuresId);
this.onChangeHandle.dispose();
window.removeEventListener("beforeunload", this.beforeUnload);
this.ws?.close();
}