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 4772c97
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { AccountsStore } from '@polkadot/extension-base/stores';
import { keyring } from '@polkadot/ui-keyring';
import { assert } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { runHeartbeat, startHeartbeat } from './heartbeat';

// setup the notification (same a FF default background, white text)
withErrorLog(() => chrome.action.setBadgeBackgroundColor({ color: '#d90000' }));
Expand Down Expand Up @@ -77,6 +78,27 @@ 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 83 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 runHeartbeat()

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;
}

chrome.alarms.create('heartbeat', {

Check failure on line 95 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
periodInMinutes: 0.5
});
});

startHeartbeat();

Check failure on line 100 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;

Check failure on line 10 in packages/extension/src/heartbeat.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

'NodeJS' is not defined

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

Check failure on line 13 in packages/extension/src/heartbeat.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

'chrome' is not defined
}

/**
* 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 async function startHeartbeat () {

Check failure on line 21 in packages/extension/src/heartbeat.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Async function 'startHeartbeat' has no 'await' expression
// Run the heartbeat once at service worker startup.
runHeartbeat().then(() => {

Check failure on line 23 in packages/extension/src/heartbeat.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
// Then again every 20 seconds.
heartbeatInterval = setInterval(runHeartbeat, 20 * 1000);

Check failure on line 25 in packages/extension/src/heartbeat.ts

View workflow job for this annotation

GitHub Actions / pr (lint)

Promise returned in function argument where a void return was expected
});
}

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

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

0 comments on commit 4772c97

Please sign in to comment.