@fullstackunicorn/webapp 2.0.5 → 2.1.1
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/files.json +810 -0
- package/imports/cache.js +38 -1
- package/imports/client.js +124 -17
- package/imports/core/Link.js +8 -2
- package/imports/core/Pressable.js +4 -2
- package/imports/files.js +45 -0
- package/load/Load.js +57 -25
- package/load/engine.js +18 -11
- package/load/resources.js +16 -4
- package/load/use.js +4 -1
- package/native/imports/files.js +7 -0
- package/package.json +2 -1
- package/package.md +71 -13
package/imports/cache.js
CHANGED
|
@@ -11,10 +11,12 @@ class Cache {
|
|
|
11
11
|
initialization = null
|
|
12
12
|
generation = 0
|
|
13
13
|
commits = new Map()
|
|
14
|
+
sources = new Map()
|
|
14
15
|
|
|
15
16
|
initialize = () => {
|
|
16
17
|
if (!this.initialization) this.initialization = entries().then(values => {
|
|
17
18
|
for (const [key, value] of values) if (typeof key === 'string' && key.startsWith('version:') && value) this.versions.set(key.slice(8), value)
|
|
19
|
+
for (const [key, value] of values) if (typeof key === 'string' && key.startsWith('source:') && value) this.sources.set(key.slice(7), value)
|
|
18
20
|
this.initialized = true
|
|
19
21
|
}).catch(error => { this.initialization = null; throw error })
|
|
20
22
|
return this.initialization
|
|
@@ -22,6 +24,36 @@ class Cache {
|
|
|
22
24
|
|
|
23
25
|
version = key => this.initialized ? this.versions.get(key) : undefined
|
|
24
26
|
|
|
27
|
+
source = origin => this.sources.get(origin)?.revision
|
|
28
|
+
|
|
29
|
+
release = origin => this.sources.get(origin)?.cursor
|
|
30
|
+
|
|
31
|
+
observe = async (origin, revision, cursor) => {
|
|
32
|
+
await this.initialize()
|
|
33
|
+
if (this.source(origin) === revision && this.release(origin) === cursor) return false
|
|
34
|
+
const state = { revision, cursor }
|
|
35
|
+
await set('source:' + origin, state)
|
|
36
|
+
this.sources.set(origin, state)
|
|
37
|
+
return true
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
current = (record, origin) => {
|
|
41
|
+
const revision = this.source(origin)
|
|
42
|
+
if (record?.source !== origin || !/^[a-f0-9]{64}$/.test(record.hash || '') || !/^[a-f0-9]{64}$/.test(record.revision || '')) return false
|
|
43
|
+
return !revision || record.revision === revision
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
record = async (store, key) => {
|
|
47
|
+
const response = await store.match(key)
|
|
48
|
+
if (!response) return null
|
|
49
|
+
return {
|
|
50
|
+
data: await response.json(),
|
|
51
|
+
hash: response.headers.get('X-Cache-Hash'),
|
|
52
|
+
source: response.headers.get('X-Cache-Source'),
|
|
53
|
+
revision: response.headers.get('X-Cache-Revision'),
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
25
57
|
commit = (key, work) => {
|
|
26
58
|
const previous = this.commits.get(key) || Promise.resolve()
|
|
27
59
|
const current = previous.catch(() => {}).then(work)
|
|
@@ -57,7 +89,12 @@ class Cache {
|
|
|
57
89
|
set = async (cache, key, value, isBlob, metadata = {}) => {
|
|
58
90
|
|
|
59
91
|
const headers = { headers: {'Content-Type': value?.type || 'application/octet-stream', 'X-Cache-Version': metadata.version || ''}}
|
|
60
|
-
const headersJson = { headers: {
|
|
92
|
+
const headersJson = { headers: {
|
|
93
|
+
'Content-Type': 'application/json',
|
|
94
|
+
'X-Cache-Hash': metadata.hash || '',
|
|
95
|
+
'X-Cache-Source': metadata.source || '',
|
|
96
|
+
'X-Cache-Revision': metadata.revision || '',
|
|
97
|
+
} }
|
|
61
98
|
|
|
62
99
|
const response = isBlob
|
|
63
100
|
? new Response(value, headers)
|
package/imports/client.js
CHANGED
|
@@ -14,6 +14,28 @@ class Client {
|
|
|
14
14
|
this.cacheStale = constants.cache.stale
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
source = endpoint => {
|
|
18
|
+
if (typeof endpoint !== 'string') return null
|
|
19
|
+
const origins = Object.entries(constants.env || {}).filter(([name, value]) => /^CDN\d+$/.test(name) && typeof value === 'string' && /^https?:\/\//.test(value))
|
|
20
|
+
.map(([name, value]) => ({ id: name === 'CDN0' ? 'site' : name, origin: value.replace(/\/+$/, '') }))
|
|
21
|
+
.sort((a, b) => b.origin.length - a.origin.length)
|
|
22
|
+
for (const source of origins) {
|
|
23
|
+
if (!endpoint.startsWith(source.origin + '/')) continue
|
|
24
|
+
const path = endpoint.slice(source.origin.length + 1).split('#')[0]
|
|
25
|
+
if (/^(data|page|site|lookup|menu)\//.test(path) && !path.includes('..') && !path.includes('?')) return { ...source, path }
|
|
26
|
+
}
|
|
27
|
+
return null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
etagHash = value => /^(?:W\/)?"([a-f0-9]{64})"$/.exec(String(value || ''))?.[1]
|
|
31
|
+
|
|
32
|
+
identity = (headers, source) => {
|
|
33
|
+
const hash = this.etagHash(headers['etag'])
|
|
34
|
+
const revision = headers['x-data-revision']
|
|
35
|
+
if (!/^[a-f0-9]{64}$/.test(hash) || !/^[a-f0-9]{64}$/.test(revision || '')) throw new Error('Invalid public JSON identity')
|
|
36
|
+
return { hash, source: source.origin, revision }
|
|
37
|
+
}
|
|
38
|
+
|
|
17
39
|
validate = ({ response = {}, is = {} }) => {
|
|
18
40
|
if (!F3.obj.isGood(response)) return { isError: true, error: 'no-response' }
|
|
19
41
|
if (F3.err.is400(response.status)) return { isError: true, error: 'client-error' }
|
|
@@ -101,47 +123,92 @@ class Client {
|
|
|
101
123
|
cacheData = async ({ key, method, endpoint, data = {}, config = {}, revalidate = false, cacheOnly = false, isVersion = false }, isGet) => {
|
|
102
124
|
|
|
103
125
|
const cacheStore = await cache.open(this.cacheNameObjects)
|
|
104
|
-
const
|
|
126
|
+
const source = isGet && !isVersion ? this.source(endpoint) : null
|
|
127
|
+
if (source) await cache.initialize()
|
|
128
|
+
let cachedRecord
|
|
129
|
+
try { cachedRecord = source ? await cache.record(cacheStore, key) : null }
|
|
130
|
+
catch (error) {
|
|
131
|
+
if (!(error instanceof SyntaxError)) throw error
|
|
132
|
+
await cache.remove(cacheStore, key)
|
|
133
|
+
}
|
|
134
|
+
const cachedItem = source ? cachedRecord?.data : await cache.get(cacheStore, key, false)
|
|
105
135
|
const cachedResponse = cachedItem ? { key, data: cachedItem } : undefined
|
|
106
136
|
const { etag, timestamp } = await cache.getMeta(key)
|
|
137
|
+
const pulse = source ? cache.source(source.origin) : undefined
|
|
138
|
+
const cursor = source ? cache.release(source.origin) : undefined
|
|
139
|
+
const url = cursor ? endpoint + '?release=' + encodeURIComponent(cursor) : endpoint
|
|
140
|
+
const retry = () => this.cacheData({ key, method, endpoint, data, config: { ...config, headers: { ...config.headers, 'If-None-Match': undefined } }, revalidate, cacheOnly, isVersion }, isGet)
|
|
107
141
|
|
|
108
142
|
const age = (Date.now() - timestamp) / 1000
|
|
109
|
-
if (cachedItem && !revalidate && (cacheOnly || age < this.cacheStale)) return cachedResponse
|
|
143
|
+
if (cachedItem && (source ? cache.current(cachedRecord, source.origin) : !revalidate && (cacheOnly || age < this.cacheStale))) return cachedResponse
|
|
110
144
|
|
|
111
145
|
config.timeout = 10000
|
|
112
|
-
if (etag && cachedItem) {
|
|
146
|
+
if (etag && cachedItem && (!source || cachedRecord?.hash === this.etagHash(etag))) {
|
|
113
147
|
if (!config.headers) config.headers = {}
|
|
114
|
-
config.headers['If-None-Match'] = etag
|
|
148
|
+
config.headers['If-None-Match'] = source ? '"' + cachedRecord.hash + '"' : etag
|
|
115
149
|
config.validateStatus = status => (status >= 200 && status < 300) || status === 304
|
|
116
150
|
}
|
|
117
151
|
|
|
118
152
|
try {
|
|
119
153
|
|
|
120
154
|
const response = isGet
|
|
121
|
-
? await axios.get(
|
|
155
|
+
? await axios.get(url, config)
|
|
122
156
|
: await axios[method](endpoint, data, config)
|
|
123
157
|
|
|
124
158
|
if (config.signal?.aborted) throw new DOMException('Aborted', 'AbortError')
|
|
159
|
+
const identity = source ? this.identity(response.headers, source) : undefined
|
|
125
160
|
if (cachedItem && response.status === 304) {
|
|
126
|
-
|
|
127
|
-
|
|
161
|
+
let data = cachedItem
|
|
162
|
+
let superseded = false
|
|
163
|
+
await cache.commit(key, async () => {
|
|
164
|
+
if (config.signal?.aborted) throw new DOMException('Aborted', 'AbortError')
|
|
165
|
+
const record = source ? await cache.record(cacheStore, key) : null
|
|
166
|
+
const current = source ? record?.data : await cache.get(cacheStore, key, false)
|
|
167
|
+
if (source && pulse !== cache.source(source.origin) && identity.revision !== cache.source(source.origin)) {
|
|
168
|
+
if (current && cache.current(record, source.origin)) data = current
|
|
169
|
+
else superseded = true
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
if (pulse && identity.revision !== pulse) throw new Error('Public JSON revision does not match Pulse')
|
|
173
|
+
if (current) data = current
|
|
174
|
+
else await cache.set(cacheStore, key, cachedItem, false, identity)
|
|
175
|
+
if (!current || JSON.stringify(current) === JSON.stringify(cachedItem)) {
|
|
176
|
+
if (source) await cache.set(cacheStore, key, data, false, identity)
|
|
177
|
+
await cache.setMeta(key, response.headers['etag'] || etag, data?.version)
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
if (superseded) return await retry()
|
|
181
|
+
return { key, data }
|
|
128
182
|
}
|
|
129
183
|
|
|
130
184
|
const validated = this.validate({ response, key })
|
|
131
185
|
if (!validated.isValid) return validated
|
|
132
186
|
|
|
187
|
+
let data = response.data
|
|
188
|
+
let superseded = false
|
|
133
189
|
await cache.commit(key, async () => {
|
|
134
190
|
if (config.signal?.aborted) throw new DOMException('Aborted', 'AbortError')
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
191
|
+
const record = source ? await cache.record(cacheStore, key) : null
|
|
192
|
+
const current = source ? record?.data : await cache.get(cacheStore, key, false)
|
|
193
|
+
if (source && pulse !== cache.source(source.origin) && identity.revision !== cache.source(source.origin)) {
|
|
194
|
+
if (current && cache.current(record, source.origin)) data = current
|
|
195
|
+
else superseded = true
|
|
196
|
+
return
|
|
197
|
+
}
|
|
198
|
+
if (pulse && identity.revision !== pulse) throw new Error('Public JSON revision does not match Pulse')
|
|
199
|
+
if (!isVersion && (!source || cache.current(record, source.origin)) && current?.version && data.version && current.version > data.version) {
|
|
200
|
+
data = current
|
|
201
|
+
return
|
|
202
|
+
}
|
|
203
|
+
await cache.set(cacheStore, key, data, false, identity)
|
|
204
|
+
await cache.setMeta(key, response.headers['etag'], data?.version)
|
|
139
205
|
})
|
|
140
206
|
|
|
141
|
-
|
|
207
|
+
if (superseded) return await retry()
|
|
208
|
+
return { key, data }
|
|
142
209
|
|
|
143
210
|
} catch (error) {
|
|
144
|
-
if (revalidate || config.signal?.aborted) throw error
|
|
211
|
+
if (source || revalidate || config.signal?.aborted) throw error
|
|
145
212
|
return await err.cache(error, cachedResponse)
|
|
146
213
|
}
|
|
147
214
|
}
|
|
@@ -240,21 +307,61 @@ class Client {
|
|
|
240
307
|
await cache.remove(cacheStore, key)
|
|
241
308
|
}
|
|
242
309
|
|
|
243
|
-
cacheDataSet = async ({ endpoint, value, version }) => {
|
|
310
|
+
cacheDataSet = async ({ endpoint, value, version, sourceRevision }) => {
|
|
244
311
|
const key = abs.makeKey(endpoint, 'get')
|
|
245
312
|
const cacheStore = await cache.open(this.cacheNameObjects)
|
|
313
|
+
const source = this.source(endpoint)
|
|
314
|
+
await cache.initialize()
|
|
246
315
|
await cache.commit(key, async () => {
|
|
247
|
-
await cache.
|
|
248
|
-
await cache.
|
|
316
|
+
const record = source ? await cache.record(cacheStore, key) : null
|
|
317
|
+
const current = source ? record?.data : await cache.get(cacheStore, key, false)
|
|
318
|
+
if (source && cache.source(source.origin) && sourceRevision !== cache.source(source.origin)) throw new DOMException('Superseded Data revision', 'AbortError')
|
|
319
|
+
if ((!source || cache.current(record, source.origin)) && current?.version && value?.version && current.version > value.version) throw new Error('Superseded JSON version')
|
|
320
|
+
await cache.set(cacheStore, key, value, false, { hash: version, source: source?.origin, revision: sourceRevision })
|
|
321
|
+
await cache.setMeta(key, /^[a-f0-9]{64}$/.test(version || '') ? '"' + version + '"' : undefined, value?.version || null)
|
|
249
322
|
})
|
|
250
323
|
return key
|
|
251
324
|
}
|
|
252
325
|
|
|
326
|
+
cacheDataConfirm = async ({ endpoint, version, sourceRevision }) => {
|
|
327
|
+
const key = abs.makeKey(endpoint, 'get')
|
|
328
|
+
const store = await cache.open(this.cacheNameObjects)
|
|
329
|
+
const source = this.source(endpoint)
|
|
330
|
+
return await cache.commit(key, async () => {
|
|
331
|
+
const record = await cache.record(store, key)
|
|
332
|
+
if (!record || record.hash !== version) return false
|
|
333
|
+
if (source && cache.source(source.origin) && sourceRevision !== cache.source(source.origin)) throw new DOMException('Superseded Data revision', 'AbortError')
|
|
334
|
+
if (record.source === source?.origin && record.revision === sourceRevision) return true
|
|
335
|
+
await cache.set(store, key, record.data, false, { hash: version, source: source?.origin, revision: sourceRevision })
|
|
336
|
+
return true
|
|
337
|
+
})
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
cacheDataEntries = async () => {
|
|
341
|
+
const store = await cache.open(this.cacheNameObjects)
|
|
342
|
+
const result = []
|
|
343
|
+
for (const request of await store.keys()) {
|
|
344
|
+
const endpoint = request.url.split('#')[0]
|
|
345
|
+
const source = this.source(endpoint)
|
|
346
|
+
if (!source) continue
|
|
347
|
+
const key = abs.makeKey(endpoint, 'get')
|
|
348
|
+
try {
|
|
349
|
+
const record = await cache.record(store, key)
|
|
350
|
+
if (record && /^[a-f0-9]{64}$/.test(record.hash || '')) result.push({ endpoint, path: source.path, source: source.id, sha256: record.hash })
|
|
351
|
+
} catch (error) {
|
|
352
|
+
if (!(error instanceof SyntaxError)) throw error
|
|
353
|
+
await cache.remove(store, key)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return result
|
|
357
|
+
}
|
|
358
|
+
|
|
253
359
|
cacheDataHas = async ({ endpoint, version }) => {
|
|
254
360
|
await cache.initialize()
|
|
255
361
|
const key = abs.makeKey(endpoint, 'get')
|
|
256
|
-
if (version && cache.version(key) !== version) return false
|
|
257
362
|
const cacheStore = await cache.open(this.cacheNameObjects)
|
|
363
|
+
const response = await cacheStore.match(key)
|
|
364
|
+
if (version && (response?.headers.get('X-Cache-Hash') || cache.version(key)) !== version) return false
|
|
258
365
|
try {
|
|
259
366
|
return (await cache.get(cacheStore, key, false)) !== null
|
|
260
367
|
} catch {
|
package/imports/core/Link.js
CHANGED
|
@@ -24,7 +24,7 @@ import { useNavigate } from '#imports/router.js'
|
|
|
24
24
|
|
|
25
25
|
export const Link = ({
|
|
26
26
|
children, classes = [], inline = {}, hover = [], isExternal = false, isView = false,
|
|
27
|
-
isDraggable = false, setIsHover = null, onPress = null, linkTo = '', navigateOptions = {}, target, label
|
|
27
|
+
isDraggable = false, setIsHover = null, onPress = null, onFocus = null, onBlur = null, linkTo = '', navigateOptions = {}, target, label
|
|
28
28
|
}) => {
|
|
29
29
|
|
|
30
30
|
const navigate = useNavigate()
|
|
@@ -49,6 +49,8 @@ export const Link = ({
|
|
|
49
49
|
if (setIsHover) setIsHover(false)
|
|
50
50
|
}}
|
|
51
51
|
onClick={onPress}
|
|
52
|
+
onFocus={onFocus}
|
|
53
|
+
onBlur={onBlur}
|
|
52
54
|
>
|
|
53
55
|
{children}
|
|
54
56
|
</div>
|
|
@@ -74,6 +76,8 @@ export const Link = ({
|
|
|
74
76
|
if (setIsHover) setIsHover(false)
|
|
75
77
|
}}
|
|
76
78
|
onClick={onPress}
|
|
79
|
+
onFocus={onFocus}
|
|
80
|
+
onBlur={onBlur}
|
|
77
81
|
>
|
|
78
82
|
{children}
|
|
79
83
|
</a>
|
|
@@ -104,8 +108,10 @@ export const Link = ({
|
|
|
104
108
|
if (setIsHover) setIsHover(false)
|
|
105
109
|
}}
|
|
106
110
|
onClick={handleClick}
|
|
111
|
+
onFocus={onFocus}
|
|
112
|
+
onBlur={onBlur}
|
|
107
113
|
>
|
|
108
114
|
{children}
|
|
109
115
|
</a>
|
|
110
116
|
)
|
|
111
|
-
}
|
|
117
|
+
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
export const Pressable = ({ classes=[], inline={}, children, onPress, setIsHover, isButton = false, disabled = false, label }) => {
|
|
1
|
+
export const Pressable = ({ classes=[], inline={}, children, onPress, onFocus, onBlur, setIsHover, isButton = false, disabled = false, label }) => {
|
|
2
2
|
|
|
3
3
|
const props = {}
|
|
4
4
|
props.className = classes.join(' ')
|
|
5
5
|
props.style = inline
|
|
6
6
|
|
|
7
7
|
if (onPress && !disabled) props.onClick=onPress
|
|
8
|
+
if (onFocus) props.onFocus=onFocus
|
|
9
|
+
if (onBlur) props.onBlur=onBlur
|
|
8
10
|
if (label) props['aria-label'] = label
|
|
9
11
|
if (isButton) { props.type = 'button'; props.disabled = disabled }
|
|
10
12
|
if (setIsHover) {
|
|
@@ -14,4 +16,4 @@ export const Pressable = ({ classes=[], inline={}, children, onPress, setIsHover
|
|
|
14
16
|
|
|
15
17
|
const Element = isButton ? 'button' : 'div'
|
|
16
18
|
return <Element {...props}>{children}</Element>
|
|
17
|
-
}
|
|
19
|
+
}
|
package/imports/files.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const requiredText = (value, name) => {
|
|
2
|
+
if (typeof value !== 'string') throw new TypeError(name + '_must_be_string')
|
|
3
|
+
return value
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export const copyText = async value => {
|
|
7
|
+
const text = requiredText(value, 'copy_text')
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
if (navigator.clipboard?.writeText) {
|
|
11
|
+
await navigator.clipboard.writeText(text)
|
|
12
|
+
return true
|
|
13
|
+
}
|
|
14
|
+
} catch {}
|
|
15
|
+
|
|
16
|
+
const input = document.createElement('textarea')
|
|
17
|
+
input.value = text
|
|
18
|
+
input.setAttribute('readonly', '')
|
|
19
|
+
input.style.position = 'fixed'
|
|
20
|
+
input.style.opacity = '0'
|
|
21
|
+
document.body.append(input)
|
|
22
|
+
input.select()
|
|
23
|
+
const copied = document.execCommand('copy')
|
|
24
|
+
input.remove()
|
|
25
|
+
if (!copied) throw new Error('copy_text_failed')
|
|
26
|
+
return true
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const downloadText = ({ name, value, type = 'text/plain;charset=utf-8' }) => {
|
|
30
|
+
const filename = requiredText(name, 'download_name')
|
|
31
|
+
const text = requiredText(value, 'download_text')
|
|
32
|
+
const mime = requiredText(type, 'download_type')
|
|
33
|
+
if (!filename || filename.includes('/') || filename.includes('\\')) throw new Error('download_name_invalid')
|
|
34
|
+
|
|
35
|
+
const object = URL.createObjectURL(new Blob([text], { type: mime }))
|
|
36
|
+
const link = document.createElement('a')
|
|
37
|
+
link.href = object
|
|
38
|
+
link.download = filename
|
|
39
|
+
link.hidden = true
|
|
40
|
+
document.body.append(link)
|
|
41
|
+
link.click()
|
|
42
|
+
link.remove()
|
|
43
|
+
URL.revokeObjectURL(object)
|
|
44
|
+
return true
|
|
45
|
+
}
|
package/load/Load.js
CHANGED
|
@@ -6,28 +6,22 @@ import { useWindowDimensions } from '#imports/use.js'
|
|
|
6
6
|
import { visibility } from '#imports/visibility.js'
|
|
7
7
|
import { useUrl } from '#@/base/router/@use.js'
|
|
8
8
|
import { engine } from '#load/engine.js'
|
|
9
|
-
import { useRefreshContent } from '#load/use.js'
|
|
9
|
+
import { usePulse, useSetPulse, useRefreshContent } from '#load/use.js'
|
|
10
10
|
import { loadGeometry } from '#load/Geometry.js'
|
|
11
11
|
|
|
12
12
|
const ready = () => {}
|
|
13
13
|
|
|
14
|
-
export const
|
|
14
|
+
export const Pulse = () => {
|
|
15
15
|
|
|
16
16
|
const { lang } = useUrl()
|
|
17
17
|
const refresh = useRefreshContent()
|
|
18
18
|
const refreshRef = useRef(refresh)
|
|
19
|
-
const
|
|
20
|
-
const dimensions = useWindowDimensions()
|
|
21
|
-
const geometry = loadGeometry.signature(dimensions.width, img.pixelRatio())
|
|
22
|
-
const dimensionsRef = useRef(dimensions)
|
|
19
|
+
const setPulse = useSetPulse()
|
|
23
20
|
refreshRef.current = refresh
|
|
24
|
-
onReadyRef.current = onReady
|
|
25
|
-
dimensionsRef.current = dimensions
|
|
26
21
|
|
|
27
22
|
useEffect(() => {
|
|
28
23
|
let active = true
|
|
29
|
-
|
|
30
|
-
let timer, running = false, prepared = false
|
|
24
|
+
let timer, running = false, refreshed, sequence = 0
|
|
31
25
|
const schedule = () => {
|
|
32
26
|
clearTimeout(timer)
|
|
33
27
|
if (active && visibility.isVisible()) timer = setTimeout(check, engine.interval)
|
|
@@ -36,21 +30,20 @@ export const Load = ({ onReady = ready }) => {
|
|
|
36
30
|
if (running || !visibility.isVisible()) return schedule()
|
|
37
31
|
running = true
|
|
38
32
|
try {
|
|
39
|
-
await
|
|
33
|
+
const current = await engine.pulse(lang, true)
|
|
40
34
|
if (!active) return
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
35
|
+
const signature = engine.signature(current)
|
|
36
|
+
const done = priority.begin()
|
|
37
|
+
try {
|
|
38
|
+
setPulse({ lang, current, sequence: ++sequence })
|
|
39
|
+
if (refreshed !== signature) {
|
|
40
|
+
await refreshRef.current(lang)
|
|
41
|
+
refreshed = signature
|
|
47
42
|
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
prepared = true
|
|
51
|
-
onReadyRef.current(true)
|
|
43
|
+
} finally {
|
|
44
|
+
done()
|
|
52
45
|
}
|
|
53
|
-
} catch (error) { if (error.name !== 'AbortError') console.error('
|
|
46
|
+
} catch (error) { if (error.name !== 'AbortError') console.error('pulse-failed', error) }
|
|
54
47
|
finally {
|
|
55
48
|
running = false
|
|
56
49
|
schedule()
|
|
@@ -65,12 +58,51 @@ export const Load = ({ onReady = ready }) => {
|
|
|
65
58
|
check()
|
|
66
59
|
return () => {
|
|
67
60
|
active = false
|
|
68
|
-
controller.abort()
|
|
69
|
-
requests.pause()
|
|
70
61
|
clearTimeout(timer)
|
|
71
62
|
stop()
|
|
72
63
|
}
|
|
73
|
-
}, [lang,
|
|
64
|
+
}, [lang, setPulse])
|
|
65
|
+
|
|
66
|
+
return null
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const Load = ({ onReady = ready }) => {
|
|
70
|
+
|
|
71
|
+
const { lang } = useUrl()
|
|
72
|
+
const pulse = usePulse()
|
|
73
|
+
const onReadyRef = useRef(onReady)
|
|
74
|
+
const prepared = useRef(false)
|
|
75
|
+
const dimensions = useWindowDimensions()
|
|
76
|
+
const geometry = loadGeometry.signature(dimensions.width, img.pixelRatio())
|
|
77
|
+
const dimensionsRef = useRef(dimensions)
|
|
78
|
+
onReadyRef.current = onReady
|
|
79
|
+
dimensionsRef.current = dimensions
|
|
80
|
+
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
if (pulse?.lang !== lang) return
|
|
83
|
+
let active = true
|
|
84
|
+
const controller = new AbortController()
|
|
85
|
+
const notify = () => {
|
|
86
|
+
if (!prepared.current && active) {
|
|
87
|
+
prepared.current = true
|
|
88
|
+
onReadyRef.current(true)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const check = async () => {
|
|
92
|
+
try {
|
|
93
|
+
await priority.ready(controller.signal)
|
|
94
|
+
if (!active) return
|
|
95
|
+
await engine.run(lang, dimensionsRef.current.width, controller.signal, notify, pulse.current)
|
|
96
|
+
notify()
|
|
97
|
+
} catch (error) { if (error.name !== 'AbortError') console.error('load-failed', error) }
|
|
98
|
+
}
|
|
99
|
+
check()
|
|
100
|
+
return () => {
|
|
101
|
+
active = false
|
|
102
|
+
controller.abort()
|
|
103
|
+
requests.pause()
|
|
104
|
+
}
|
|
105
|
+
}, [lang, geometry, pulse])
|
|
74
106
|
|
|
75
107
|
return null
|
|
76
108
|
}
|
package/load/engine.js
CHANGED
|
@@ -7,6 +7,7 @@ import { resources } from '#load/resources.js'
|
|
|
7
7
|
import { uri } from '#@/App.uri.js'
|
|
8
8
|
import settings from '#@/configure/load.json'
|
|
9
9
|
import { loadGeometry } from '#load/Geometry.js'
|
|
10
|
+
import { constants } from '#@/root/constants.js'
|
|
10
11
|
|
|
11
12
|
class Engine {
|
|
12
13
|
|
|
@@ -17,9 +18,9 @@ class Engine {
|
|
|
17
18
|
key = lang => 'load:' + lang
|
|
18
19
|
|
|
19
20
|
signature = state => JSON.stringify({
|
|
20
|
-
site: state?.site ? { version: state.site.version, fingerprint: state.site.fingerprint } : null,
|
|
21
|
+
site: state?.site ? { revision: state.site.revision, version: state.site.version, fingerprint: state.site.fingerprint } : null,
|
|
21
22
|
sources: Object.fromEntries(Object.entries(state?.sources || {}).sort(([a], [b]) => a.localeCompare(b))
|
|
22
|
-
.map(([name, value]) => [name, { version: value.version, fingerprint: value.fingerprint }])),
|
|
23
|
+
.map(([name, value]) => [name, { revision: value.revision, version: value.version, fingerprint: value.fingerprint }])),
|
|
23
24
|
})
|
|
24
25
|
|
|
25
26
|
pulse = (lang, force = false) => {
|
|
@@ -27,9 +28,14 @@ class Engine {
|
|
|
27
28
|
if (previous && (previous.pending || (!force && Date.now() - previous.time < this.interval))) return previous.promise
|
|
28
29
|
const entry = { pending: true, time: Date.now() }
|
|
29
30
|
const endpoint = uri.pulse + '?locale=' + encodeURIComponent(lang)
|
|
30
|
-
entry.promise = json.read(endpoint, true, { isVersion: true }).then(response => {
|
|
31
|
+
entry.promise = json.read(endpoint, true, { isVersion: true }).then(async response => {
|
|
31
32
|
if (!response?.data?.current?.site) throw new Error('Invalid Data pulse')
|
|
32
|
-
|
|
33
|
+
const current = response.data.current
|
|
34
|
+
for (const [source, state] of Object.entries({ CDN0: current.site, ...current.sources })) {
|
|
35
|
+
const origin = constants.env[source]?.replace(/\/$/, '')
|
|
36
|
+
if (origin && /^[a-f0-9]{64}$/.test(state.revision || '')) await cache.observe(origin, state.revision, state.cursor)
|
|
37
|
+
}
|
|
38
|
+
return current
|
|
33
39
|
}).catch(error => { this.pulses.delete(lang); throw error })
|
|
34
40
|
.finally(() => { entry.pending = false })
|
|
35
41
|
this.pulses.set(lang, entry)
|
|
@@ -44,39 +50,40 @@ class Engine {
|
|
|
44
50
|
return !resourcesReady || this.signature(previous.state) !== this.signature(current)
|
|
45
51
|
}
|
|
46
52
|
|
|
47
|
-
run = async (lang, width, signal, onResources) => {
|
|
53
|
+
run = async (lang, width, signal, onResources, current) => {
|
|
48
54
|
const previous = this.jobs.get(lang)
|
|
49
55
|
if (previous) await previous.catch(error => { if (error.name !== 'AbortError') throw error })
|
|
50
56
|
if (signal?.aborted) throw new DOMException('Aborted', 'AbortError')
|
|
51
|
-
const job = this.check(lang, width, signal, onResources).finally(() => { if (this.jobs.get(lang) === job) this.jobs.delete(lang) })
|
|
57
|
+
const job = this.check(lang, width, signal, onResources, current).finally(() => { if (this.jobs.get(lang) === job) this.jobs.delete(lang) })
|
|
52
58
|
this.jobs.set(lang, job)
|
|
53
59
|
return job
|
|
54
60
|
}
|
|
55
61
|
|
|
56
|
-
check = async (lang, width, signal, onResources) => {
|
|
62
|
+
check = async (lang, width, signal, onResources, observed) => {
|
|
57
63
|
await cache.initialize()
|
|
58
64
|
const key = this.key(lang)
|
|
59
65
|
const before = await get(key) || { state: null, complete: false, items: {}, geometry: null }
|
|
60
|
-
const current = await this.pulse(lang
|
|
66
|
+
const current = observed || await this.pulse(lang)
|
|
61
67
|
const geometry = loadGeometry.signature(width, img.pixelRatio())
|
|
62
68
|
const signature = this.signature(current)
|
|
63
69
|
const completion = key + ':' + signature + ':' + geometry + ':' + cache.generation
|
|
64
|
-
if (this.completed.has(completion) && before.complete) return false
|
|
65
70
|
|
|
66
71
|
const previousItems = before.items || {}
|
|
67
72
|
const items = await resources.available(previousItems)
|
|
68
73
|
const inventoryComplete = Object.keys(items).length === Object.keys(previousItems).length
|
|
74
|
+
if (this.completed.has(completion) && before.complete && inventoryComplete) return false
|
|
69
75
|
const changed = !before.complete || !inventoryComplete || this.signature(before.state) !== signature
|
|
70
76
|
const geometryChanged = before.geometry !== geometry
|
|
71
77
|
let nextItems = items
|
|
72
78
|
let refresh = false
|
|
73
79
|
|
|
74
80
|
if (changed) {
|
|
81
|
+
nextItems = { ...nextItems, ...await resources.inventory() }
|
|
75
82
|
const inventory = Object.fromEntries(Object.entries(nextItems).map(([itemKey, item]) => [itemKey, item.sha256]))
|
|
76
83
|
const response = await client.post(uri.data.update, {
|
|
77
84
|
locale: lang,
|
|
78
85
|
from: before.state,
|
|
79
|
-
cache: { complete: before.complete === true && inventoryComplete, items: inventory },
|
|
86
|
+
cache: { complete: before.complete === true && inventoryComplete && this.signature(before.state) !== signature, items: inventory },
|
|
80
87
|
}, { headers: { 'Content-Type': 'application/json' }, signal })
|
|
81
88
|
const preload = response?.preload
|
|
82
89
|
if (!response?.current?.site || !preload || !Array.isArray(preload.delete)
|
|
@@ -88,7 +95,7 @@ class Engine {
|
|
|
88
95
|
nextItems = await resources.apply(preload, width, nextItems, signal, async resourceItems => {
|
|
89
96
|
await set(key, { state: response.current, complete: false, resources: true, items: resourceItems, geometry })
|
|
90
97
|
await onResources?.(refresh)
|
|
91
|
-
})
|
|
98
|
+
}, { current: response.current })
|
|
92
99
|
await set(key, { state: response.current, complete: true, resources: true, items: nextItems, geometry })
|
|
93
100
|
} else if (geometryChanged) {
|
|
94
101
|
await onResources?.(false)
|
package/load/resources.js
CHANGED
|
@@ -49,6 +49,12 @@ class Resources {
|
|
|
49
49
|
return verified
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
inventory = async () => Object.fromEntries((await client.cacheDataEntries())
|
|
53
|
+
.filter(item => resource.url(item.path) === item.endpoint)
|
|
54
|
+
.map(item => ['resource:' + item.path, { key: 'resource:' + item.path, kind: 'resource', path: item.path, source: item.source, sha256: item.sha256 }]))
|
|
55
|
+
|
|
56
|
+
revision = (state, source) => source && source !== 'site' ? state?.sources?.[source]?.revision : state?.site?.revision
|
|
57
|
+
|
|
52
58
|
image = async (item, width, current, signal) => {
|
|
53
59
|
const selected = img.getImage(item.picture, loadGeometry.width(item, width), current?.size)
|
|
54
60
|
if (current?.url === selected.src && current?.hash === selected.hash) {
|
|
@@ -63,10 +69,9 @@ class Resources {
|
|
|
63
69
|
return { ...item, size: selected.size, url: selected.src, version: selected.version, hash: selected.hash }
|
|
64
70
|
}
|
|
65
71
|
|
|
66
|
-
apply = async (preload, width, current = {}, signal, onResources) => {
|
|
72
|
+
apply = async (preload, width, current = {}, signal, onResources, context = {}) => {
|
|
67
73
|
const next = { ...current }
|
|
68
74
|
for (const key of preload.delete || []) {
|
|
69
|
-
await this.remove(next[key])
|
|
70
75
|
delete next[key]
|
|
71
76
|
}
|
|
72
77
|
|
|
@@ -74,8 +79,15 @@ class Resources {
|
|
|
74
79
|
const resources = changed.filter(item => item.kind === 'resource')
|
|
75
80
|
await this.queue(resources, settings.concurrency, async item => {
|
|
76
81
|
if (!/^(data|page|site|lookup|menu)\//.test(item.path || '') || item.path.includes('..')) throw new Error('Invalid load resource: ' + item.path)
|
|
77
|
-
await client.cacheDataSet({ endpoint: resource.url(item.path), value: item.value, version: item.sha256
|
|
78
|
-
|
|
82
|
+
await client.cacheDataSet({ endpoint: resource.url(item.path), value: item.value, version: item.sha256,
|
|
83
|
+
sourceRevision: this.revision(context.current, item.source) })
|
|
84
|
+
const { value, ...descriptor } = item
|
|
85
|
+
next[item.key] = descriptor
|
|
86
|
+
})
|
|
87
|
+
await this.queue(Object.values(next).filter(item => item.kind === 'resource'), settings.concurrency, async item => {
|
|
88
|
+
const confirmed = await client.cacheDataConfirm({ endpoint: resource.url(item.path), version: item.sha256,
|
|
89
|
+
sourceRevision: this.revision(context.current, item.source) })
|
|
90
|
+
if (!confirmed) throw new DOMException('Load inventory changed', 'AbortError')
|
|
79
91
|
})
|
|
80
92
|
await onResources?.(next)
|
|
81
93
|
|
package/load/use.js
CHANGED
|
@@ -5,15 +5,18 @@ import { useSetLookups } from '#@/base/lookups/@use.js'
|
|
|
5
5
|
|
|
6
6
|
const revision = atom(0)
|
|
7
7
|
export const useContentRevision = () => useGetAtom(revision)
|
|
8
|
+
const pulse = atom(null)
|
|
9
|
+
export const usePulse = () => useGetAtom(pulse)
|
|
10
|
+
export const useSetPulse = () => useSetAtom(pulse)
|
|
8
11
|
|
|
9
12
|
export const useRefreshContent = () => {
|
|
10
13
|
const setRevision = useSetAtom(revision)
|
|
11
14
|
const setStatics = useSetStatics()
|
|
12
15
|
const setLookups = useSetLookups()
|
|
13
16
|
return useCallback(async lang => {
|
|
17
|
+
setRevision(value => value + 1)
|
|
14
18
|
const data = await tools.loadData(lang)
|
|
15
19
|
setStatics(data.statics)
|
|
16
20
|
setLookups(data.lookups)
|
|
17
|
-
setRevision(value => value + 1)
|
|
18
21
|
}, [setLookups, setRevision, setStatics])
|
|
19
22
|
}
|