Mark all notifications as seen syncs all tabs

This commit is contained in:
Eduard Urbach 2018-03-20 18:27:57 +01:00
parent d6efc2b861
commit 24b0036617
3 changed files with 61 additions and 40 deletions

View File

@ -30,7 +30,16 @@ export async function markNotificationsAsSeen(arn: AnimeNotifier) {
}) })
// Update notification counter // Update notification counter
arn.notificationManager.update() if(!("serviceWorker" in navigator)) {
// If there is no service worker, update the counter on this tab
arn.notificationManager.update()
} else {
// If we have service worker support, broadcast the "notifications marked as seen" message to all open tabs
arn.serviceWorkerManager.postMessage({
type: "broadcast",
realType: "notifications marked as seen"
})
}
// Update notifications // Update notifications
arn.reloadContent() arn.reloadContent()

View File

@ -11,12 +11,10 @@ export class ServiceWorkerManager {
register() { register() {
if(!("serviceWorker" in navigator)) { if(!("serviceWorker" in navigator)) {
console.log("service worker not supported, skipping registration") console.warn("service worker not supported, skipping registration")
return return
} }
console.log("register service worker")
navigator.serviceWorker.register(this.uri).then(registration => { navigator.serviceWorker.register(this.uri).then(registration => {
// registration.update() // registration.update()
}) })
@ -74,6 +72,7 @@ export class ServiceWorkerManager {
switch(message.type) { switch(message.type) {
case "new notification": case "new notification":
case "notifications marked as seen":
this.arn.notificationManager.update() this.arn.notificationManager.update()
break break

View File

@ -198,12 +198,8 @@ class MyServiceWorker {
var payload = evt.data ? evt.data.json() : {} var payload = evt.data ? evt.data.json() : {}
// Notify all clients about the new notification so they can update their notification counter // Notify all clients about the new notification so they can update their notification counter
self.clients.matchAll().then(function(clientList) { this.broadcast({
for(let client of clientList) { type: "new notification"
client.postMessage(JSON.stringify({
type: "new notification"
}))
}
}) })
// Display the notification // Display the notification
@ -275,6 +271,17 @@ class MyServiceWorker {
}) })
} }
// Broadcast sends a message to all clients (open tabs etc.)
broadcast(msg: object) {
const msgText = JSON.stringify(msg)
self.clients.matchAll().then(function(clientList) {
for(let client of clientList) {
client.postMessage(msgText)
}
})
}
// installCache is called when the service worker is installed for the first time. // installCache is called when the service worker is installed for the first time.
installCache() { installCache() {
return caches.open(this.cache.version).then(cache => { return caches.open(this.cache.version).then(cache => {
@ -373,6 +380,12 @@ class MyClient {
case "loaded": case "loaded":
this.onDOMContentLoaded(message.url) this.onDOMContentLoaded(message.url)
break break
case "broadcast":
message.type = message.realType
delete message.realType
serviceWorker.broadcast(message)
break
} }
} }
@ -429,37 +442,37 @@ class MyClient {
// }) // })
} }
prefetchFullPage(url: string) { // prefetchFullPage(url: string) {
let fullPage = new Request(url.replace("/_/", "/")) // let fullPage = new Request(url.replace("/_/", "/"))
let fullPageRefresh = fetch(fullPage, { // let fullPageRefresh = fetch(fullPage, {
credentials: "same-origin" // credentials: "same-origin"
}).then(response => { // }).then(response => {
// Save the new version of the resource in the cache // // Save the new version of the resource in the cache
let cacheRefresh = caches.open(serviceWorker.cache.version).then(cache => { // let cacheRefresh = caches.open(serviceWorker.cache.version).then(cache => {
return cache.put(fullPage, response) // return cache.put(fullPage, response)
}) // })
CACHEREFRESH.set(fullPage.url, cacheRefresh) // CACHEREFRESH.set(fullPage.url, cacheRefresh)
return response // return response
}) // })
// Save in map // // Save in map
serviceWorker.reloads.set(fullPage.url, fullPageRefresh) // serviceWorker.reloads.set(fullPage.url, fullPageRefresh)
} // }
async reloadContent(url: string) { // async reloadContent(url: string) {
let cacheRefresh = CACHEREFRESH.get(url) // let cacheRefresh = CACHEREFRESH.get(url)
if(cacheRefresh) { // if(cacheRefresh) {
await cacheRefresh // await cacheRefresh
} // }
return this.postMessage({ // return this.postMessage({
type: "new content", // type: "new content",
url // url
}) // })
} // }
// async reloadPage(url: string) { // async reloadPage(url: string) {
// let networkFetch = serviceWorker.reloads.get(url.replace("/_/", "/")) // let networkFetch = serviceWorker.reloads.get(url.replace("/_/", "/"))
@ -474,11 +487,11 @@ class MyClient {
// }) // })
// } // }
reloadStyles() { // reloadStyles() {
return this.postMessage({ // return this.postMessage({
type: "reload styles" // type: "reload styles"
}) // })
} // }
// Map of clients // Map of clients
static idToClient = new Map<string, MyClient>() static idToClient = new Map<string, MyClient>()