📄 src/models/worker/libs/initialLoad.ts
D-OPEN SOVEREIGN

Documentation & Insights

uid -> array index, from the `ids` column of the slim tier.
Worker-side retained sidecar data, for GetRecord re-hydration.
Fetches and parses all sidecar tiers off the main thread, retains them in
worker memory (for GetRecord), and returns the parsed structures plus all
locally stored patches. Maps and TypedArrays survive the structured clone.
Single-record extraction for LRU re-hydration (WorkerCommands.GetRecord).
Returns cols objects whose arrays hold exactly one entry — the main thread
merges them with `merge*(cols, 0, lang)`.
1/*
2 * Copyright (c) 2026 DataPond D-Library Pty Ltd. ("The Sanctuary")
3 * Non-Profit Public Library — The World Library
4 * 
5 * D-SAFE Certification issued by POND Enterprise.
6 * Published under the D-Open Code Sovereign Licence v1.0 framework
7 * DataPond D-Library All exclusive Right reserved on modifiying the code - CC Attribution to data pond, Non modifiable, Non Commercial.
8 * https://registry.world.bibliotech.com/licence
9 * Source code donated to DataPond D-Library by it's author: data pond.
10 * 
11 * Technical Guardian ("The Shield"): Pond Enterprise Pty Ltd. (ACN 694 747 987)
12 * All liability claims about D-SAFE certification issues coming from the published D-Safe direction must be addressed with Pond Enterprise Pty Ltd.
13 * Code Modification automatically voids the certified liability protection provided by Pond Enterprise.
14 */
15import type { Lang, SidecarUrls } from "../../interfaces.ts";
16import type { StoredPatch } from "../../indexdb/patch_storage.ts";
17import { getAllPatches } from "../../indexdb/patch_storage.ts";
18import { parseSidecar, extractRecordCols, type ParsedSidecar } from "../../enriched-index.ts";
19
20export interface SidecarSet {
21    objectType: number;
22    lang: Lang;
23    slim?: ParsedSidecar;
24    extended?: ParsedSidecar;
25    prov?: ParsedSidecar;
26    /** uid -> array index, from the `ids` column of the slim tier. */
27    idIndex: Map<number, number>;
28}
29
30/** Worker-side retained sidecar data, for GetRecord re-hydration. */
31export const sidecarStore = new Map<number, Map<Lang, SidecarSet>>();
32
33let currentLangs: Lang[] = ['eng'];
34export const getCurrentLangs = (): Lang[] => currentLangs;
35
36const fetchAndParse = async (url: string): Promise<ParsedSidecar> => {
37    const response = await fetch(url);
38    if (!response.ok) {
39        throw new Error(`failed to fetch sidecar ${url}: ${response.status}`);
40    }
41    return parseSidecar(await response.arrayBuffer());
42}
43
44const buildIdIndex = (parsed: ParsedSidecar | undefined): Map<number, number> => {
45    const map = new Map<number, number>();
46    const ids = parsed?.columns.get('ids');
47    if (ids && !Array.isArray(ids)) {
48        for (let i = 0; i < ids.length; i++) {
49            map.set(ids[i], i);
50        }
51    }
52    return map;
53}
54
55/**
56 * Fetches and parses all sidecar tiers off the main thread, retains them in
57 * worker memory (for GetRecord), and returns the parsed structures plus all
58 * locally stored patches. Maps and TypedArrays survive the structured clone.
59 */
60export const initialLoad = async (sidecars: Array<SidecarUrls>, selectedLangs: Lang[]): Promise<{
61    sidecars: Array<SidecarSet>,
62    patches: { [user: string]: Array<StoredPatch> },
63}> => {
64    console.log(`[worker] initialLoad started: ${sidecars.length} sidecar set(s), langs=${selectedLangs.join(',')}`)
65    currentLangs = selectedLangs;
66    sidecarStore.clear();
67
68    const sets: Array<SidecarSet> = [];
69    for (const { objectType, lang, slimUrl, extendedUrl, provUrl } of sidecars) {
70        const [slim, extended, prov] = await Promise.all([
71            slimUrl ? fetchAndParse(slimUrl) : Promise.resolve(undefined),
72            extendedUrl ? fetchAndParse(extendedUrl) : Promise.resolve(undefined),
73            provUrl ? fetchAndParse(provUrl) : Promise.resolve(undefined),
74        ]);
75        const set: SidecarSet = {
76            objectType,
77            lang,
78            slim,
79            extended,
80            prov,
81            idIndex: buildIdIndex(slim ?? extended),
82        };
83        
84        let byLang = sidecarStore.get(objectType);
85        if (!byLang) {
86            byLang = new Map<Lang, SidecarSet>();
87            sidecarStore.set(objectType, byLang);
88        }
89        byLang.set(lang, set);
90        sets.push(set);
91    }
92
93    console.log(`[worker] fetching local patches...`)
94    const patches = await getAllPatches();
95
96    console.log(`[worker] initialLoad complete`)
97    return { sidecars: sets, patches };
98}
99
100/**
101 * Single-record extraction for LRU re-hydration (WorkerCommands.GetRecord).
102 * Returns cols objects whose arrays hold exactly one entry — the main thread
103 * merges them with `merge*(cols, 0, lang)`.
104 */
105export const getRecordCols = (objectType: number, id: number): Array<{
106    lang: Lang,
107    slim?: Record<string, any>,
108    extended?: Record<string, any>,
109    prov?: Record<string, any>,
110}> => {
111    const out: Array<{
112        lang: Lang,
113        slim?: Record<string, any>,
114        extended?: Record<string, any>,
115        prov?: Record<string, any>,
116    }> = [];
117
118    const byLang = sidecarStore.get(objectType);
119    if (!byLang) return out;
120
121    for (const lang of currentLangs) {
122        const set = byLang.get(lang);
123        if (!set) continue;
124
125        const index = set.idIndex.get(id);
126        if (typeof index === 'undefined') continue;
127
128        out.push({
129            lang,
130            slim: set.slim ? extractRecordCols(set.slim, index) : undefined,
131            extended: set.extended ? extractRecordCols(set.extended, index) : undefined,
132            prov: set.prov ? extractRecordCols(set.prov, index) : undefined,
133        });
134    }
135
136    return out;
137}
138