Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 3222] Extension - Recheck time of stop background on Firefox browser #3321

Open
wants to merge 2 commits into
base: subwallet-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/extension-koni/src/helper/ActionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { MessageTypes, RequestSignatures, TransportRequestMessage } from '@subwallet/extension-base/background/types';
import { PORT_CONTENT, PORT_EXTENSION } from '@subwallet/extension-base/defaults';
import { SWHandler } from '@subwallet/extension-base/koni/background/handlers';
import { sendMessage } from '@subwallet/extension-base/page';
import { isFirefox } from '@subwallet/extension-base/utils';
import { createPromiseHandler } from '@subwallet/extension-base/utils/promise';
import { startHeartbeat, stopHeartbeat } from '@subwallet/extension-koni/helper/HeartBeat';

Expand Down Expand Up @@ -92,7 +94,9 @@ export class ActionHandler {

if (!this.isActive) {
this.isActive = true;
startHeartbeat();
startHeartbeat(async () => {
await (isFirefox ? sendMessage('pub(ping)', null) : chrome.storage.local.set({ 'last-heartbeat': new Date().getTime() }));
});
this.mainHandler && await this.mainHandler.state.wakeup();
this.waitActiveHandler.resolve(true);
}
Expand Down
42 changes: 33 additions & 9 deletions packages/extension-koni/src/helper/HeartBeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

// These code from https://developer.chrome.com/docs/extensions/develop/migrate/to-service-workers#keep-sw-alive

import { isFirefox } from '@subwallet/extension-base/utils';

/**
* Tracks when a service worker was last alive and extends the service worker
* lifetime by writing the current time to extension storage every 20 seconds.
Expand All @@ -13,31 +15,53 @@

let heartbeatInterval: NodeJS.Timer | undefined;

async function runHeartbeat () {
await chrome.storage.local.set({ 'last-heartbeat': new Date().getTime() });
async function runHeartbeat (cb: () => Promise<void>) {
if (isFirefox) {
await chrome.alarms.create('keep-loaded-alarm', {
periodInMinutes: 0.3
});

chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'keep-loaded-alarm') {
cb().catch(() => console.error('Failed to load alarms'));
}
});
} else {
await cb();
}
}

/**
* Starts the heartbeat interval which keeps the service worker alive. Call
* this sparingly when you are doing work which requires persistence, and call
* stopHeartbeat once that work is complete.
*/
export function startHeartbeat () {
export function startHeartbeat (cb: () => Promise<void>) {
// Run the heartbeat once at service worker startup.
if (heartbeatInterval) {
clearInterval(heartbeatInterval);
}

runHeartbeat().then(() => {
// Then again every 20 seconds.
heartbeatInterval = setInterval(() => {
runHeartbeat().catch(console.error);
}, 20 * 1000);
runHeartbeat(cb).then(() => {
if (!isFirefox) {
heartbeatInterval = setInterval(() => {
runHeartbeat(cb).catch(console.error);
}, 20 * 1000);
}
}).catch(console.error);
}

export function stopHeartbeat () {
clearInterval(heartbeatInterval);
if (isFirefox) {
chrome.alarms.clear('keep-loaded-alarm', function (wasCleared) {
if (wasCleared) {
console.log('Alarm was cleared');
}
});
} else {
clearInterval(heartbeatInterval);
}

heartbeatInterval = undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/koni-ci-mv3-firefox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function updateManifest() {
delete manifest.web_accessible_resources[0].use_dynamic_url;
}
manifest.permissions.push("activeTab");
manifest = {...manifest, host_permissions : ["<all_urls>"], optional_permissions : ["activeTab"]};
manifest = {...manifest, host_permissions : ["<all_urls>"], optional_permissions : ["activeTab", "alarms"]};

if(manifest.content_scripts && manifest.content_scripts.length > 0) {
manifest.content_scripts[0].js.push("injectGlobal.js");
Expand Down
Loading