Skip to content

Commit

Permalink
chore: add heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanleecode committed Jul 29, 2024
1 parent ba295ec commit 9af8d7f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@ chrome.tabs.onRemoved.addListener(() => {
getActiveTabs();
});

// add heartbeat using alarms to prevent the service worker from being killed as
// much as possible.
chrome.alarms.onAlarm.addListener(async () => {

Check failure on line 82 in packages/extension/src/background.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Promise returned in function argument where a void return was expected
await chrome.storage.local.set({ 'last-heartbeat': new Date().getTime() });
})

Check failure on line 84 in packages/extension/src/background.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon

chrome.runtime.onInstalled.addListener(({ reason }) => {
if (
reason !== chrome.runtime.OnInstalledReason.INSTALL &&
reason !== chrome.runtime.OnInstalledReason.UPDATE
)
return

Check failure on line 91 in packages/extension/src/background.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Expected { after 'if' condition

Check failure on line 91 in packages/extension/src/background.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon

chrome.alarms.create("heartbeat", {

Check failure on line 93 in packages/extension/src/background.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check failure on line 93 in packages/extension/src/background.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Strings must use singlequote
periodInMinutes: 0.5,

Check failure on line 94 in packages/extension/src/background.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Unexpected trailing comma
})

Check failure on line 95 in packages/extension/src/background.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon
})

Check failure on line 96 in packages/extension/src/background.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Missing semicolon

startHeartbeat()

Check failure on line 98 in packages/extension/src/background.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

// initial setup
cryptoWaitReady()
.then((): void => {
Expand Down
39 changes: 39 additions & 0 deletions packages/extension/src/heartbeat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2019-2024 @polkadot/extension authors & contributors
// SPDX-License-Identifier: Apache-2.0

/**
* Heartbeat functions as described by google.
*
* @see https://developer.chrome.com/docs/extensions/develop/migrate/to-service-workers#keep_a_service_worker_alive_continuously
*/

let heartbeatInterval: string | number | NodeJS.Timeout | undefined;

async function runHeartbeat() {
await chrome.storage.local.set({ 'last-heartbeat': new Date().getTime() });
}

/**
* 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.
*/
async function startHeartbeat() {
// Run the heartbeat once at service worker startup.
runHeartbeat().then(() => {
// Then again every 20 seconds.
heartbeatInterval = setInterval(runHeartbeat, 20 * 1000);
});
}

async function stopHeartbeat() {
clearInterval(heartbeatInterval);
}

/**
* Returns the last heartbeat stored in extension storage, or undefined if
* the heartbeat has never run before.
*/
async function getLastHeartbeat() {
return (await chrome.storage.local.get('last-heartbeat'))['last-heartbeat'];
}

0 comments on commit 9af8d7f

Please sign in to comment.