Skip to content

Commit

Permalink
issue #153 - keep service worker alive during the execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Manvel committed Jun 8, 2024
1 parent abd8bd4 commit 357f550
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/js/background/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ async function playProject() {
}
else if(cba.instructArray.length) {
setBadgeText("play");
const playingTabId = await cba.getPlayingTabId();
if (playingTabId) {
void browser.tabs.sendMessage(playingTabId, {action: "startKeepAlive"});
}
const [instruction] = cba.instructArray.splice(0, 1);
cba.playingActionIndex = (cba.defInstructArray.length - cba.instructArray.length) - 1;

Expand Down
71 changes: 71 additions & 0 deletions src/js/cs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

require("./record");
require("./actions");
const {sendRpcMessage, addRpcListener, removeRpcListener} = require("../rpc/client");
/**
* @typedef {import("../rpc/types").RpcHandler} RpcHandler
*/

browser.runtime.onMessage.addListener((request) => {
if(request.action == "highlight") {
Expand All @@ -28,6 +32,9 @@ browser.runtime.onMessage.addListener((request) => {
else if(request.action == "unHighlight") {
return setHighlight(request.selector, false);
}
else if (request.action == "startKeepAlive") {
keepAlive();
}
});

function setHighlight(query, highlight = true)
Expand All @@ -39,3 +46,67 @@ function setHighlight(query, highlight = true)
target.style["outline-width"] = highlight ? "1px" : "";
}
}

/**
* @returns {Promise<import("../background/CBA").State | null>}
*/
function getCbaState() {
return new Promise((resolve) => {
const uuid = uuidv4();
const onTimeout = () => {
removeRpcListener(handler);
resolve(null);
}
/** @type {RpcHandler} */
const handler = (state) => {
if (state.msgType === "GetStateResponse" && state.id === uuid) {
removeRpcListener(handler);
resolve(state.state);
}
clearTimeout(onTimeout);
};
setTimeout(onTimeout, 1000);
addRpcListener(handler);
sendRpcMessage({msgType: "GetState", id: uuid});
});
}

function uuidv4() {
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}

/**
* While a CBA project is being executed Service worker should be kept alive. It
* has been observed that service worker is being killed by the browser on
* `click-update` and `update` actions, breaking our user workflow which
* involves project with repeating run and relying on page update triggered by
* the user .
*/
let keepAlivePort;
let postTimer;
async function keepAlive() {
if (postTimer) {
return;
}
const state = await getCbaState();
if (state === null) {
return;
}
if (state && (state.allowPlay || state.paused)) {
keepAlivePort = chrome.runtime.connect({name: 'keepAlive'});
postTimer = window.setTimeout(keepAlive, 15000);
} else {
if (keepAlivePort) {
keepAlivePort.disconnect();
keepAlivePort = null;
}
if (postTimer) {
window.clearTimeout(postTimer);
postTimer = null;
}
}
}

keepAlive();
3 changes: 3 additions & 0 deletions src/js/rpc/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
const listeners = [];

browser.runtime.onConnect.addListener((port) => {
if (port.name === "keepAlive") {
return;
}
if (port.name !== "rpcPort") throw new Error("Unknown port.");
port.onMessage.addListener(async(/** @type {RpcMessages} */ msg) => {
for (const listener of listeners) {
Expand Down

0 comments on commit 357f550

Please sign in to comment.