aboutsummaryrefslogtreecommitdiff
path: root/loading/content.ts
blob: d3181fabb0a74b4865a3f621dce6dfb2a088eb05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { fetchManifest } from '@hollowdark/content-system/loader/loader'
import { populateFromManifest } from '@hollowdark/content-system/registry/registry'
import { setContentRegistry } from '@hollowdark/content-system/registry/singleton'
import { loadingProgress } from '@hollowdark/loading/progress'

/**
 * Run the real initial-load pipeline. Fetches the content manifest,
 * walks every chunk into the shared registry, and reports progress to
 * the loading store so the initial-load screen's bar moves in real
 * time. Falls back to whatever is cached when offline.
 *
 * `baseUrl` is the app's base path — empty string in local dev, the
 * hosting sub-path under GitHub Pages.
 */
export async function runInitialLoad(baseUrl: string): Promise<void> {
  loadingProgress.set({
    percentage: 0,
    currentMessage: 'Preparing your reading space',
    phase: 'loading'
  })

  const manifest = await fetchManifest(baseUrl)
  const chunkCount = Object.keys(manifest.chunks).length

  if (chunkCount === 0) {
    setContentRegistry(
      await populateFromManifest(manifest, baseUrl, () => {})
    )
    loadingProgress.set({
      percentage: 1,
      currentMessage: 'Preparing your reading space',
      phase: 'complete'
    })
    return
  }

  const registry = await populateFromManifest(manifest, baseUrl, (loaded, total) => {
    loadingProgress.set({
      percentage: loaded / total,
      currentMessage: 'Preparing your reading space',
      phase: loaded === total ? 'complete' : 'loading'
    })
  })

  setContentRegistry(registry)
}