mirror of
https://github.com/fregante/GhostText.git
synced 2024-11-28 05:44:01 +03:00
Avoid auto-disconnect when inactive (#318)
This commit is contained in:
parent
3fb653eea1
commit
745604460d
@ -97,12 +97,17 @@ chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => {
|
|||||||
|
|
||||||
console.log('will open socket');
|
console.log('will open socket');
|
||||||
const socket = new WebSocket('ws://localhost:' + WebSocketPort);
|
const socket = new WebSocket('ws://localhost:' + WebSocketPort);
|
||||||
|
let interval; // eslint-disable-line prefer-const -- Ur rong
|
||||||
await Promise.race([
|
await Promise.race([
|
||||||
oneEvent(socket, 'open'),
|
oneEvent(socket, 'open'),
|
||||||
oneEvent(socket, 'error'),
|
oneEvent(socket, 'error'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const onSocketClose = () => port.postMessage({close: true});
|
const onSocketClose = () => {
|
||||||
|
port.postMessage({close: true});
|
||||||
|
clearInterval(interval);
|
||||||
|
};
|
||||||
|
|
||||||
socket.addEventListener('close', onSocketClose);
|
socket.addEventListener('close', onSocketClose);
|
||||||
socket.addEventListener('message', event => port.postMessage({message: event.data}));
|
socket.addEventListener('message', event => port.postMessage({message: event.data}));
|
||||||
socket.addEventListener('error', event => console.error('error!', event));
|
socket.addEventListener('error', event => console.error('error!', event));
|
||||||
@ -114,8 +119,15 @@ chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => {
|
|||||||
port.onDisconnect.addListener(() => {
|
port.onDisconnect.addListener(() => {
|
||||||
socket.removeEventListener('close', onSocketClose);
|
socket.removeEventListener('close', onSocketClose);
|
||||||
socket.close();
|
socket.close();
|
||||||
|
clearInterval(interval);
|
||||||
});
|
});
|
||||||
port.postMessage({ready: true});
|
port.postMessage({ready: true});
|
||||||
|
|
||||||
|
interval = setInterval(() => {
|
||||||
|
// Keep-alive for MV3 https://github.com/fregante/GhostText/issues/317
|
||||||
|
port.postMessage({ping: true});
|
||||||
|
console.log('ping');
|
||||||
|
}, 5000);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
function handleMessages({code, count}, {tab}) {
|
function handleMessages({code, count}, {tab}) {
|
||||||
|
Loading…
Reference in New Issue
Block a user