Skip to content

Commit

Permalink
working on background sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
andorsk committed May 7, 2024
1 parent 1cfdb37 commit 62aedd9
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ app
manifest-checksum*

settings.gradle

**/public/workbox-*.js
**/public/sw.js
**/public/worker-*.js
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const webpack = require("webpack");

const withPWA = require("next-pwa")({
dest: "public",
customWorkerDir: "src/workers",
});

/** @type {import('next').NextConfig} */
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@mui/material": "^5.15.16",
"@mui/material-nextjs": "^5.15.11",
"@web5/api": "^0.9.2",
"babel-loader": "^9.1.3",
"crypto-browserify": "^3.12.0",
"formik": "^2.4.6",
"next": "14.2.3",
Expand Down
104 changes: 104 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 15 additions & 14 deletions src/components/timer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ class Timer {
this._timerId = crypto.randomUUID();
this.tickHandler = tickHandler;
this._intervalId = null;

// Start a background sync to update timer state
}

async sendTimerMessage() {
if ("serviceWorker" in navigator && "SyncManager" in window) {
const registration = await navigator.serviceWorker.ready;
try {
// @ts-ignore
await registration.sync.register("timer-sync");
} catch {
console.log("Background Sync could not be registered!");
}
}
}

get totalTime() {
Expand All @@ -51,27 +65,14 @@ class Timer {
if (!this._isPlaying && this._remainingTime > 0) {
this._isPlaying = true;
this._intervalId = setInterval(() => {
this.sendTimerMessage();
if (this._remainingTime > 0) {
this._remainingTime -= 10; // Decrease by 10 milliseconds each tick
this.update();
} else {
this.setFinished();
}
}, 10); // Tick every 10 milliseconds

// Start a background sync to update timer state
if ("serviceWorker" in navigator && "SyncManager" in window) {
navigator.serviceWorker.ready
.then((registration) => {
console.log("registering sync");
// TODO: Remove
// @ts-ignore
return registration.sync.register("timerSync");
})
.catch((err) => {
console.error("Background sync registration failed:", err);
});
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/workers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

self.addEventListener("sync", (event) => {
console.log("got sync message", event);
// @ts-ignore
if (event.tag === "timer-sync") {
console.log("got timer sync message");
const logMessage = (e: any) => {
console.log("got timer", e);
};
// @ts-ignore
event.waitUntil(logMessage);
}
});

0 comments on commit 62aedd9

Please sign in to comment.