Skip to content

Commit 0db4456

Browse files
remove delay from notifications (#1109)
1 parent 33eee96 commit 0db4456

File tree

6 files changed

+11
-33
lines changed

6 files changed

+11
-33
lines changed

docs/src/notifications-page.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ func (p *notificationsPage) testNotification(ctx app.Context, e app.Event) {
110110
// Path: "/notifications",
111111
// },
112112
// },
113-
// Delay: time.Second * 2,
114113
})
115114
}
116115

docs/web/app.wasm

-101 Bytes
Binary file not shown.

docs/web/documents/reference.html

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ <h2 id="pkg-constants">Constants</h2>
16431643

16441644
<pre>const (
16451645
<span class="comment">// The default template used to generate app-worker.js.</span>
1646-
<span id="DefaultAppWorkerJS">DefaultAppWorkerJS</span> = &#34;// -----------------------------------------------------------------------------\n// PWA\n// -----------------------------------------------------------------------------\nconst cacheName = \&#34;app-\&#34; + \&#34;{{.Version}}\&#34;;\nconst resourcesToCache = {{.ResourcesToCache}};\n\nself.addEventListener(\&#34;install\&#34;, async (event) =&gt; {\n try {\n console.log(\&#34;installing app worker {{.Version}}\&#34;);\n await installWorker();\n await self.skipWaiting();\n } catch (error) {\n console.error(\&#34;error during installation:\&#34;, error);\n }\n});\n\nasync function installWorker() {\n const cache = await caches.open(cacheName);\n await cache.addAll(resourcesToCache);\n}\n\nself.addEventListener(\&#34;activate\&#34;, async (event) =&gt; {\n try {\n await deletePreviousCaches(); // Await cache cleanup\n await self.clients.claim(); // Ensure the service worker takes control of the clients\n console.log(\&#34;app worker {{.Version}} is activated\&#34;);\n } catch (error) {\n console.error(\&#34;error during activation:\&#34;, error);\n }\n});\n\nasync function deletePreviousCaches() {\n const keys = await caches.keys();\n await Promise.all(\n keys.map(async (key) =&gt; {\n if (key !== cacheName) {\n try {\n console.log(\&#34;deleting\&#34;, key, \&#34;cache\&#34;);\n await caches.delete(key);\n } catch (err) {\n console.error(\&#34;deleting\&#34;, key, \&#34;cache failed:\&#34;, err);\n }\n }\n })\n );\n}\n\nself.addEventListener(\&#34;fetch\&#34;, (event) =&gt; {\n event.respondWith(fetchWithCache(event.request));\n});\n\nasync function fetchWithCache(request) {\n const cachedResponse = await caches.match(request);\n if (cachedResponse) {\n return cachedResponse;\n }\n return await fetch(request);\n}\n\n// -----------------------------------------------------------------------------\n// Push Notifications\n// -----------------------------------------------------------------------------\nself.addEventListener(\&#34;push\&#34;, (event) =&gt; {\n if (!event.data || !event.data.text()) {\n return;\n }\n\n const notification = JSON.parse(event.data.text());\n if (!notification) {\n return;\n }\n\n event.waitUntil(\n showNotification(self.registration, notification)\n );\n});\n\nself.addEventListener(\&#34;message\&#34;, (event) =&gt; {\n const msg = event.data;\n if (!msg || msg.type !== \&#34;goapp:notify\&#34;) {\n return;\n }\n\n event.waitUntil(\n showNotification(self.registration, msg.options)\n );\n});\n\nasync function showNotification(registration, notification) {\n const title = notification.title || \&#34;\&#34;;\n let delay = notification.delay || 0;\n\n let actions = [];\n for (let i in notification.actions) {\n const action = notification.actions[i];\n actions.push({\n action: action.action,\n path: action.path,\n });\n delete action.path;\n }\n\n notification.data = notification.data || {};\n notification.data.goapp = {\n path: notification.path,\n actions: actions,\n };\n delete notification.title;\n delete notification.path;\n delete notification.delay;\n\n if (delay &gt; 0) {\n delay = Math.floor(delay / 1e6);\n await sleep(delay);\n }\n await registration.showNotification(title, notification);\n}\n\nfunction sleep(ms) {\n return new Promise((resolve) =&gt; setTimeout(resolve, ms));\n}\n\nself.addEventListener(\&#34;notificationclick\&#34;, (event) =&gt; {\n event.notification.close();\n\n const notification = event.notification;\n let path = notification.data.goapp.path;\n\n for (let i in notification.data.goapp.actions) {\n const action = notification.data.goapp.actions[i];\n if (action.action === event.action) {\n path = action.path;\n break;\n }\n }\n\n event.waitUntil(\n clients\n .matchAll({\n type: \&#34;window\&#34;,\n })\n .then((clientList) =&gt; {\n for (var i = 0; i &lt; clientList.length; i++) {\n let client = clientList[i];\n if (\&#34;focus\&#34; in client) {\n client.focus();\n client.postMessage({\n goapp: {\n type: \&#34;notification\&#34;,\n path: path,\n },\n });\n return;\n }\n }\n\n if (clients.openWindow) {\n return clients.openWindow(path);\n }\n })\n );\n});&#34;
1646+
<span id="DefaultAppWorkerJS">DefaultAppWorkerJS</span> = &#34;// -----------------------------------------------------------------------------\n// PWA\n// -----------------------------------------------------------------------------\nconst cacheName = \&#34;app-\&#34; + \&#34;{{.Version}}\&#34;;\nconst resourcesToCache = {{.ResourcesToCache}};\n\nself.addEventListener(\&#34;install\&#34;, async (event) =&gt; {\n try {\n console.log(\&#34;installing app worker {{.Version}}\&#34;);\n await installWorker();\n await self.skipWaiting();\n } catch (error) {\n console.error(\&#34;error during installation:\&#34;, error);\n }\n});\n\nasync function installWorker() {\n const cache = await caches.open(cacheName);\n await cache.addAll(resourcesToCache);\n}\n\nself.addEventListener(\&#34;activate\&#34;, async (event) =&gt; {\n try {\n await deletePreviousCaches(); // Await cache cleanup\n await self.clients.claim(); // Ensure the service worker takes control of the clients\n console.log(\&#34;app worker {{.Version}} is activated\&#34;);\n } catch (error) {\n console.error(\&#34;error during activation:\&#34;, error);\n }\n});\n\nasync function deletePreviousCaches() {\n const keys = await caches.keys();\n await Promise.all(\n keys.map(async (key) =&gt; {\n if (key !== cacheName) {\n try {\n console.log(\&#34;deleting\&#34;, key, \&#34;cache\&#34;);\n await caches.delete(key);\n } catch (err) {\n console.error(\&#34;deleting\&#34;, key, \&#34;cache failed:\&#34;, err);\n }\n }\n })\n );\n}\n\nself.addEventListener(\&#34;fetch\&#34;, (event) =&gt; {\n event.respondWith(fetchWithCache(event.request));\n});\n\nasync function fetchWithCache(request) {\n const cachedResponse = await caches.match(request);\n if (cachedResponse) {\n return cachedResponse;\n }\n return await fetch(request);\n}\n\n// -----------------------------------------------------------------------------\n// Push Notifications\n// -----------------------------------------------------------------------------\nself.addEventListener(\&#34;push\&#34;, (event) =&gt; {\n if (!event.data || !event.data.text()) {\n return;\n }\n\n const notification = JSON.parse(event.data.text());\n if (!notification) {\n return;\n }\n\n event.waitUntil(\n showNotification(self.registration, notification)\n );\n});\n\nself.addEventListener(\&#34;message\&#34;, (event) =&gt; {\n const msg = event.data;\n if (!msg || msg.type !== \&#34;goapp:notify\&#34;) {\n return;\n }\n\n event.waitUntil(\n showNotification(self.registration, msg.options)\n );\n});\n\nasync function showNotification(registration, notification) {\n const title = notification.title || \&#34;\&#34;;\n\n let actions = [];\n for (let i in notification.actions) {\n const action = notification.actions[i];\n actions.push({\n action: action.action,\n path: action.path,\n });\n delete action.path;\n }\n\n notification.data = notification.data || {};\n notification.data.goapp = {\n path: notification.path,\n actions: actions,\n };\n delete notification.title;\n delete notification.path;\n\n await registration.showNotification(title, notification);\n}\n\nfunction sleep(ms) {\n return new Promise((resolve) =&gt; setTimeout(resolve, ms));\n}\n\nself.addEventListener(\&#34;notificationclick\&#34;, (event) =&gt; {\n event.notification.close();\n\n const notification = event.notification;\n let path = notification.data.goapp.path;\n\n for (let i in notification.data.goapp.actions) {\n const action = notification.data.goapp.actions[i];\n if (action.action === event.action) {\n path = action.path;\n break;\n }\n }\n\n event.waitUntil(\n clients\n .matchAll({\n type: \&#34;window\&#34;,\n })\n .then((clientList) =&gt; {\n for (var i = 0; i &lt; clientList.length; i++) {\n let client = clientList[i];\n if (\&#34;focus\&#34; in client) {\n client.focus();\n client.postMessage({\n goapp: {\n type: \&#34;notification\&#34;,\n path: path,\n },\n });\n return;\n }\n }\n\n if (clients.openWindow) {\n return clients.openWindow(path);\n }\n })\n );\n});&#34;
16471647
)</pre>
16481648

16491649

@@ -26221,7 +26221,7 @@ <h2 id="Navigator">type <a href="/src/github.com/maxence-charriere/go-app/v10/pk
2622126221

2622226222

2622326223

26224-
<h2 id="Notification">type <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=153:2377#L1">Notification</a>
26224+
<h2 id="Notification">type <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=145:2047#L1">Notification</a>
2622526225
<a class="permalink" href="#Notification">&#xb6;</a>
2622626226

2622726227

@@ -26275,13 +26275,6 @@ <h2 id="Notification">type <a href="/src/github.com/maxence-charriere/go-app/v10
2627526275
<span class="comment">// See: https://developer.mozilla.org/en-US/docs/Web/API/Vibration_API</span>
2627626276
Vibrate []<a href="/pkg/builtin/#int">int</a> `json:&#34;vibrate,omitempty&#34;`
2627726277

26278-
<span id="Notification.Delay"></span> <span class="comment">// Delay specifies how long to wait before displaying the notification.</span>
26279-
<span class="comment">//</span>
26280-
<span class="comment">// The duration is serialized as a Go time.Duration and interpreted</span>
26281-
<span class="comment">// client-side. Actual delivery time is best-effort and may be delayed</span>
26282-
<span class="comment">// further depending on browser and system constraints.</span>
26283-
Delay <a href="/pkg/time/">time</a>.<a href="/pkg/time/#Duration">Duration</a> `json:&#34;delay,omitempty&#34;`
26284-
2628526278
<span id="Notification.Actions"></span> <span class="comment">// Actions lists the available actions displayed within the notification.</span>
2628626279
Actions []<a href="#NotificationAction">NotificationAction</a> `json:&#34;actions,omitempty&#34;`
2628726280
}
@@ -26301,7 +26294,7 @@ <h2 id="Notification">type <a href="/src/github.com/maxence-charriere/go-app/v10
2630126294

2630226295

2630326296

26304-
<h2 id="NotificationAction">type <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=2454:2882#L60">NotificationAction</a>
26297+
<h2 id="NotificationAction">type <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=2124:2552#L52">NotificationAction</a>
2630526298
<a class="permalink" href="#NotificationAction">&#xb6;</a>
2630626299

2630726300

@@ -26338,7 +26331,7 @@ <h2 id="NotificationAction">type <a href="/src/github.com/maxence-charriere/go-a
2633826331

2633926332

2634026333

26341-
<h2 id="NotificationPermission">type <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=3464:3498#L93">NotificationPermission</a>
26334+
<h2 id="NotificationPermission">type <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=3134:3168#L85">NotificationPermission</a>
2634226335
<a class="permalink" href="#NotificationPermission">&#xb6;</a>
2634326336

2634426337

@@ -26379,7 +26372,7 @@ <h2 id="NotificationPermission">type <a href="/src/github.com/maxence-charriere/
2637926372

2638026373

2638126374

26382-
<h2 id="NotificationService">type <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=4180:4213#L112">NotificationService</a>
26375+
<h2 id="NotificationService">type <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=3850:3883#L104">NotificationService</a>
2638326376
<a class="permalink" href="#NotificationService">&#xb6;</a>
2638426377

2638526378

@@ -26401,7 +26394,7 @@ <h2 id="NotificationService">type <a href="/src/github.com/maxence-charriere/go-
2640126394

2640226395

2640326396

26404-
<h3 id="NotificationService.New">func (NotificationService) <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=5063:5111#L142">New</a>
26397+
<h3 id="NotificationService.New">func (NotificationService) <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=4733:4781#L134">New</a>
2640526398
<a class="permalink" href="#NotificationService.New">&#xb6;</a>
2640626399

2640726400

@@ -26414,7 +26407,7 @@ <h3 id="NotificationService.New">func (NotificationService) <a href="/src/github
2641426407

2641526408

2641626409

26417-
<h3 id="NotificationService.Permission">func (NotificationService) <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=4283:4347#L115">Permission</a>
26410+
<h3 id="NotificationService.Permission">func (NotificationService) <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=3953:4017#L107">Permission</a>
2641826411
<a class="permalink" href="#NotificationService.Permission">&#xb6;</a>
2641926412

2642026413

@@ -26427,7 +26420,7 @@ <h3 id="NotificationService.Permission">func (NotificationService) <a href="/src
2642726420

2642826421

2642926422

26430-
<h3 id="NotificationService.RequestPermission">func (NotificationService) <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=4617:4688#L125">RequestPermission</a>
26423+
<h3 id="NotificationService.RequestPermission">func (NotificationService) <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=4287:4358#L117">RequestPermission</a>
2643126424
<a class="permalink" href="#NotificationService.RequestPermission">&#xb6;</a>
2643226425

2643326426

@@ -26440,7 +26433,7 @@ <h3 id="NotificationService.RequestPermission">func (NotificationService) <a hre
2644026433

2644126434

2644226435

26443-
<h3 id="NotificationService.Subscribe">func (NotificationService) <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=5390:5485#L150">Subscribe</a>
26436+
<h3 id="NotificationService.Subscribe">func (NotificationService) <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=5060:5155#L142">Subscribe</a>
2644426437
<a class="permalink" href="#NotificationService.Subscribe">&#xb6;</a>
2644526438

2644626439

@@ -26457,7 +26450,7 @@ <h3 id="NotificationService.Subscribe">func (NotificationService) <a href="/src/
2645726450

2645826451

2645926452

26460-
<h2 id="NotificationSubscription">type <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=2963:3365#L76">NotificationSubscription</a>
26453+
<h2 id="NotificationSubscription">type <a href="/src/github.com/maxence-charriere/go-app/v10/pkg/app/notification.go?s=2633:3035#L68">NotificationSubscription</a>
2646126454
<a class="permalink" href="#NotificationSubscription">&#xb6;</a>
2646226455

2646326456

pkg/app/gen/app-worker.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ self.addEventListener("message", (event) => {
8888

8989
async function showNotification(registration, notification) {
9090
const title = notification.title || "";
91-
let delay = notification.delay || 0;
9291

9392
let actions = [];
9493
for (let i in notification.actions) {
@@ -107,12 +106,7 @@ async function showNotification(registration, notification) {
107106
};
108107
delete notification.title;
109108
delete notification.path;
110-
delete notification.delay;
111109

112-
if (delay > 0) {
113-
delay = Math.floor(delay / 1e6);
114-
await sleep(delay);
115-
}
116110
await registration.showNotification(title, notification);
117111
}
118112

pkg/app/notification.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package app
22

33
import (
44
"encoding/json"
5-
"time"
65

76
"github.com/maxence-charriere/go-app/v10/pkg/errors"
87
)
@@ -55,13 +54,6 @@ type Notification struct {
5554
// See: https://developer.mozilla.org/en-US/docs/Web/API/Vibration_API
5655
Vibrate []int `json:"vibrate,omitempty"`
5756

58-
// Delay specifies how long to wait before displaying the notification.
59-
//
60-
// The duration is serialized as a Go time.Duration and interpreted
61-
// client-side. Actual delivery time is best-effort and may be delayed
62-
// further depending on browser and system constraints.
63-
Delay time.Duration `json:"delay,omitempty"`
64-
6557
// Actions lists the available actions displayed within the notification.
6658
Actions []NotificationAction `json:"actions,omitempty"`
6759
}

0 commit comments

Comments
 (0)