-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba295ec
commit 4772c97
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
export 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. | ||
*/ | ||
export async function startHeartbeat () { | ||
// Run the heartbeat once at service worker startup. | ||
runHeartbeat().then(() => { | ||
// Then again every 20 seconds. | ||
heartbeatInterval = setInterval(runHeartbeat, 20 * 1000); | ||
}); | ||
} | ||
|
||
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']; | ||
} |