Add sw activation logic

This commit is contained in:
artur
2024-02-19 09:56:46 +03:00
parent 0f28c80a15
commit 8c77b10b60
4 changed files with 41 additions and 16 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "Nsec.app", "name": "Nsec.app - Nostr key management tool",
"short_name": "Nsec.app - Nostr key management tool", "short_name": "Nsec.app",
"start_url": ".", "start_url": ".",
"icons": [ "icons": [
{ {

View File

@@ -1,6 +1,6 @@
import { DbKey, dbi } from './modules/db' import { DbKey, dbi } from './modules/db'
import { useCallback, useEffect, useState } from 'react' import { useCallback, useEffect, useState } from 'react'
import { swicOnRender } from './modules/swic' import { swicOnReload, swicOnRender } from './modules/swic'
import { useAppDispatch } from './store/hooks/redux' import { useAppDispatch } from './store/hooks/redux'
import { setApps, setKeys, setPending, setPerms } from './store/reducers/content.slice' import { setApps, setKeys, setPending, setPerms } from './store/reducers/content.slice'
import AppRoutes from './routes/AppRoutes' import AppRoutes from './routes/AppRoutes'
@@ -77,6 +77,12 @@ function App() {
setRender((r) => r + 1) setRender((r) => r + 1)
}) })
// subscribe to service worker updates
swicOnReload(() => {
console.log('reload')
// FIXME show 'Please reload' badge at page top
})
return ( return (
<> <>
<AppRoutes /> <AppRoutes />

View File

@@ -144,13 +144,8 @@ export class NoauthBackend {
const self = this const self = this
swg.addEventListener('activate', (event) => { swg.addEventListener('activate', (event) => {
console.log('activate') console.log('activate new sw worker')
// swg.addEventListener('activate', event => event.waitUntil(swg.clients.claim())); this.reloadUI()
})
swg.addEventListener('install', (event) => {
console.log('install')
// swg.addEventListener('install', event => event.waitUntil(swg.skipWaiting()));
}) })
swg.addEventListener('push', (event) => { swg.addEventListener('push', (event) => {
@@ -1001,7 +996,7 @@ export class NoauthBackend {
} }
private async editName(npub: string, name: string) { private async editName(npub: string, name: string) {
const key = this.enckeys.find(k => k.npub == npub) const key = this.enckeys.find(k => k.npub === npub)
if (!key) throw new Error("Npub not found"); if (!key) throw new Error("Npub not found");
if (key.name) { if (key.name) {
await this.sendDeleteNameToServer(npub, key.name) await this.sendDeleteNameToServer(npub, key.name)
@@ -1015,7 +1010,7 @@ export class NoauthBackend {
} }
private async transferName(npub: string, name: string, newNpub: string) { private async transferName(npub: string, name: string, newNpub: string) {
const key = this.enckeys.find(k => k.npub == npub) const key = this.enckeys.find(k => k.npub === npub)
if (!key) throw new Error("Npub not found") if (!key) throw new Error("Npub not found")
if (!name) throw new Error("Empty name") if (!name) throw new Error("Empty name")
if (key.name !== name) throw new Error("Name changed, please reload") if (key.name !== name) throw new Error("Name changed, please reload")
@@ -1103,10 +1098,20 @@ export class NoauthBackend {
} }
} }
private async reloadUI() {
const clients = await this.swg.clients.matchAll({
includeUncontrolled: true,
})
console.log('reloadUI clients', clients.length)
for (const client of clients) {
client.postMessage({ result: 'reload' })
}
}
public async onPush(event: any) { public async onPush(event: any) {
console.log('push', { data: event.data }) console.log('push', { data: event.data })
// noop - we just need browser to launch this worker // noop - we just need browser to launch this worker
// FIXME use event.waitUntil and and unblock after we // FIXME use event.waitUntil and and unblock after we
// show a notification // show a notification to avoid annoying the browser
} }
} }

View File

@@ -1,10 +1,12 @@
// service-worker client interface // service-worker client interface,
// works on the frontend, not sw
import * as serviceWorkerRegistration from '../serviceWorkerRegistration' import * as serviceWorkerRegistration from '../serviceWorkerRegistration'
export let swr: ServiceWorkerRegistration | null = null export let swr: ServiceWorkerRegistration | null = null
const reqs = new Map<number, { ok: (r: any) => void; rej: (r: any) => void }>() const reqs = new Map<number, { ok: (r: any) => void; rej: (r: any) => void }>()
let nextReqId = 1 let nextReqId = 1
let onRender: (() => void) | null = null let onRender: (() => void) | null = null
let onReload: (() => void) | null = null
export async function swicRegister() { export async function swicRegister() {
serviceWorkerRegistration.register({ serviceWorkerRegistration.register({
@@ -13,8 +15,12 @@ export async function swicRegister() {
swr = registration swr = registration
}, },
onError(e) { onError(e) {
console.log(`error ${e}`) console.log('sw error', e)
}, },
onUpdate() {
// tell new SW that it should activate immediately
swr?.waiting?.postMessage({type: 'SKIP_WAITING'})
}
}) })
navigator.serviceWorker.ready.then((r) => { navigator.serviceWorker.ready.then((r) => {
@@ -37,7 +43,11 @@ function onMessage(data: any) {
console.log('SW message', id, result, error) console.log('SW message', id, result, error)
if (!id) { if (!id) {
if (onRender) onRender() if (result === 'reload') {
if (onReload) onReload()
} else {
if (onRender) onRender()
}
return return
} }
@@ -76,3 +86,7 @@ export async function swicCall(method: string, ...args: any[]) {
export function swicOnRender(cb: () => void) { export function swicOnRender(cb: () => void) {
onRender = cb onRender = cb
} }
export function swicOnReload(cb: () => void) {
onReload = cb
}