@jskit-ai/push-web 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - A tapped notification opens its page in the app's open window even when the browser refuses to focus it (no user activation): `/jskit-sw.js` posts `jskit-push-tap` all the same, where the refusal ended the tap.
5
6
  - `/jskit-sw.js` shows the icon and badge image an app's worker sets in `self.JSKIT_PUSH = { icon, badge }` before loading it, and sets the app icon's count from a notification's `badge` (`setAppBadge`; 0 clears it) where the browser can. `enable({ label })` names the device. `isCurrent(device)` answers whether a device from `GET /api/push` is this one. A browser subscribed before the app used push-web is found by its push manager subscription, so it shows as on.
6
7
  - The notifications section (`PushSettingsSection`, its state in `usePushSettings`) keeps its form on screen and the person's unsaved edits when the device switch is turned or a device is removed; only the first load shows the skeleton. A failed save reads "Your notification settings weren't saved. Try again." and a failed removal "{label} wasn't removed. Try again." (they read "didn't load"), in the shell's snackbar rather than an alert pushing the form down. Each device row and the empty list carry the notifications icon. A quiet hours end the server refuses shows beside that field. Remove sends one request however often it is pressed, and Remove, Save ("Saving…") and the device switch stay focusable while they run (`v-pending`).
7
8
  - The manifest marks `push.core` install-time only (`jskit.capabilities.installRequires`): the client calls push-core's routes and no server feature of push-web requires it, so start-up no longer logs it as a requirement no feature has.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/push-web",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -67,7 +67,8 @@ function onNotificationClick(event) {
67
67
  const windows = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
68
68
  const open = windows.find((client) => new URL(client.url).origin === self.location.origin);
69
69
  if (open) {
70
- await open.focus();
70
+ // A browser may refuse to focus the window (no user activation): its page still opens there.
71
+ await open.focus().catch(() => {});
71
72
  open.postMessage({ type: "jskit-push-tap", url: page, action: event.action || "", data: data || {} });
72
73
  return;
73
74
  }
@@ -7,7 +7,7 @@ import vm from "node:vm";
7
7
  // push shows, with the app's icon and badge images, and the count the app's icon shows.
8
8
  const SOURCE = readFileSync(new URL("../src/client/serviceWorker/jskit-sw.js", import.meta.url), "utf8");
9
9
 
10
- function worker({ settings, appBadge = true } = {}) {
10
+ function worker({ settings, appBadge = true, windows = [] } = {}) {
11
11
  const shown = [];
12
12
  const badges = [];
13
13
  const listeners = new Map();
@@ -15,6 +15,8 @@ function worker({ settings, appBadge = true } = {}) {
15
15
  ...(settings ? { JSKIT_PUSH: settings } : {}),
16
16
  addEventListener: (type, listener) => listeners.set(type, listener),
17
17
  registration: { showNotification: async (title, options) => shown.push([title, options]) },
18
+ location: { origin: "https://app.example" },
19
+ clients: { matchAll: async () => windows, openWindow: async () => {} },
18
20
  navigator: appBadge ? { setAppBadge: async (count) => badges.push(count), clearAppBadge: async () => badges.push("cleared") } : {}
19
21
  };
20
22
  vm.runInNewContext(SOURCE, { self, URL });
@@ -23,7 +25,12 @@ function worker({ settings, appBadge = true } = {}) {
23
25
  listeners.get("push")({ data: { json: () => message }, waitUntil: (promise) => waits.push(promise) });
24
26
  await Promise.all(waits);
25
27
  }
26
- return { push, shown, badges };
28
+ async function tap(data, action = "") {
29
+ const waits = [];
30
+ listeners.get("notificationclick")({ action, notification: { close() {}, data }, waitUntil: (promise) => waits.push(promise) });
31
+ await Promise.all(waits);
32
+ }
33
+ return { push, tap, shown, badges };
27
34
  }
28
35
 
29
36
  test("a push shows the app's own icon and badge images when its worker names them, and none otherwise", async () => {
@@ -46,3 +53,15 @@ test("a push's numeric badge sets the app icon's count, 0 clears it, and a brows
46
53
  await browser.push({ title: "Ada", badge: 3 });
47
54
  assert.equal(browser.shown.length, 1, "the notification still shows");
48
55
  });
56
+
57
+ test("a tap posts its page to the app's open window even when the browser refuses to focus it", async () => {
58
+ const posted = [];
59
+ const open = {
60
+ url: "https://app.example/home",
61
+ focus: async () => { throw new Error("Not allowed to focus a window."); },
62
+ postMessage: (message) => posted.push(message)
63
+ };
64
+ await worker({ windows: [open] }).tap({ url: "/chats/7", data: { chat: 7 } }, "reply");
65
+ // Made inside the worker's own context, whose Object is another one: compared by value.
66
+ assert.deepEqual(JSON.parse(JSON.stringify(posted)), [{ type: "jskit-push-tap", url: "/chats/7", action: "reply", data: { chat: 7 } }]);
67
+ });