@fullstackunicorn/webapp 1.0.0 → 2.0.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/imports/client.js CHANGED
@@ -239,6 +239,16 @@ class Client {
239
239
  await cache.remove(cacheStore, key)
240
240
  }
241
241
 
242
+ cacheDataSet = async ({ endpoint, value, version }) => {
243
+ const key = abs.makeKey(endpoint, 'get')
244
+ const cacheStore = await cache.open(this.cacheNameObjects)
245
+ await cache.commit(key, async () => {
246
+ await cache.set(cacheStore, key, value, false)
247
+ await cache.setMeta(key, undefined, version)
248
+ })
249
+ return key
250
+ }
251
+
242
252
  cacheClear = async (cacheName) => await cache.clear(cacheName)
243
253
 
244
254
  cachePreloadGet = async (items = []) => {
@@ -1,6 +1,3 @@
1
- import { useImageProfiles } from '#@/abstracts/images.js'
2
- import { cache } from '#imports/cache.js'
3
- import { abs } from '#@/abstracts/abs.js'
4
1
  import { priority } from '#load/priority.js'
5
2
  import { useState, useEffect, useRef } from '#dep'
6
3
  import { img } from '#@/abstracts/img.js'
@@ -8,128 +5,153 @@ import { useGetStatics } from '#@/base/statics/@use.js'
8
5
  import { ImgPod, ImgDataPod, NoImgPod } from './Img.pod.js'
9
6
  import { api } from '#@/client/@api.js'
10
7
 
11
- export const ImgData = (props) => <ImgDataPod {...props} />
8
+ export const ImgData = props => <ImgDataPod {...props} />
12
9
 
13
- export const NoImg = (props) => {
10
+ export const NoImg = props => {
14
11
  const statics = useGetStatics()
15
12
  return <NoImgPod {...props} svg={statics.visual.svg.picture} />
16
13
  }
17
14
 
18
15
  export const Img = ({ classes, inline, picture = {} }) => {
19
- useImageProfiles()
20
- const size = picture.size || img.profile(picture.profile, window.innerWidth)
21
- const image = img.getImage({ ...picture, size })
22
- return <ImageRequest key={(image.src || 'empty') + ':' + (image.version || '')} {...{ classes, inline, image }} />
16
+ if (!picture.src) return <NoImg {...{ classes, inline }} />
17
+ return <ImageRequest {...{ classes, inline, picture }} />
23
18
  }
24
19
 
25
- const ImageRequest = ({ classes, inline, image }) => {
26
- const finishImage = useRef(() => {})
27
- const [src, setSrc] = useState()
28
- const [loadedSrc, setLoadedSrc] = useState()
29
- const [animate, setAnimate] = useState(() => cache.initialized && cache.version(abs.makeKey(image.src, 'get')) !== image.version)
20
+ const ImageRequest = ({ classes, inline, picture }) => {
21
+ const hostRef = useRef()
22
+ const displayedRef = useRef()
23
+ const pendingRef = useRef()
24
+ const urlsRef = useRef(new Set())
25
+ const frameRef = useRef()
26
+ const [width, setWidth] = useState(0)
27
+ const [displayed, setDisplayed] = useState()
28
+ const [pending, setPending] = useState()
29
+ const [pendingVisible, setPendingVisible] = useState(false)
30
+ displayedRef.current = displayed
31
+ pendingRef.current = pending
30
32
 
31
33
  useEffect(() => {
32
- if (image.isNoImage || !image.src) return
34
+ const node = hostRef.current
35
+ if (!node) return
36
+ const measure = value => {
37
+ const next = Math.ceil(value || node.getBoundingClientRect().width || 0)
38
+ if (next > 0) setWidth(current => current === next ? current : next)
39
+ }
40
+ measure()
41
+ const observer = new ResizeObserver(entries => measure(entries[0]?.contentRect?.width))
42
+ observer.observe(node)
43
+ return () => observer.disconnect()
44
+ }, [])
33
45
 
34
- finishImage.current = priority.begin()
35
- let mounted = true
36
- const objectUrls = new Set()
37
- const controller = new AbortController()
46
+ const image = width > 0 ? img.getImage(picture, width, displayed?.image?.size) : null
47
+
48
+ useEffect(() => {
49
+ if (!image?.src || (displayedRef.current?.image?.src === image.src
50
+ && displayedRef.current?.image?.hash === image.hash)) return
38
51
 
39
- setSrc(undefined)
40
- setLoadedSrc(undefined)
41
- setAnimate(cache.initialized && cache.version(abs.makeKey(image.src, 'get')) !== image.version)
52
+ const controller = new AbortController()
53
+ const finish = priority.begin()
54
+ let active = true
42
55
 
43
- const setBlob = async (blob) => {
44
- const objectUrl = URL.createObjectURL(blob)
45
- objectUrls.add(objectUrl)
56
+ const prepare = async (src, objectUrl = false) => {
57
+ if (objectUrl) urlsRef.current.add(src)
46
58
  const element = new Image()
47
- element.src = objectUrl
59
+ element.src = src
48
60
  try {
49
61
  await element.decode()
50
- if (!mounted) return
51
- setSrc(objectUrl)
52
- setLoadedSrc(objectUrl)
62
+ if (!active) return
63
+ const next = { src, image, loaded: true }
64
+ if (!displayedRef.current) {
65
+ displayedRef.current = next
66
+ setDisplayed(next)
67
+ } else {
68
+ pendingRef.current = next
69
+ setPendingVisible(false)
70
+ setPending(next)
71
+ }
53
72
  } finally {
54
- if (!mounted) {
55
- URL.revokeObjectURL(objectUrl)
56
- objectUrls.delete(objectUrl)
73
+ if (!active && objectUrl) {
74
+ URL.revokeObjectURL(src)
75
+ urlsRef.current.delete(src)
57
76
  }
58
77
  }
59
78
  }
60
79
 
61
- const fetchImage = async () => {
62
- const fetched = await api.statics.image(image.src, 'set', { signal: controller.signal, version: image.version, hash: image.hash })
63
-
64
- if (fetched?.isCanceled) return fetched
65
- if (!mounted) return fetched
66
- if (!fetched?.blob) return fetched
67
-
68
- await setBlob(fetched.blob)
69
- return fetched
70
- }
71
-
72
80
  const load = async () => {
73
81
  const cached = await api.statics.image(image.src, 'get', { version: image.version })
74
-
75
- if (!mounted) return
76
-
77
- if (cached?.blob) {
78
- await setBlob(cached.blob)
79
- return
80
- }
81
-
82
- setAnimate(true)
83
- const fetched = await fetchImage()
84
- if (fetched?.isCanceled) return
85
-
86
- if (mounted && fetched?.blob) {
87
- return
88
- }
89
-
90
- if (mounted) {
91
- setSrc(image.src)
92
- }
82
+ if (!active) return
83
+ if (cached?.blob) return await prepare(URL.createObjectURL(cached.blob), true)
84
+ const fetched = await api.statics.image(image.src, 'set', {
85
+ signal: controller.signal,
86
+ version: image.version,
87
+ hash: image.hash,
88
+ })
89
+ if (!active || fetched?.isCanceled) return
90
+ if (fetched?.blob) return await prepare(URL.createObjectURL(fetched.blob), true)
91
+ await prepare(image.src)
93
92
  }
94
93
 
95
94
  load().catch(error => {
96
- if (error.name === 'AbortError') { finishImage.current(); return }
97
- console.error('image-load-failed', { src: image.src, error })
98
- finishImage.current()
99
- })
95
+ if (error.name !== 'AbortError') console.error('image-load-failed', { src: image.src, error })
96
+ }).finally(finish)
100
97
 
101
98
  return () => {
102
- mounted = false
103
- finishImage.current()
99
+ active = false
104
100
  controller.abort()
105
- objectUrls.forEach(url => URL.revokeObjectURL(url))
106
- objectUrls.clear()
101
+ finish()
102
+ const next = pendingRef.current
103
+ if (next?.image?.src === image.src && next?.image?.hash === image.hash) {
104
+ if (next.src?.startsWith('blob:')) {
105
+ URL.revokeObjectURL(next.src)
106
+ urlsRef.current.delete(next.src)
107
+ }
108
+ pendingRef.current = undefined
109
+ setPending(undefined)
110
+ setPendingVisible(false)
111
+ }
107
112
  }
108
- }, [image.hash, image.isNoImage, image.src, image.version])
109
-
110
- if (image.isNoImage) return <NoImg {...{ classes, inline }} />
113
+ }, [image?.hash, image?.src])
114
+
115
+ useEffect(() => () => {
116
+ cancelAnimationFrame(frameRef.current)
117
+ for (const url of urlsRef.current) URL.revokeObjectURL(url)
118
+ urlsRef.current.clear()
119
+ }, [])
120
+
121
+ const revealPending = () => {
122
+ cancelAnimationFrame(frameRef.current)
123
+ frameRef.current = requestAnimationFrame(() => setPendingVisible(true))
124
+ }
125
+
126
+ const commitPending = event => {
127
+ if (event.propertyName !== 'opacity' || !pendingRef.current) return
128
+ const previous = displayedRef.current?.src
129
+ const next = pendingRef.current
130
+ displayedRef.current = next
131
+ pendingRef.current = undefined
132
+ setDisplayed(next)
133
+ setPending(undefined)
134
+ setPendingVisible(false)
135
+ if (previous?.startsWith('blob:')) {
136
+ URL.revokeObjectURL(previous)
137
+ urlsRef.current.delete(previous)
138
+ }
139
+ }
111
140
 
112
141
  return (
113
142
  <ImgPod
114
- {...{ classes, inline }}
115
- src={src}
116
- placeholder={image.placeholder}
117
- showPlaceholder={animate}
118
- animate={animate}
119
- alt={image.alt}
120
- ratio={image.ratio}
121
- isLoaded={!!src && loadedSrc === src}
122
- onLoad={async event => {
123
- const element = event.currentTarget
124
- const finish = finishImage.current
125
- try {
126
- await element.decode()
127
- setLoadedSrc(src)
128
- } catch (error) {
129
- console.error('image-decode-failed', { src: image.src, error })
130
- } finally { finish() }
131
- }}
132
- onError={() => finishImage.current()}
143
+ {...{ classes, inline, hostRef }}
144
+ src={displayed?.src}
145
+ pendingSrc={pending?.src}
146
+ pendingVisible={pendingVisible}
147
+ placeholder={picture.placeholder}
148
+ showPlaceholder={!displayed}
149
+ animate={!displayed}
150
+ alt={picture.alt}
151
+ ratio={picture.ratio}
152
+ isLoaded={displayed?.loaded === true}
153
+ onPendingLoad={revealPending}
154
+ onPendingTransitionEnd={commitPending}
133
155
  />
134
156
  )
135
157
  }
@@ -28,26 +28,30 @@ export const NoImgPod = ({ classes, inline, svg }) => {
28
28
 
29
29
  export const ImgPod = ({
30
30
  classes = [], inline = {}, src, alt = '', ratio = 1,
31
- isLoaded, onLoad, onError, placeholder, showPlaceholder = true, animate = true }) => {
31
+ isLoaded, onError, placeholder, showPlaceholder = true, animate = true, hostRef,
32
+ pendingSrc, pendingVisible = false, onPendingLoad, onPendingTransitionEnd }) => {
32
33
 
33
34
  const { s, c, f, t, F, B, S, I, H } = useStyle()
34
35
 
35
36
  B.imgOpacity = !isLoaded ? [s.opacity0] : []
36
37
  B.blurhashOpacity = isLoaded ? [s.opacity0] : []
37
38
  B.imgTransition = { ...inline, transition: animate ? 'opacity 0.3s ease-in-out' : 'none' }
39
+ B.pendingTransition = { ...inline, opacity: pendingVisible ? 1 : 0, transition: 'opacity 0.3s ease-in-out' }
38
40
  B.blurhashTransition = { transition: animate ? 'opacity 0.3s ease-in-out' : 'none' }
39
41
 
40
42
  S.image = [s.positionRelative, s.overflowHidden, s.row_cXcY, ...classes]
41
43
  S.blurhash = [s.positionAbsolute, s.XY100p, ...B.blurhashOpacity]
42
44
  S.box = [s.positionAbsolute, s.XY100p, s.row_cXcY]
43
45
  S.img = [...B.imgOpacity, ...classes]
46
+ S.pending = [s.positionAbsolute, s.XY100p, ...classes]
44
47
 
45
48
  I.image = { objectFit: 'cover', aspectRatio: ratio, ...inline }
46
49
  I.img = B.imgTransition
50
+ I.pending = B.pendingTransition
47
51
  I.blurhash = B.blurhashTransition
48
52
 
49
53
  return (
50
- <div className={S.image.join(' ')} style={I.image}>
54
+ <div ref={hostRef} className={S.image.join(' ')} style={I.image}>
51
55
  {showPlaceholder && typeof placeholder === 'string' && placeholder.length >= 6 && <div className={S.blurhash.join(' ')} style={I.blurhash}>
52
56
  <Blurhash
53
57
  hash={placeholder}
@@ -59,13 +63,19 @@ export const ImgPod = ({
59
63
  />
60
64
  </div>}
61
65
  <div className={S.box.join(' ')}>
62
- <img className={S.img.join(' ')} style={I.img}
66
+ {src && <img className={S.img.join(' ')} style={I.img}
63
67
  src={src}
64
68
  alt={alt}
65
- onLoad={onLoad}
66
69
  onError={onError}
67
- />
70
+ />}
71
+ {pendingSrc && <img className={S.pending.join(' ')} style={I.pending}
72
+ src={pendingSrc}
73
+ alt={alt}
74
+ onLoad={onPendingLoad}
75
+ onError={onError}
76
+ onTransitionEnd={onPendingTransitionEnd}
77
+ />}
68
78
  </div>
69
79
  </div>
70
80
  )
71
- }
81
+ }
package/index.js CHANGED
@@ -51,6 +51,7 @@ export * from './imports/storage.js'
51
51
  export * from './imports/use.js'
52
52
  export * from './imports/visibility.js'
53
53
  export * from './load/Load.js'
54
+ export * from './load/Geometry.js'
54
55
  export * from './load/engine.js'
55
56
  export * from './load/priority.js'
56
57
  export * from './load/requests.js'
@@ -0,0 +1,53 @@
1
+ import geometry from '#@/configure/geometry.json'
2
+ import images from '#@/configure/images.json'
3
+ import registry from '#@/root/registry.js'
4
+ import { calculations, roles } from '#@/load/geometry.scope.js'
5
+
6
+ class Geometry {
7
+
8
+ route = resource => {
9
+ const match = resource.match(/^(?:data|page)\/(.+?)\/(?:logic|[a-z]{2}-[A-Za-z]{2,4})$/)
10
+ if (!match) return null
11
+ const path = match[1]
12
+ return { path, render: path === 'home' ? 'home' : registry.routes?.[path]?.render }
13
+ }
14
+
15
+ uses = item => {
16
+ const uses = []
17
+ for (const resource of item.resources || []) {
18
+ const role = roles.find(value => resource === value.prefix || resource.startsWith(value.prefix))
19
+ if (role) {
20
+ uses.push({ render: role.render, slot: role.slot })
21
+ continue
22
+ }
23
+ const route = this.route(resource)
24
+ if (!route?.render) continue
25
+ for (const field of item.fields || []) uses.push({ render: route.render, slot: images.fields[field] || field })
26
+ }
27
+ if (!uses.length) for (const field of item.fields || []) uses.push({ render: 'global', slot: images.fields[field] || field })
28
+ return [...new Map(uses.map(value => [value.render + ':' + value.slot, value])).values()]
29
+ }
30
+
31
+ width = (item, viewport = 0) => {
32
+ const widths = this.uses(item).map(({ render, slot }) => {
33
+ const calculate = calculations[render]?.[slot] || calculations.global?.[slot]
34
+ if (typeof calculate !== 'function') throw new Error(`Missing load Geometry: ${render}.${slot}`)
35
+ const width = Number(calculate({ geometry, viewport: Number(viewport) || 0 }))
36
+ if (!Number.isFinite(width) || width <= 0) throw new Error(`Invalid load Geometry: ${render}.${slot}`)
37
+ return width
38
+ })
39
+ if (!widths.length) throw new Error('Missing load Geometry use: ' + item.src)
40
+ return Math.max(...widths)
41
+ }
42
+
43
+ signature = (viewport, pixelRatio = 1) => JSON.stringify({
44
+ pixelRatio,
45
+ widths: Object.fromEntries(Object.entries(calculations).map(([render, slots]) => [
46
+ render,
47
+ Object.fromEntries(Object.entries(slots).map(([slot, calculate]) => [slot, calculate({ geometry, viewport: Number(viewport) || 0 })])),
48
+ ])),
49
+ })
50
+
51
+ }
52
+
53
+ export const loadGeometry = new Geometry()
package/load/Load.js CHANGED
@@ -7,13 +7,14 @@ import { visibility } from '#imports/visibility.js'
7
7
  import { useUrl } from '#@/base/router/@use.js'
8
8
  import { engine } from '#load/engine.js'
9
9
  import { useRefreshContent } from '#load/use.js'
10
+ import { loadGeometry } from '#load/Geometry.js'
10
11
 
11
12
  export const Load = () => {
12
13
 
13
- const { lang, path } = useUrl()
14
+ const { lang } = useUrl()
14
15
  const refresh = useRefreshContent()
15
16
  const dimensions = useWindowDimensions()
16
- const profiles = img.profiles(dimensions.width)
17
+ const geometry = loadGeometry.signature(dimensions.width, img.pixelRatio())
17
18
  const dimensionsRef = useRef(dimensions)
18
19
  dimensionsRef.current = dimensions
19
20
 
@@ -54,7 +55,7 @@ export const Load = () => {
54
55
  clearTimeout(timer)
55
56
  stop()
56
57
  }
57
- }, [lang, path, profiles, refresh])
58
+ }, [lang, geometry, refresh])
58
59
 
59
60
  return null
60
61
  }
package/load/engine.js CHANGED
@@ -3,9 +3,10 @@ import { img } from '#@/abstracts/img.js'
3
3
  import { get, set } from '#dep'
4
4
  import { client } from '#imports/client.js'
5
5
  import { json } from '#@/client/json.js'
6
- import { resources as loader } from '#load/resources.js'
6
+ import { resources } from '#load/resources.js'
7
7
  import { uri } from '#@/App.uri.js'
8
- import settings from '#@/configure/preload.json'
8
+ import settings from '#@/configure/load.json'
9
+ import { loadGeometry } from '#load/Geometry.js'
9
10
 
10
11
  class Engine {
11
12
 
@@ -13,7 +14,7 @@ class Engine {
13
14
  jobs = new Map()
14
15
  completed = new Set()
15
16
 
16
- key = lang => 'preload:' + lang
17
+ key = lang => 'load:' + lang
17
18
 
18
19
  signature = state => JSON.stringify({
19
20
  site: state?.site ? { version: state.site.version, fingerprint: state.site.fingerprint } : null,
@@ -39,7 +40,7 @@ class Engine {
39
40
 
40
41
  changed = async lang => {
41
42
  const [current, previous] = await Promise.all([this.pulse(lang), get(this.key(lang))])
42
- return this.signature(previous?.state) !== this.signature(current)
43
+ return !previous?.complete || this.signature(previous.state) !== this.signature(current)
43
44
  }
44
45
 
45
46
  run = async (lang, width, signal) => {
@@ -54,49 +55,43 @@ class Engine {
54
55
  check = async (lang, width, signal) => {
55
56
  await cache.initialize()
56
57
  const key = this.key(lang)
57
- const previous = await get(key)
58
+ const before = await get(key) || { state: null, complete: false, items: {}, geometry: null }
58
59
  const current = await this.pulse(lang, true)
59
- const profile = img.profiles(width)
60
+ const geometry = loadGeometry.signature(width, img.pixelRatio())
60
61
  const signature = this.signature(current)
61
- const completion = key + ':' + signature + ':' + profile + ':' + cache.generation
62
- if (this.completed.has(completion)) return false
63
- const changed = this.signature(previous?.state) !== signature
64
- const profileChanged = previous?.images?.profile !== profile
65
-
66
- if (!changed && !profileChanged) {
67
- await set(key, { ...previous, state: current })
68
- this.completed.add(completion)
69
- return false
70
- }
71
-
72
- let resources = []
73
- let paths = previous?.paths || []
74
- let state = current
75
- if (changed || !previous?.state) {
76
- const response = await client.post(uri.data.update, { locale: lang, from: previous?.state || null }, {
77
- headers: { 'Content-Type': 'application/json' },
78
- signal,
62
+ const completion = key + ':' + signature + ':' + geometry + ':' + cache.generation
63
+ if (this.completed.has(completion) && before.complete) return false
64
+
65
+ const changed = !before.complete || this.signature(before.state) !== signature
66
+ const geometryChanged = before.geometry !== geometry
67
+ let items = before.items || {}
68
+ let refresh = false
69
+
70
+ if (changed) {
71
+ const inventory = Object.fromEntries(Object.entries(items).map(([itemKey, item]) => [itemKey, item.sha256]))
72
+ const response = await client.post(uri.data.update, {
73
+ locale: lang,
74
+ from: before.state,
75
+ cache: { complete: before.complete === true, items: inventory },
76
+ }, { headers: { 'Content-Type': 'application/json' }, signal })
77
+ const preload = response?.preload
78
+ if (!response?.current?.site || !preload || !Array.isArray(preload.delete)
79
+ || !Array.isArray(preload.replace) || !Array.isArray(preload.new)) throw new Error('Invalid Data load bundle')
80
+ items = await resources.apply(preload, width, items, signal)
81
+ refresh = [...preload.delete, ...preload.replace, ...preload.new].some(value => {
82
+ const item = typeof value === 'string' ? before.items?.[value] : value
83
+ return item?.kind === 'resource'
79
84
  })
80
- if (!response?.current?.site || !response?.preload || !['same', 'delta', 'full'].includes(response.mode)) throw new Error('Invalid Data update manifest')
81
- state = response.current
82
- if (response.preload.reset) await client.cacheClear(client.cacheNameObjects)
83
- await loader.remove(response.preload.remove || [])
84
- const upsert = (response.preload.upsert || []).map(item => item.path)
85
- resources = await loader.resources(upsert, true, signal)
86
- paths = response.mode === 'full'
87
- ? upsert
88
- : [...new Set([...paths.filter(path => !(response.preload.remove || []).includes(path)), ...upsert])].sort()
89
- await set(key, { state: response.current, paths, refresh: changed, images: null })
90
- } else if (profileChanged) {
91
- resources = await loader.resources(paths, false, signal)
85
+ await set(key, { state: response.current, complete: true, items, geometry })
86
+ } else if (geometryChanged) {
87
+ items = await resources.resize(items, width, signal)
88
+ await set(key, { ...before, state: current, complete: true, items, geometry })
89
+ } else {
90
+ await set(key, { ...before, state: current })
92
91
  }
93
92
 
94
- if (signal?.aborted) throw new DOMException('Aborted', 'AbortError')
95
- await loader.images(loader.pictures(resources), width, signal)
96
- if (signal?.aborted) throw new DOMException('Aborted', 'AbortError')
97
- await set(key, { state, paths, refresh: false, images: { signature: this.signature(state), profile } })
98
93
  this.completed.add(completion)
99
- return changed
94
+ return refresh
100
95
  }
101
96
 
102
97
  interval = settings.interval
@@ -104,4 +99,3 @@ class Engine {
104
99
  }
105
100
 
106
101
  export const engine = new Engine()
107
-
package/load/resources.js CHANGED
@@ -1,12 +1,11 @@
1
- import settings from '#@/configure/preload.json'
1
+ import settings from '#@/configure/load.json'
2
2
  import images from '#@/configure/images.json'
3
3
  import { priority } from '#load/priority.js'
4
- import { json } from '#@/client/json.js'
5
4
  import { statics } from '#@/client/statics.js'
6
5
  import { img } from '#@/abstracts/img.js'
7
6
  import { resource } from '#@/abstracts/resource.js'
8
7
  import { client } from '#imports/client.js'
9
- import registry from '#@/root/registry.js'
8
+ import { loadGeometry } from '#load/Geometry.js'
10
9
 
11
10
  class Resources {
12
11
 
@@ -26,82 +25,58 @@ class Resources {
26
25
  }))
27
26
  }
28
27
 
29
- resources = async (paths, revalidate = false, signal) => {
30
- const resources = new Map()
31
- const read = async key => {
32
- if (!/^(data|page|site|lookup)\//.test(key) || key.includes('..')) throw new Error('Invalid preload path: ' + key)
33
- const value = await this.background(() => json.read(resource.url(key), revalidate, { signal, background: true }), signal)
34
- resources.set(key, resource.media(value))
28
+ remove = async item => {
29
+ if (item?.kind === 'resource' && item.path) {
30
+ await client.cacheRemove({ method: 'get', endpoint: resource.url(item.path), cacheName: client.cacheNameObjects })
31
+ }
32
+ if (item?.kind === 'image' && item.url) {
33
+ await client.cacheRemove({ method: 'get', endpoint: item.url, cacheName: client.cacheNameImg })
35
34
  }
36
- await this.queue([...new Set(paths)], settings.concurrency, read)
37
- return [...resources.values()]
38
- }
39
-
40
- remove = async paths => {
41
- await this.queue([...new Set(paths)], settings.concurrency, key => client.cacheRemove({
42
- method: 'get',
43
- endpoint: resource.url(key),
44
- cacheName: client.cacheNameObjects,
45
- }))
46
35
  }
47
36
 
48
- get = async (source, lang, revalidate = false, signal) => {
49
- const resources = new Map()
50
- const routes = new Set(Object.keys(registry.routes || {}))
51
- const paths = new Set((source.items || []).map(value => F3.fabUrl(value, { lang })))
52
- for (const group of source.lists || []) for (const key of group.lists) paths.add(F3.fabUrl(key, { lang }))
53
- const read = async key => {
54
- if (!/^(data|page|site|lookup)\//.test(key) || key.includes('..')) throw new Error('Invalid preload path: ' + key)
55
- const value = await this.background(() => json.read(resource.url(key), revalidate, { signal, background: true }), signal)
56
- resources.set(key, resource.media(value))
57
- }
58
- await this.queue([...paths], settings.concurrency, read)
59
- const products = new Set()
60
- for (const { item, lists } of source.lists || []) for (const key of lists) {
61
- const source = resources.get(F3.fabUrl(key, { lang }))
62
- const list = source?.['@data']?.list
63
- if (!list || typeof list !== 'object') throw new Error('Preload list is missing @data.list')
64
- for (const path of Array.isArray(list) ? list : Object.keys(list)) {
65
- if (routes.size && /^(data|page)\/:path(?:\/|$)/.test(item) && !routes.has(path)) continue
66
- const key = F3.fabUrl(item, { path, lang })
67
- if (!resources.has(key)) products.add(key)
68
- }
37
+ image = async (item, width, current, signal) => {
38
+ const selected = img.getImage(item.picture, loadGeometry.width(item, width), current?.size)
39
+ if (current?.url === selected.src && current?.hash === selected.hash) {
40
+ return { ...item, size: selected.size, url: selected.src, version: selected.version, hash: selected.hash }
69
41
  }
70
- await this.queue([...products], settings.concurrency, read)
71
- return [...resources.values()]
42
+ const options = { version: selected.version, hash: selected.hash, signal, background: true }
43
+ await this.background(async () => {
44
+ if (await statics.image(selected.src, 'get', options)) return
45
+ await statics.image(selected.src, 'set', options)
46
+ }, signal)
47
+ if (current?.url && current.url !== selected.src) await this.remove(current)
48
+ return { ...item, size: selected.size, url: selected.src, version: selected.version, hash: selected.hash }
72
49
  }
73
50
 
74
- pictures = resources => {
75
- const pictures = []
76
- const visit = (value, key = '') => {
77
- if (!value || typeof value !== 'object') return
78
- if (value.src && /\/S40\//.test(value.src)) {
79
- if (['background', 'backdrop', 'ambient'].includes(key) && value.placeholder) return
80
- pictures.push({ ...value, profile: value.profile || images.fields[key] })
81
- return
82
- }
83
- for (const [key, item] of Object.entries(value)) visit(item, key)
51
+ apply = async (preload, width, current = {}, signal) => {
52
+ const next = { ...current }
53
+ for (const key of preload.delete || []) {
54
+ await this.remove(next[key])
55
+ delete next[key]
84
56
  }
85
- resources.forEach(value => {
86
- visit(value)
87
- const backdrop = value.picture || value.items?.[0]?.picture || value.groups?.[0]?.items?.[0]?.picture
88
- if (backdrop?.src && !backdrop.placeholder) pictures.push({ ...backdrop, profile: undefined })
57
+
58
+ const changed = [...(preload.replace || []), ...(preload.new || [])]
59
+ const resources = changed.filter(item => item.kind === 'resource')
60
+ await this.queue(resources, settings.concurrency, async item => {
61
+ if (!/^(data|page|site|lookup|menu)\//.test(item.path || '') || item.path.includes('..')) throw new Error('Invalid load resource: ' + item.path)
62
+ await client.cacheDataSet({ endpoint: resource.url(item.path), value: item.value, version: item.sha256 })
63
+ next[item.key] = item
64
+ })
65
+
66
+ const pictures = changed.filter(item => item.kind === 'image')
67
+ await this.queue(pictures, images.concurrency, async item => {
68
+ next[item.key] = await this.image(item, width, next[item.key], signal)
89
69
  })
90
- return pictures
70
+ return next
91
71
  }
92
72
 
93
- images = async (pictures, width, signal) => {
94
- const unique = new Map()
95
- for (const picture of pictures) {
96
- const size = picture.size || img.profile(picture.profile, width)
97
- const image = img.getImage({ ...picture, size })
98
- unique.set(image.src + ':' + image.version, image)
99
- }
100
- await this.queue([...unique.values()], images.concurrency, image => this.background(async () => {
101
- const options = { version: image.version, hash: image.hash, signal, background: true }
102
- if (await statics.image(image.src, 'get', options)) return
103
- await statics.image(image.src, 'set', options)
104
- }, signal))
73
+ resize = async (items, width, signal) => {
74
+ const next = { ...items }
75
+ const pictures = Object.values(items).filter(item => item.kind === 'image')
76
+ await this.queue(pictures, images.concurrency, async item => {
77
+ next[item.key] = await this.image(item, width, item, signal)
78
+ })
79
+ return next
105
80
  }
106
81
 
107
82
  }
package/package.json CHANGED
@@ -1,76 +1,76 @@
1
1
  {
2
- "version": "1.0.0",
3
- "name": "@fullstackunicorn/webapp",
4
- "author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
5
- "description": "Shared Web and Native runtime engines and core components for Fullstack Unicorn applications",
6
- "license": "MIT",
7
- "type": "module",
8
- "private": false,
9
- "readme": "package.md",
10
- "files": [
11
- "index.js",
12
- "index.dep.js",
13
- "imports/",
14
- "load/",
15
- "native/",
16
- "package.md"
17
- ],
18
- "repository": {
19
- "type": "git",
20
- "url": "https://gitlab.com/fullstackunicorn/webapp.git"
2
+ "version": "2.0.1",
3
+ "name": "@fullstackunicorn/webapp",
4
+ "author": "lucanigido (https://fullstackunicorn.dev/author/lucanigido)",
5
+ "description": "Shared Web and Native runtime engines and core components for Fullstack Unicorn applications",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "packageManager": "yarn@4.18.1",
9
+ "readme": "package.md",
10
+ "files": [
11
+ "index.js",
12
+ "index.dep.js",
13
+ "imports/",
14
+ "load/",
15
+ "native/",
16
+ "package.md"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://gitlab.com/fullstackunicorn/webapp.git"
21
+ },
22
+ "keywords": [
23
+ "react",
24
+ "webapp",
25
+ "runtime",
26
+ "fullstackunicorn"
27
+ ],
28
+ "imports": {
29
+ "#dep": {
30
+ "react-native": "./native/index.dep.js",
31
+ "default": "./index.dep.js"
21
32
  },
22
- "keywords": [
23
- "react",
24
- "webapp",
25
- "runtime",
26
- "fullstackunicorn"
27
- ],
28
- "imports": {
29
- "#dep": {
30
- "react-native": "./native/index.dep.js",
31
- "default": "./index.dep.js"
32
- },
33
- "#imports/*": {
34
- "react-native": "./native/imports/*",
35
- "default": "./imports/*"
36
- },
37
- "#load/*": "./load/*"
33
+ "#imports/*": {
34
+ "react-native": "./native/imports/*",
35
+ "default": "./imports/*"
38
36
  },
39
- "exports": {
40
- ".": {
41
- "react-native": "./native/index.js",
42
- "default": "./index.js"
43
- },
44
- "./index.dep.js": {
45
- "react-native": "./native/index.dep.js",
46
- "default": "./index.dep.js"
47
- },
48
- "./imports/*": {
49
- "react-native": "./native/imports/*",
50
- "default": "./imports/*"
51
- },
52
- "./load/*": "./load/*"
37
+ "#load/*": "./load/*"
38
+ },
39
+ "exports": {
40
+ ".": {
41
+ "react-native": "./native/index.js",
42
+ "default": "./index.js"
53
43
  },
54
- "scripts": {
55
- "test": "node --test tests/*.test.js"
44
+ "./index.dep.js": {
45
+ "react-native": "./native/index.dep.js",
46
+ "default": "./index.dep.js"
56
47
  },
57
- "peerDependencies": {
58
- "axios": "^1.20.0",
59
- "blurhash": "^2.0.5",
60
- "idb-keyval": "^6.3.0",
61
- "jotai": "^3.0.1",
62
- "jwt-decode": "^4.0.0",
63
- "jwt-encode": "^1.0.1",
64
- "object-hash": "^3.0.0",
65
- "qs": "^6.16.1",
66
- "react": "^19.3.1",
67
- "react-blurhash": "^0.3.0",
68
- "react-dom": "^19.3.1",
69
- "react-helmet-async": "^3.0.0",
70
- "react-router-dom": "^7.18.4",
71
- "uuid": "^14.0.3"
48
+ "./imports/*": {
49
+ "react-native": "./native/imports/*",
50
+ "default": "./imports/*"
72
51
  },
73
- "engines": {
74
- "node": ">=22.12.0"
75
- }
76
- }
52
+ "./load/*": "./load/*"
53
+ },
54
+ "scripts": {
55
+ "test": "node --test tests/*.test.js"
56
+ },
57
+ "peerDependencies": {
58
+ "axios": "^1.20.0",
59
+ "blurhash": "^2.0.5",
60
+ "idb-keyval": "^6.3.0",
61
+ "jotai": "^3.0.1",
62
+ "jwt-decode": "^4.0.0",
63
+ "jwt-encode": "^1.0.1",
64
+ "object-hash": "^3.0.0",
65
+ "qs": "^6.16.1",
66
+ "react": "^19.3.1",
67
+ "react-blurhash": "^0.3.0",
68
+ "react-dom": "^19.3.1",
69
+ "react-helmet-async": "^3.0.0",
70
+ "react-router-dom": "^7.18.4",
71
+ "uuid": "^14.0.3"
72
+ },
73
+ "engines": {
74
+ "node": ">=22.12.0"
75
+ }
76
+ }
package/package.md CHANGED
@@ -7,3 +7,10 @@ Shared Web and Native runtime engines and React core components used by Fullstac
7
7
  Applications import this package only through their canonical dependency barrel. Package-internal third-party imports are centralized in `index.dep.js`. Host-specific configuration, data, pages, routes and styles remain in the application.
8
8
 
9
9
  The load engine lives in `load/` and its component entry point is `load/Load.js`.
10
+ It reads host settings from `configure/load.json`, checks `/pulse`, reports a
11
+ complete or partial cache inventory to `POST /api/data/load/get`, applies only
12
+ `delete`, `replace` and `new` operations from the one BFF response, and resolves
13
+ initial image sizes through the host's `load/geometry.scope.js`. The shared
14
+ `Img` component measures its mounted container, treats `src + hash` as binary
15
+ identity and keeps the decoded displayed image visible until a changed pending
16
+ binary has decoded and cross-faded.