@inglorious/web 6.29.0 → 6.30.0
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/README.md +31 -2255
- package/package.json +2 -2
- package/src/auth/index.js +23 -21
- package/src/decorators/limit.js +67 -0
- package/src/decorators/with-debounce/index.js +6 -57
- package/src/decorators/with-throttle/index.js +12 -67
- package/src/device/keyboard/index.js +58 -21
- package/src/mount.js +7 -4
- package/src/native/app-links/index.js +44 -30
- package/src/native/back-button/index.js +40 -21
- package/src/native/live-update/index.js +104 -92
- package/src/native/system-bars/index.js +63 -33
- package/src/native/update/index.js +37 -38
- package/src/pwa/index.js +67 -3
- package/src/pwa/update.js +10 -20
- package/types/auth.d.ts +1 -10
- package/types/device/keyboard.d.ts +26 -6
- package/types/mount.d.ts +11 -5
- package/types/native/app-links.d.ts +22 -14
- package/types/native/back-button.d.ts +22 -12
- package/types/native/live-update.d.ts +25 -32
- package/types/native/system-bars.d.ts +24 -8
- package/types/native/update.d.ts +12 -25
- package/types/pwa.d.ts +11 -20
- package/src/pwa/handlers.js +0 -17
|
@@ -1,123 +1,120 @@
|
|
|
1
1
|
import { isNative } from "../index.js"
|
|
2
2
|
|
|
3
|
-
const DEFAULT_ID = "liveUpdate"
|
|
4
3
|
const ONE_HOUR_MS = 3_600_000
|
|
5
4
|
const NO_MIN_BUILD = 0
|
|
6
5
|
const RESUME_EVENT = "resume"
|
|
7
6
|
const PAUSE_EVENT = "pause"
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
|
-
* The type that
|
|
11
|
-
* code inside the binary; a live update replaces that code
|
|
12
|
-
* stores,
|
|
13
|
-
*
|
|
9
|
+
* The type that keeps a native app on its latest published web code. A native
|
|
10
|
+
* app carries its web code inside the binary; a live update replaces that code
|
|
11
|
+
* without the stores, through `LiveUpdate` from
|
|
12
|
+
* `@capawesome/capacitor-live-update`.
|
|
14
13
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
14
|
+
* Once created, it confirms that the running bundle works, so the plugin
|
|
15
|
+
* doesn't roll it back. Then, at launch, every `checkEveryMs` and on each
|
|
16
|
+
* return to the foreground, it reads the manifest at `manifestUrl` and
|
|
17
|
+
* downloads a bundle that is wanted (`isLiveUpdateWanted`) and not blocked by
|
|
18
|
+
* an earlier rollback. A downloaded bundle sets `isUpdateReady` and
|
|
19
|
+
* `pendingRelease`, is applied the next time the app goes to the background,
|
|
20
|
+
* and `liveUpdateApply` moves over to it at once. Does nothing outside a
|
|
21
|
+
* native app, and without `manifestUrl` or `release`: a development build
|
|
22
|
+
* must not update itself.
|
|
23
|
+
*
|
|
24
|
+
* @param {Object} config
|
|
25
|
+
* @param {Object} config.plugin `LiveUpdate` from `@capawesome/capacitor-live-update`.
|
|
26
|
+
* @param {Object} [config.app] `App` from `@capacitor/app`.
|
|
27
|
+
* @param {string} [config.manifestUrl]
|
|
28
|
+
* @param {number} [config.release] The release of the running web code.
|
|
29
|
+
* @param {number} [config.checkEveryMs]
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* const types = {
|
|
33
|
+
* LiveUpdate: createLiveUpdate({
|
|
34
|
+
* plugin: LiveUpdate,
|
|
35
|
+
* app: App,
|
|
36
|
+
* manifestUrl,
|
|
37
|
+
* release: Number(import.meta.env.VITE_LIVE_UPDATE_RELEASE),
|
|
38
|
+
* }),
|
|
39
|
+
* }
|
|
40
|
+
* const entities = { liveUpdate: { type: "LiveUpdate" } }
|
|
17
41
|
*/
|
|
18
|
-
export function createLiveUpdate({
|
|
42
|
+
export function createLiveUpdate({
|
|
43
|
+
plugin,
|
|
44
|
+
app,
|
|
45
|
+
manifestUrl,
|
|
46
|
+
release,
|
|
47
|
+
checkEveryMs = ONE_HOUR_MS,
|
|
48
|
+
}) {
|
|
49
|
+
const watches = new Map()
|
|
50
|
+
|
|
19
51
|
return {
|
|
20
|
-
create(entity) {
|
|
52
|
+
create(entity, _, api) {
|
|
21
53
|
entity.isUpdateReady ??= false
|
|
54
|
+
entity.pendingRelease ??= null
|
|
55
|
+
if (!isNative() || !app || !manifestUrl || !release) return
|
|
56
|
+
|
|
57
|
+
const { id } = entity
|
|
58
|
+
watches.get(id)?.()
|
|
59
|
+
watches.set(id, watch(id, api))
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
destroy(entity) {
|
|
63
|
+
watches.get(entity.id)?.()
|
|
64
|
+
watches.delete(entity.id)
|
|
22
65
|
},
|
|
23
66
|
|
|
24
|
-
liveUpdateReady(entity) {
|
|
67
|
+
liveUpdateReady(entity, pendingRelease) {
|
|
25
68
|
entity.isUpdateReady = true
|
|
69
|
+
entity.pendingRelease = pendingRelease
|
|
26
70
|
},
|
|
27
71
|
|
|
28
72
|
liveUpdateApply() {
|
|
29
73
|
plugin.reload()
|
|
30
74
|
},
|
|
31
75
|
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Whether a published bundle should replace the running one: it must be newer
|
|
36
|
-
* than what runs, and the installed build must be one it was made for.
|
|
37
|
-
*
|
|
38
|
-
* @param {{ release: number, minBuild?: number } | null} manifest
|
|
39
|
-
* @param {{ release: number, build: number }} installed
|
|
40
|
-
*/
|
|
41
|
-
export function isLiveUpdateWanted(manifest, { release, build }) {
|
|
42
|
-
return (
|
|
43
|
-
Number.isFinite(manifest?.release) &&
|
|
44
|
-
manifest.release > release &&
|
|
45
|
-
build >= (manifest.minBuild ?? NO_MIN_BUILD)
|
|
46
|
-
)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Keeps a native app on its latest published web code. Confirms at once that
|
|
51
|
-
* the running bundle works, so the plugin doesn't roll it back. Then, at launch,
|
|
52
|
-
* every `checkEveryMs` and on each return to the foreground, reads the
|
|
53
|
-
* manifest at `manifestUrl` and downloads a bundle that is wanted
|
|
54
|
-
* (`isLiveUpdateWanted`) and not blocked by an earlier rollback. A downloaded
|
|
55
|
-
* bundle is applied the next time the app goes to the background, and
|
|
56
|
-
* announced as `#${id}:liveUpdateReady` meanwhile. Does nothing outside a
|
|
57
|
-
* native app.
|
|
58
|
-
*
|
|
59
|
-
* @param {Object} options
|
|
60
|
-
* @param {Object} options.plugin `LiveUpdate` from
|
|
61
|
-
* `@capawesome/capacitor-live-update`.
|
|
62
|
-
* @param {Object} options.app `App` from `@capacitor/app`.
|
|
63
|
-
* @param {{ notify: (type: string, payload?: unknown) => void }} [options.store]
|
|
64
|
-
* @param {string} options.manifestUrl
|
|
65
|
-
* @param {number} options.release The release of the running web code.
|
|
66
|
-
* @param {string} [options.id]
|
|
67
|
-
* @param {number} [options.checkEveryMs]
|
|
68
|
-
* @returns {() => Promise<void>} Stops watching.
|
|
69
|
-
*/
|
|
70
|
-
export function watchLiveUpdate({
|
|
71
|
-
plugin,
|
|
72
|
-
app,
|
|
73
|
-
store,
|
|
74
|
-
manifestUrl,
|
|
75
|
-
release,
|
|
76
|
-
id = DEFAULT_ID,
|
|
77
|
-
checkEveryMs = ONE_HOUR_MS,
|
|
78
|
-
}) {
|
|
79
|
-
if (!isNative()) return async () => {}
|
|
80
76
|
|
|
81
|
-
|
|
82
|
-
|
|
77
|
+
function watch(id, api) {
|
|
78
|
+
let isChecking = false
|
|
83
79
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
80
|
+
plugin.ready().catch(() => {})
|
|
81
|
+
check()
|
|
82
|
+
const interval = setInterval(check, checkEveryMs)
|
|
83
|
+
const listeners = [
|
|
84
|
+
app.addListener(RESUME_EVENT, check),
|
|
85
|
+
app.addListener(PAUSE_EVENT, apply),
|
|
86
|
+
]
|
|
91
87
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
88
|
+
return function stop() {
|
|
89
|
+
clearInterval(interval)
|
|
90
|
+
for (const listener of listeners) {
|
|
91
|
+
listener.then((handle) => handle.remove())
|
|
92
|
+
}
|
|
97
93
|
}
|
|
98
|
-
}
|
|
99
94
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
95
|
+
function apply() {
|
|
96
|
+
if (api.getEntity(id)?.pendingRelease != null) {
|
|
97
|
+
plugin.reload().catch(() => {})
|
|
98
|
+
}
|
|
99
|
+
}
|
|
103
100
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
101
|
+
async function check() {
|
|
102
|
+
if (isChecking) return
|
|
103
|
+
isChecking = true
|
|
104
|
+
try {
|
|
105
|
+
const manifest = await latest()
|
|
106
|
+
const { build } = await app.getInfo()
|
|
107
|
+
if (!isLiveUpdateWanted(manifest, { release, build: Number(build) })) {
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
if (api.getEntity(id)?.pendingRelease === manifest.release) return
|
|
111
|
+
await prepare(manifest)
|
|
112
|
+
api.notify(`#${id}:liveUpdateReady`, manifest.release)
|
|
113
|
+
} catch {
|
|
114
|
+
// Offline, or a bundle that fails its checks: the running code stays.
|
|
115
|
+
} finally {
|
|
116
|
+
isChecking = false
|
|
112
117
|
}
|
|
113
|
-
if (pending === manifest.release) return
|
|
114
|
-
await prepare(manifest)
|
|
115
|
-
pending = manifest.release
|
|
116
|
-
store?.notify(`#${id}:liveUpdateReady`)
|
|
117
|
-
} catch {
|
|
118
|
-
// Offline, or a bundle that fails its checks: the running code stays.
|
|
119
|
-
} finally {
|
|
120
|
-
isChecking = false
|
|
121
118
|
}
|
|
122
119
|
}
|
|
123
120
|
|
|
@@ -138,3 +135,18 @@ export function watchLiveUpdate({
|
|
|
138
135
|
await plugin.setNextBundle({ bundleId })
|
|
139
136
|
}
|
|
140
137
|
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Whether a published bundle should replace the running one: it must be newer
|
|
141
|
+
* than what runs, and the installed build must be one it was made for.
|
|
142
|
+
*
|
|
143
|
+
* @param {{ release: number, minBuild?: number } | null} manifest
|
|
144
|
+
* @param {{ release: number, build: number }} installed
|
|
145
|
+
*/
|
|
146
|
+
export function isLiveUpdateWanted(manifest, { release, build }) {
|
|
147
|
+
return (
|
|
148
|
+
Number.isFinite(manifest?.release) &&
|
|
149
|
+
manifest.release > release &&
|
|
150
|
+
build >= (manifest.minBuild ?? NO_MIN_BUILD)
|
|
151
|
+
)
|
|
152
|
+
}
|
|
@@ -7,47 +7,48 @@ const DARK_QUERY = "(prefers-color-scheme: dark)"
|
|
|
7
7
|
const WATCHED_ATTRIBUTES = ["class", "style", "data-theme"]
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
* The bars follow the device's light or dark mode, so a light-only
|
|
12
|
-
* phone in dark mode gets white icons on a white page. This reads the
|
|
13
|
-
* `color-scheme` instead, which every Inglorious UI theme sets, and
|
|
14
|
-
* when a theme class changes.
|
|
15
|
-
*
|
|
10
|
+
* The type that keeps the status bar and navigation bar readable on the app's
|
|
11
|
+
* own colours. The bars follow the device's light or dark mode, so a light-only
|
|
12
|
+
* app on a phone in dark mode gets white icons on a white page. This reads the
|
|
13
|
+
* app's `color-scheme` instead, which every Inglorious UI theme sets, and
|
|
14
|
+
* follows it when a theme class changes. The entity's `style` is the bars'
|
|
15
|
+
* current style. An app that follows the device keeps the device's style. Does
|
|
16
|
+
* nothing outside a native app.
|
|
16
17
|
*
|
|
17
|
-
* @param {Object}
|
|
18
|
-
* @
|
|
18
|
+
* @param {Object} config
|
|
19
|
+
* @param {Object} config.plugin `SystemBars` from `@capacitor/core`.
|
|
19
20
|
*
|
|
20
21
|
* @example
|
|
21
|
-
*
|
|
22
|
+
* import { SystemBars } from "@capacitor/core"
|
|
23
|
+
* const types = { SystemBars: createSystemBars({ plugin: SystemBars }) }
|
|
24
|
+
* const entities = { systemBars: { type: "SystemBars" } }
|
|
22
25
|
*/
|
|
23
|
-
export function
|
|
24
|
-
|
|
26
|
+
export function createSystemBars({ plugin }) {
|
|
27
|
+
const watches = new Map()
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
current = style
|
|
31
|
-
systemBars.setStyle({ style })
|
|
32
|
-
}
|
|
29
|
+
return {
|
|
30
|
+
create(entity, _, api) {
|
|
31
|
+
entity.style ??= null
|
|
32
|
+
if (!isNative()) return
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
const { id } = entity
|
|
35
|
+
watches.get(id)?.()
|
|
36
|
+
watches.set(
|
|
37
|
+
id,
|
|
38
|
+
watch((style) => api.notify(`#${id}:systemBarsStyleChange`, style)),
|
|
39
|
+
)
|
|
40
|
+
},
|
|
35
41
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
attributes: true,
|
|
41
|
-
attributeFilter: WATCHED_ATTRIBUTES,
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const media = window.matchMedia?.(DARK_QUERY)
|
|
46
|
-
media?.addEventListener("change", update)
|
|
42
|
+
destroy(entity) {
|
|
43
|
+
watches.get(entity.id)?.()
|
|
44
|
+
watches.delete(entity.id)
|
|
45
|
+
},
|
|
47
46
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
systemBarsStyleChange(entity, style) {
|
|
48
|
+
if (entity.style === style) return
|
|
49
|
+
entity.style = style
|
|
50
|
+
plugin.setStyle({ style })
|
|
51
|
+
},
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
|
|
@@ -68,6 +69,35 @@ export function styleFor(colorScheme) {
|
|
|
68
69
|
return DEVICE
|
|
69
70
|
}
|
|
70
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Follows the app's `color-scheme`, calling `onChange` with the bars' style
|
|
74
|
+
* now and whenever a theme class or the device's mode changes.
|
|
75
|
+
* @param {(style: string) => void} onChange
|
|
76
|
+
* @returns {() => void} Stops following.
|
|
77
|
+
*/
|
|
78
|
+
function watch(onChange) {
|
|
79
|
+
const update = () => onChange(styleFor(appColorScheme()))
|
|
80
|
+
|
|
81
|
+
update()
|
|
82
|
+
|
|
83
|
+
const observer = new MutationObserver(update)
|
|
84
|
+
for (const element of [document.documentElement, document.body]) {
|
|
85
|
+
if (element) {
|
|
86
|
+
observer.observe(element, {
|
|
87
|
+
attributes: true,
|
|
88
|
+
attributeFilter: WATCHED_ATTRIBUTES,
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const media = window.matchMedia?.(DARK_QUERY)
|
|
93
|
+
media?.addEventListener("change", update)
|
|
94
|
+
|
|
95
|
+
return function stop() {
|
|
96
|
+
observer.disconnect()
|
|
97
|
+
media?.removeEventListener("change", update)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
71
101
|
function appColorScheme() {
|
|
72
102
|
const element = document.body ?? document.documentElement
|
|
73
103
|
return getComputedStyle(element).colorScheme ?? ""
|
|
@@ -2,7 +2,6 @@ import { handleAsync } from "@inglorious/store/async"
|
|
|
2
2
|
|
|
3
3
|
import { isNative } from "../index.js"
|
|
4
4
|
|
|
5
|
-
const DEFAULT_ID = "nativeUpdate"
|
|
6
5
|
const RESUME_EVENT = "resume"
|
|
7
6
|
|
|
8
7
|
/**
|
|
@@ -12,24 +11,46 @@ const RESUME_EVENT = "resume"
|
|
|
12
11
|
* sets `isUpdateRequired`, which the app renders as a page with nothing but
|
|
13
12
|
* a way to the store (`nativeUpdateApply`, opening `storeUrl`).
|
|
14
13
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
14
|
+
* Given `App` from `@capacitor/app` as `plugin`, it checks the installed build
|
|
15
|
+
* at launch and each time the app comes back to the foreground; otherwise the
|
|
16
|
+
* app sends `nativeUpdateCheck({ platform, build })` itself. A failed check
|
|
17
|
+
* keeps the last answer: the gate never closes or opens on a guess. Does
|
|
18
|
+
* nothing outside a native app.
|
|
17
19
|
*
|
|
18
20
|
* The `adapter` says where the minimum lives: `requirement(platform)`
|
|
19
21
|
* resolves to `{ minBuild, storeUrl }`, or `null` for no minimum.
|
|
20
22
|
*
|
|
21
23
|
* @param {Object} config
|
|
22
24
|
* @param {Object} config.adapter
|
|
25
|
+
* @param {Object} [config.plugin] `App` from `@capacitor/app`.
|
|
23
26
|
*
|
|
24
27
|
* @example
|
|
25
|
-
*
|
|
26
|
-
*
|
|
28
|
+
* import { App } from "@capacitor/app"
|
|
29
|
+
* const types = {
|
|
30
|
+
* NativeUpdate: [createNativeUpdate({ adapter, plugin: App }), template],
|
|
31
|
+
* }
|
|
32
|
+
* const entities = { nativeUpdate: { type: "NativeUpdate" } }
|
|
27
33
|
*/
|
|
28
|
-
export function createNativeUpdate({ adapter }) {
|
|
34
|
+
export function createNativeUpdate({ adapter, plugin }) {
|
|
35
|
+
const listeners = new Map()
|
|
36
|
+
|
|
29
37
|
return {
|
|
30
|
-
create(entity) {
|
|
38
|
+
create(entity, _, api) {
|
|
31
39
|
entity.isUpdateRequired ??= false
|
|
32
40
|
entity.storeUrl ??= null
|
|
41
|
+
if (!plugin || !isNative()) return
|
|
42
|
+
|
|
43
|
+
const { id } = entity
|
|
44
|
+
const check = () => checkBuild(plugin, id, api)
|
|
45
|
+
|
|
46
|
+
check()
|
|
47
|
+
listeners.set(id, plugin.addListener(RESUME_EVENT, check))
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
destroy(entity) {
|
|
51
|
+
const listener = listeners.get(entity.id)
|
|
52
|
+
listeners.delete(entity.id)
|
|
53
|
+
listener?.then((handle) => handle.remove())
|
|
33
54
|
},
|
|
34
55
|
|
|
35
56
|
...handleAsync("nativeUpdateCheck", {
|
|
@@ -49,37 +70,15 @@ export function createNativeUpdate({ adapter }) {
|
|
|
49
70
|
}
|
|
50
71
|
}
|
|
51
72
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
*/
|
|
62
|
-
export function watchNativeUpdate(app, store, { id = DEFAULT_ID } = {}) {
|
|
63
|
-
if (!isNative()) return async () => {}
|
|
64
|
-
|
|
65
|
-
check()
|
|
66
|
-
const listener = app.addListener(RESUME_EVENT, check)
|
|
67
|
-
|
|
68
|
-
return async function stop() {
|
|
69
|
-
const handle = await listener
|
|
70
|
-
await handle.remove()
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async function check() {
|
|
74
|
-
try {
|
|
75
|
-
const { build } = await app.getInfo()
|
|
76
|
-
store.notify(`#${id}:nativeUpdateCheck`, {
|
|
77
|
-
platform: globalThis.Capacitor.getPlatform(),
|
|
78
|
-
build: Number(build),
|
|
79
|
-
})
|
|
80
|
-
} catch {
|
|
81
|
-
// An unreadable build is no reason to block anyone.
|
|
82
|
-
}
|
|
73
|
+
async function checkBuild(plugin, id, api) {
|
|
74
|
+
try {
|
|
75
|
+
const { build } = await plugin.getInfo()
|
|
76
|
+
api.notify(`#${id}:nativeUpdateCheck`, {
|
|
77
|
+
platform: globalThis.Capacitor.getPlatform(),
|
|
78
|
+
build: Number(build),
|
|
79
|
+
})
|
|
80
|
+
} catch {
|
|
81
|
+
// An unreadable build is no reason to block anyone.
|
|
83
82
|
}
|
|
84
83
|
}
|
|
85
84
|
|
package/src/pwa/index.js
CHANGED
|
@@ -1,5 +1,69 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { isNative } from "../native/index.js"
|
|
2
|
+
import { applyPwaUpdate, watchPwaUpdate } from "./update.js"
|
|
2
3
|
|
|
3
|
-
export { applyPwaUpdate
|
|
4
|
+
export { applyPwaUpdate } from "./update.js"
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
const ONE_HOUR_MS = 3_600_000
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The type that keeps an installed PWA on its latest version. Updates are not
|
|
10
|
+
* optional: a downloaded version is applied on its own wherever nobody is
|
|
11
|
+
* mid-task, at boot, before anything is touched, and each time the app leaves
|
|
12
|
+
* or comes back to the foreground. A version that lands while someone is using
|
|
13
|
+
* the app only sets `isUpdateReady`, so the app can offer to move over at once
|
|
14
|
+
* with `pwaUpdateApply`; the next trip to the background moves over regardless.
|
|
15
|
+
*
|
|
16
|
+
* It checks for a new version every `checkEveryMs` and on each return to the
|
|
17
|
+
* foreground: a phone suspends an installed PWA rather than closing it, so the
|
|
18
|
+
* browser's own check on navigation may never come. It works with the service
|
|
19
|
+
* worker that `vite-plugin-pwa` generates, registered with `registerType:
|
|
20
|
+
* "prompt"`, and does nothing in a native app or without a service worker.
|
|
21
|
+
*
|
|
22
|
+
* @param {Object} [config]
|
|
23
|
+
* @param {number} [config.checkEveryMs] Defaults to one hour.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* registerSW() // from "virtual:pwa-register"
|
|
27
|
+
* const types = { PwaUpdate: createPwaUpdate() }
|
|
28
|
+
* const entities = { pwaUpdate: { type: "PwaUpdate" } }
|
|
29
|
+
*/
|
|
30
|
+
export function createPwaUpdate({ checkEveryMs = ONE_HOUR_MS } = {}) {
|
|
31
|
+
const watches = new Map()
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
create(entity, _, api) {
|
|
35
|
+
entity.isUpdateReady ??= false
|
|
36
|
+
const serviceWorker = globalThis.navigator?.serviceWorker
|
|
37
|
+
if (isNative() || !serviceWorker) return
|
|
38
|
+
|
|
39
|
+
const { id } = entity
|
|
40
|
+
watches.get(id)?.()
|
|
41
|
+
let isStopped = false
|
|
42
|
+
watches.set(id, () => {
|
|
43
|
+
isStopped = true
|
|
44
|
+
})
|
|
45
|
+
serviceWorker.ready.then((registration) => {
|
|
46
|
+
if (isStopped) return
|
|
47
|
+
const stop = watchPwaUpdate(
|
|
48
|
+
registration,
|
|
49
|
+
() => api.notify(`#${id}:pwaUpdateReady`),
|
|
50
|
+
{ checkEveryMs },
|
|
51
|
+
)
|
|
52
|
+
watches.set(id, stop)
|
|
53
|
+
})
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
destroy(entity) {
|
|
57
|
+
watches.get(entity.id)?.()
|
|
58
|
+
watches.delete(entity.id)
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
pwaUpdateReady(entity) {
|
|
62
|
+
entity.isUpdateReady = true
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
pwaUpdateApply() {
|
|
66
|
+
applyPwaUpdate()
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/pwa/update.js
CHANGED
|
@@ -1,32 +1,22 @@
|
|
|
1
1
|
const ONE_HOUR_MS = 3_600_000
|
|
2
2
|
const TAKEOVER_TIMEOUT_MS = 3_000
|
|
3
|
-
const DEFAULT_ID = "pwaUpdate"
|
|
4
3
|
const VISIBILITY_EVENT = "visibilitychange"
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
|
-
* Keeps an installed PWA on its latest version
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* app is only announced, as `#${id}:pwaUpdateReady`, so the app can offer to
|
|
12
|
-
* move over at once; the next trip to the background moves over regardless.
|
|
6
|
+
* Keeps an installed PWA on its latest version: applies a waiting version at
|
|
7
|
+
* boot and on each trip to or from the background, checks for a new one every
|
|
8
|
+
* `checkEveryMs` and on each return to the foreground, and calls `onReady`
|
|
9
|
+
* when a version lands mid-session.
|
|
13
10
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @param {ServiceWorkerRegistration} registration What `registerSW` from
|
|
19
|
-
* `virtual:pwa-register` hands to `onRegisteredSW`.
|
|
20
|
-
* @param {{ notify: (type: string, payload?: unknown) => void }} [store]
|
|
21
|
-
* Where to announce a mid-session version. Without one, updates are applied
|
|
22
|
-
* silently.
|
|
23
|
-
* @param {{ id?: string, checkEveryMs?: number }} [options]
|
|
11
|
+
* @param {ServiceWorkerRegistration} registration
|
|
12
|
+
* @param {() => void} [onReady]
|
|
13
|
+
* @param {{ checkEveryMs?: number }} [options]
|
|
24
14
|
* @returns {() => void} Stops watching.
|
|
25
15
|
*/
|
|
26
16
|
export function watchPwaUpdate(
|
|
27
17
|
registration,
|
|
28
|
-
|
|
29
|
-
{
|
|
18
|
+
onReady,
|
|
19
|
+
{ checkEveryMs = ONE_HOUR_MS } = {},
|
|
30
20
|
) {
|
|
31
21
|
const hadController = Boolean(navigator.serviceWorker?.controller)
|
|
32
22
|
let announced = null
|
|
@@ -65,7 +55,7 @@ export function watchPwaUpdate(
|
|
|
65
55
|
function announce(worker) {
|
|
66
56
|
if (!hadController || isApplying || announced === worker) return
|
|
67
57
|
announced = worker
|
|
68
|
-
|
|
58
|
+
onReady?.()
|
|
69
59
|
}
|
|
70
60
|
|
|
71
61
|
function watchInstalls() {
|
package/types/auth.d.ts
CHANGED
|
@@ -104,6 +104,7 @@ type Handler<TPayload = void> = (
|
|
|
104
104
|
|
|
105
105
|
export type AuthType = {
|
|
106
106
|
create(entity: AuthEntity): void
|
|
107
|
+
destroy(entity: AuthEntity): void
|
|
107
108
|
sessionChange: Handler<AuthUser | null>
|
|
108
109
|
emailSubmit: Handler<string>
|
|
109
110
|
signInSubmit: Handler<string>
|
|
@@ -168,13 +169,3 @@ export type AuthType = {
|
|
|
168
169
|
|
|
169
170
|
/** The type behind a sign-in screen, holding the signed-in `user`. */
|
|
170
171
|
export declare function createAuth(config: AuthConfig): AuthType
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Keeps the entity's `user` in step with the backend's session.
|
|
174
|
-
* @returns A function that stops watching.
|
|
175
|
-
*/
|
|
176
|
-
export declare function watchAuth(
|
|
177
|
-
adapter: Pick<AuthAdapter, "onSessionChange">,
|
|
178
|
-
store: { notify(type: string, payload?: unknown): void },
|
|
179
|
-
options?: { id?: string },
|
|
180
|
-
): () => void
|
|
@@ -1,8 +1,28 @@
|
|
|
1
|
+
export interface KeyboardEntity {
|
|
2
|
+
id?: string
|
|
3
|
+
type: string
|
|
4
|
+
/** The page has a visual viewport to read the keyboard from. */
|
|
5
|
+
isSupported: boolean
|
|
6
|
+
/** The on-screen keyboard is up. */
|
|
7
|
+
isOpen: boolean
|
|
8
|
+
/** How much of the page the keyboard covers, in pixels: 0 where the page shrinks for it. */
|
|
9
|
+
height: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface KeyboardChange {
|
|
13
|
+
isOpen: boolean
|
|
14
|
+
height: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type KeyboardType = {
|
|
18
|
+
create(entity: KeyboardEntity): void
|
|
19
|
+
destroy(entity: KeyboardEntity): void
|
|
20
|
+
keyboardChange(entity: KeyboardEntity, change: KeyboardChange): void
|
|
21
|
+
}
|
|
22
|
+
|
|
1
23
|
/**
|
|
2
|
-
*
|
|
3
|
-
* covers,
|
|
4
|
-
*
|
|
5
|
-
* for the keyboard, the class is set and the height stays 0.
|
|
6
|
-
* @returns A function that stops following.
|
|
24
|
+
* The type that knows when the on-screen keyboard is up and how much of the
|
|
25
|
+
* page it covers, setting `--iw-keyboard-height` on `<html>` and the
|
|
26
|
+
* `iw-keyboard-open` class so fixed elements can move above it.
|
|
7
27
|
*/
|
|
8
|
-
export declare
|
|
28
|
+
export declare const Keyboard: KeyboardType
|