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

chore: add heartbeat #1423

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 22 additions & 0 deletions packages/extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/* global chrome */

import '@polkadot/extension-inject/crossenv';

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

View workflow job for this annotation

GitHub Actions / pr (lint)

Run autofix to sort these imports!

import type { RequestSignatures, TransportRequestMessage } from '@polkadot/extension-base/background/types';

Expand All @@ -15,6 +15,7 @@
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 @@
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'];
}