Skip to content

Commit e913cf8

Browse files
committed
实现 WakeUpPing
1 parent eccc118 commit e913cf8

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

src/offscreen.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import LoggerCore from "./app/logger/core";
22
import MessageWriter from "./app/logger/message_writer";
33
import { OffscreenManager } from "./app/service/offscreen";
44
import { ServiceWorkerClientMessage } from "@Packages/message/window_message";
5+
import { startRepetitivePing } from "./pkg/utils/wakeup-ping";
56

67
function main() {
78
// 通过postMessage与SW通信,支持结构化克隆(Blob等)
@@ -15,6 +16,7 @@ function main() {
1516
// 初始化管理器
1617
const manager = new OffscreenManager(swPostMessage);
1718
manager.initManager();
19+
startRepetitivePing();
1820
}
1921

2022
main();

src/pkg/utils/wakeup-ping.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const PING_INTERVAL_MS = 14_225;
2+
3+
/**
4+
* scheduler 用于后台排程:Chrome 94+, Firefox 142+
5+
* @link https://developer.mozilla.org/en-US/docs/Web/API/Scheduler/postTask
6+
*/
7+
const nativeScheduler =
8+
//@ts-ignore
9+
typeof scheduler !== "undefined" && typeof scheduler?.postTask === "function" && scheduler;
10+
11+
// 高效的 BroadcastChannel 通讯:service worker 和 offscreen 共用同一通道
12+
const channel = new BroadcastChannel("custom-ping");
13+
14+
export const startRepetitivePing = () => {
15+
if (typeof frameElement === "object" && typeof document === "object" && document) {
16+
let counter = 0;
17+
let isMutationPending = false;
18+
19+
const customPingHandler = (e: Event) => {
20+
chrome.storage.session.set({ persistentWakeup: `${e.timeStamp}` });
21+
};
22+
23+
const pingNode = document.createComment("0");
24+
25+
const incrementCounter = () => {
26+
if (!isMutationPending) {
27+
isMutationPending = true;
28+
counter = counter > 8 ? 1 : counter + 1;
29+
pingNode.data = `${counter}`;
30+
}
31+
};
32+
33+
pingNode.addEventListener("custom-ping", customPingHandler);
34+
35+
const pingTask = async () => {
36+
channel.postMessage({});
37+
incrementCounter();
38+
};
39+
40+
const mutationObserver = new MutationObserver(() => {
41+
if (isMutationPending) {
42+
isMutationPending = false;
43+
if (nativeScheduler) {
44+
nativeScheduler.postTask(pingTask, { priority: "background", delay: PING_INTERVAL_MS });
45+
} else {
46+
setTimeout(pingTask, PING_INTERVAL_MS);
47+
}
48+
}
49+
});
50+
mutationObserver.observe(pingNode, { characterData: true });
51+
incrementCounter();
52+
}
53+
};
54+
55+
export const listenWakeupPing = (onWakeupPing: (...args: any) => any) => {
56+
chrome.storage.session.onChanged.addListener((obj) => {
57+
// consume persistentWakeup
58+
if (typeof obj.persistentWakeup !== "undefined") {
59+
onWakeupPing();
60+
}
61+
});
62+
channel.onmessage = (e) => {
63+
chrome.storage.session.set({ persistentWakeup: `${e.timeStamp}` });
64+
};
65+
};

src/service_worker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { MessageQueue } from "@Packages/message/message_queue";
88
import { ServiceWorkerMessageSend } from "@Packages/message/window_message";
99
import migrate, { migrateChromeStorage } from "./app/migrate";
1010
import { cleanInvalidKeys } from "./app/repo/resource";
11+
import { listenWakeupPing } from "./pkg/utils/wakeup-ping";
1112

1213
migrate();
1314
migrateChromeStorage();
@@ -59,6 +60,10 @@ async function setupOffscreenDocument() {
5960
}
6061
}
6162

63+
export const onWakeupPing = () => {
64+
console.debug("onWakeupPing"); // 不用记录在系统日志
65+
};
66+
6267
function main() {
6368
cleanInvalidKeys();
6469
// 初始化管理器
@@ -77,6 +82,7 @@ function main() {
7782
manager.initManager();
7883
// 初始化沙盒环境
7984
setupOffscreenDocument();
85+
listenWakeupPing(onWakeupPing);
8086
}
8187

8288
main();

0 commit comments

Comments
 (0)