Skip to content

Commit 4a22999

Browse files
committed
frontend/app: Reconnect remote access when app is resumed. fix #113
1 parent bfedae9 commit 4a22999

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

frontend/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
reg.update();
1818
})
1919
}
20+
function median_app_resumed() {
21+
window.dispatchEvent(new Event("appResumed"));
22+
}
2023
</script>
2124
</head>
2225
<body>

frontend/src/components/Frame.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Row, Spinner } from 'react-bootstrap';
66
import { connected, connected_to, secret } from './charger_list';
77
import { setAppNavigation } from './Navbar';
88
import { enableLogging } from '../utils';
9+
import Median from "median-js-bridge";
910

1011
export const chargerID = signal(0);
1112
export const chargerPort = signal(0);
@@ -15,20 +16,48 @@ export class Frame extends Component {
1516
worker: Worker;
1617
show_spinner = signal(true);
1718
id: string;
19+
abort: AbortController;
1820
constructor() {
1921
super();
2022

23+
this.abort = new AbortController();
24+
2125
document.title = connected_to.value;
2226

2327
this.id = crypto.randomUUID();
24-
this.worker = new Worker();
2528
navigator.serviceWorker.addEventListener("message", (e: MessageEvent) => {
2629
const msg = e.data as Message;
2730
if (msg.receiver_id === this.id) {
2831
this.worker.postMessage(msg);
2932
}
3033
});
3134

35+
this.startWorker();
36+
37+
// used by the app to detect a resumed app
38+
window.addEventListener("appResumed", () => {
39+
this.worker.terminate();
40+
this.startWorker();
41+
this.show_spinner.value = true;
42+
}, {signal: this.abort.signal});
43+
44+
// this is used by the app to close the remote connection via the native app menu.
45+
(window as any).close = () => {
46+
connected.value = false;
47+
connected_to.value = "";
48+
setAppNavigation();
49+
}
50+
51+
// this is used by the app to change location via the native app menu.
52+
(window as any).switchTo = (hash: string) => {
53+
const frame = document.getElementById("interface") as HTMLIFrameElement;
54+
const frame_window = frame.contentWindow;
55+
frame_window.location.hash = hash;
56+
}
57+
}
58+
59+
startWorker() {
60+
this.worker = new Worker();
3261
const message_event = (e: MessageEvent) => {
3362
if (typeof e.data === "string") {
3463
switch (e.data) {
@@ -136,6 +165,7 @@ export class Frame extends Component {
136165
componentWillUnmount() {
137166
this.worker.postMessage("close");
138167
document.title = "Remote Access";
168+
this.abort.abort();
139169
}
140170

141171
render() {

0 commit comments

Comments
 (0)