BlueGit

projects-timfalken-com microchess

Skip turn notifications when a Microchess tab is already visible.

timfa Tim Falken · pushed by timfa

Authored 2026-09-11 · 2 months ago

Hash
b29d13deb4831484c2fc08496ab78710bb365060
Parent
49052a41
3 files changed +23 −9
web/README.md +1 −1
@@ -163,7 +163,7 @@ The replay footer’s chain icon copies a permanent link with an unguessable tok
163 163
164 164 ## Notifications and home-screen installation
165 165
166 **Account → Enable turn notifications** opts this browser/device in. Notifications cover a new turn (including accepted challenges, match links, matchmaking and rematches), incoming challenges and rematch requests. Enabling on another device adds that device; disabling affects only the current one. Sign-out removes the current browser's server registration. An existing browser subscription is associated with the current account after signing in or switching accounts. There is a limit of ten subscriptions per account.
166 **Account → Enable turn notifications** opts this browser/device in. Notifications cover a new turn (including accepted challenges, match links, matchmaking and rematches), incoming challenges and rematch requests. The service worker skips showing an OS notification when any Microchess tab in that browser is already visible; background/hidden tabs and other devices still notify. Enabling on another device adds that device; disabling affects only the current one. Sign-out removes the current browser's server registration. An existing browser subscription is associated with the current account after signing in or switching accounts. There is a limit of ten subscriptions per account.
167 167
168 168 Game actions collect notification events inside the game transaction and dispatch only after commit. There is no cronjob, persistent notification queue, polling-based notification scan or background worker. Matchmaking triggered by a state request also dispatches its newly created games. PHP closes the session before sending; with PHP-FPM it finishes the HTTP response first. Other SAPIs may wait for push delivery (two-second connection timeout, five-second request timeout). Delivery failures are logged without endpoints/keys and cannot roll back a move. Expired subscriptions are removed. Failed deliveries are not retried; notifications are best effort. Superseded game versions are skipped before sending, messages expire after one hour, and each game's topic/tag replaces older pending/displayed notifications.
169 169
@@ -163,7 +163,7 @@ The replay footer’s chain icon copies a permanent link with an unguessable tok
163
163
164## Notifications and home-screen installation
164## Notifications and home-screen installation
165
165
166**Account → Enable turn notifications** opts this browser/device in. Notifications cover a new turn (including accepted challenges, match links, matchmaking and rematches), incoming challenges and rematch requests. Enabling on another device adds that device; disabling affects only the current one. Sign-out removes the current browser's server registration. An existing browser subscription is associated with the current account after signing in or switching accounts. There is a limit of ten subscriptions per account.
166**Account → Enable turn notifications** opts this browser/device in. Notifications cover a new turn (including accepted challenges, match links, matchmaking and rematches), incoming challenges and rematch requests. The service worker skips showing an OS notification when any Microchess tab in that browser is already visible; background/hidden tabs and other devices still notify. Enabling on another device adds that device; disabling affects only the current one. Sign-out removes the current browser's server registration. An existing browser subscription is associated with the current account after signing in or switching accounts. There is a limit of ten subscriptions per account.
167
167
168Game actions collect notification events inside the game transaction and dispatch only after commit. There is no cronjob, persistent notification queue, polling-based notification scan or background worker. Matchmaking triggered by a state request also dispatches its newly created games. PHP closes the session before sending; with PHP-FPM it finishes the HTTP response first. Other SAPIs may wait for push delivery (two-second connection timeout, five-second request timeout). Delivery failures are logged without endpoints/keys and cannot roll back a move. Expired subscriptions are removed. Failed deliveries are not retried; notifications are best effort. Superseded game versions are skipped before sending, messages expire after one hour, and each game's topic/tag replaces older pending/displayed notifications.
168Game actions collect notification events inside the game transaction and dispatch only after commit. There is no cronjob, persistent notification queue, polling-based notification scan or background worker. Matchmaking triggered by a state request also dispatches its newly created games. PHP closes the session before sending; with PHP-FPM it finishes the HTTP response first. Other SAPIs may wait for push delivery (two-second connection timeout, five-second request timeout). Delivery failures are logged without endpoints/keys and cannot roll back a move. Expired subscriptions are removed. Failed deliveries are not retried; notifications are best effort. Superseded game versions are skipped before sending, messages expire after one hour, and each game's topic/tag replaces older pending/displayed notifications.
169
169
web/sw.js +12 −6
@@ -7,12 +7,18 @@ self.addEventListener('push', event =>
7 7 let data = {};
8 8 try { data = event.data?.json() || {}; } catch (_) {}
9 9 const game = Number.isSafeInteger(data.game) && data.game > 0 ? data.game : null;
10 event.waitUntil(self.registration.showNotification('Microchess', {
11 body: typeof data.body === 'string' ? data.body : 'A game is waiting for you.',
12 icon: new URL('assets/app-icon-192.png', self.registration.scope).href,
13 tag: game ? `game-${game}` : 'microchess',
14 data: { game }
15 }));
10 event.waitUntil((async () =>
11 {
12 // Skip OS notifications while the player is already looking at Microchess.
13 const windows = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
14 if (windows.some(client => client.visibilityState === 'visible')) return;
15 await self.registration.showNotification('Microchess', {
16 body: typeof data.body === 'string' ? data.body : 'A game is waiting for you.',
17 icon: new URL('assets/app-icon-192.png', self.registration.scope).href,
18 tag: game ? `game-${game}` : 'microchess',
19 data: { game }
20 });
21 })());
16 22 });
17 23 self.addEventListener('notificationclick', event =>
18 24 {
@@ -7,12 +7,18 @@ self.addEventListener('push', event =>
7 let data = {};
7 let data = {};
8 try { data = event.data?.json() || {}; } catch (_) {}
8 try { data = event.data?.json() || {}; } catch (_) {}
9 const game = Number.isSafeInteger(data.game) && data.game > 0 ? data.game : null;
9 const game = Number.isSafeInteger(data.game) && data.game > 0 ? data.game : null;
10 event.waitUntil(self.registration.showNotification('Microchess', {
10 event.waitUntil((async () =>
11 body: typeof data.body === 'string' ? data.body : 'A game is waiting for you.',
11 {
12 icon: new URL('assets/app-icon-192.png', self.registration.scope).href,
12 // Skip OS notifications while the player is already looking at Microchess.
13 tag: game ? `game-${game}` : 'microchess',
13 const windows = await self.clients.matchAll({ type: 'window', includeUncontrolled: true });
14 data: { game }
14 if (windows.some(client => client.visibilityState === 'visible')) return;
15 }));
15 await self.registration.showNotification('Microchess', {
16 body: typeof data.body === 'string' ? data.body : 'A game is waiting for you.',
17 icon: new URL('assets/app-icon-192.png', self.registration.scope).href,
18 tag: game ? `game-${game}` : 'microchess',
19 data: { game }
20 });
21 })());
16});
22});
17self.addEventListener('notificationclick', event =>
23self.addEventListener('notificationclick', event =>
18{
24{
web/tests/pwa.mjs +10 −2
@@ -21,6 +21,14 @@ await dispatch('push', { data: { json: () => ({ game: 42, body: 'Your turn again
21 21 assert.equal(shown[0].tag, 'game-42');
22 22 assert.equal(shown[0].icon, scope + 'assets/app-icon-192.png');
23 23 assert.equal(shown[0].data.game, 42);
24 windows = [{ url: scope, visibilityState: 'visible' }];
25 await dispatch('push', { data: { json: () => ({ game: 99, body: 'Should stay silent.' }) } });
26 assert.equal(shown.length, 1, 'Visible Microchess tab suppresses OS notification');
27 windows = [{ url: scope, visibilityState: 'hidden' }];
28 await dispatch('push', { data: { json: () => ({ game: 100, body: 'Background tab still notifies.' }) } });
29 assert.equal(shown.length, 2, 'Hidden tab still shows a notification');
30 assert.equal(shown[1].tag, 'game-100');
31 windows = [];
24 32 let closed = false;
25 33 const notification = { data: { game: 42 }, close: () => { closed = true; } };
26 34 await dispatch('notificationclick', { notification });
@@ -35,7 +43,7 @@ windows = [{ url: 'https://example.test/other-app/', navigate: async () => { thr
35 43 await dispatch('notificationclick', { notification });
36 44 assert.equal(opened.length, 2, 'Another application is not navigated');
37 45 await dispatch('push', { data: { json: () => { throw Error('Malformed payload'); } } });
38 assert.equal(shown[1].body, 'A game is waiting for you.');
46 assert.equal(shown[2].body, 'A game is waiting for you.');
39 47 await dispatch('notificationclick', { notification: { data: { game: 'https://evil.test/' }, close: () => {} } });
40 48 assert.equal(opened.at(-1), scope, 'Notification data cannot redirect outside app');
41 console.log('PWA: notification display, subdirectory links, window reuse and malformed payload checks passed.');
49 console.log('PWA: notification display, visible-tab suppression, subdirectory links, window reuse and malformed payload checks passed.');
@@ -21,6 +21,14 @@ await dispatch('push', { data: { json: () => ({ game: 42, body: 'Your turn again
21assert.equal(shown[0].tag, 'game-42');
21assert.equal(shown[0].tag, 'game-42');
22assert.equal(shown[0].icon, scope + 'assets/app-icon-192.png');
22assert.equal(shown[0].icon, scope + 'assets/app-icon-192.png');
23assert.equal(shown[0].data.game, 42);
23assert.equal(shown[0].data.game, 42);
24windows = [{ url: scope, visibilityState: 'visible' }];
25await dispatch('push', { data: { json: () => ({ game: 99, body: 'Should stay silent.' }) } });
26assert.equal(shown.length, 1, 'Visible Microchess tab suppresses OS notification');
27windows = [{ url: scope, visibilityState: 'hidden' }];
28await dispatch('push', { data: { json: () => ({ game: 100, body: 'Background tab still notifies.' }) } });
29assert.equal(shown.length, 2, 'Hidden tab still shows a notification');
30assert.equal(shown[1].tag, 'game-100');
31windows = [];
24let closed = false;
32let closed = false;
25const notification = { data: { game: 42 }, close: () => { closed = true; } };
33const notification = { data: { game: 42 }, close: () => { closed = true; } };
26await dispatch('notificationclick', { notification });
34await dispatch('notificationclick', { notification });
@@ -35,7 +43,7 @@ windows = [{ url: 'https://example.test/other-app/', navigate: async () => { thr
35await dispatch('notificationclick', { notification });
43await dispatch('notificationclick', { notification });
36assert.equal(opened.length, 2, 'Another application is not navigated');
44assert.equal(opened.length, 2, 'Another application is not navigated');
37await dispatch('push', { data: { json: () => { throw Error('Malformed payload'); } } });
45await dispatch('push', { data: { json: () => { throw Error('Malformed payload'); } } });
38assert.equal(shown[1].body, 'A game is waiting for you.');
46assert.equal(shown[2].body, 'A game is waiting for you.');
39await dispatch('notificationclick', { notification: { data: { game: 'https://evil.test/' }, close: () => {} } });
47await dispatch('notificationclick', { notification: { data: { game: 'https://evil.test/' }, close: () => {} } });
40assert.equal(opened.at(-1), scope, 'Notification data cannot redirect outside app');
48assert.equal(opened.at(-1), scope, 'Notification data cannot redirect outside app');
41console.log('PWA: notification display, subdirectory links, window reuse and malformed payload checks passed.');
49console.log('PWA: notification display, visible-tab suppression, subdirectory links, window reuse and malformed payload checks passed.');