๐ src/models/worker/index.ts
D-OPEN SOVEREIGN
Documentation & Insights
Fetches and parses sidecar binaries off the main thread,
returns parsed cols + locally stored patches.Called automatically (debounced) every time a change is made
in the frontend.Single-record cols extraction for LRU re-hydration (SPEC ยง2.5).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 { ThreadWorker } from "./libs/threadWorker.ts";
16import { bulkAddPatches } from "../indexdb/patch_storage.ts";
17import { initialLoad, getRecordCols } from "./libs/initialLoad.ts";
18import { Db } from "../indexdb/common.ts";
19import type { InitPayload, SavePatchesPayload, GetRecordPayload, LoadLanguagePayload } from "../interfaces.ts";
20import { WorkerCommands, WorkerEvent } from "../enums.ts";
21
22class PatchWorker extends ThreadWorker {
23
24 constructor() {
25 super();
26 this.postEvent(WorkerEvent.WorkerReady, true)
27 }
28
29 onRequest(id: string, data: InitPayload | SavePatchesPayload | GetRecordPayload | LoadLanguagePayload) {
30
31 const { cmd, payload } = data;
32 console.log(`[patch-worker] received request: ${id} - ${cmd}`)
33
34 switch (cmd) {
35 /**
36 * Fetches and parses sidecar binaries off the main thread,
37 * returns parsed cols + locally stored patches.
38 */
39 case WorkerCommands.Init: {
40 const { sidecars, selectedLangs } = payload as InitPayload['payload'];
41 initialLoad(sidecars, selectedLangs).then((result) => {
42 this.sendResponse(id, result as any)
43 }).catch((e) => {
44 console.error(`[patch-worker] caught error: ${e.message}`)
45 this.sendError(id, e)
46 });
47 break;
48 }
49 /**
50 * Called automatically (debounced) every time a change is made
51 * in the frontend.
52 */
53 case WorkerCommands.SavePatches: {
54 const { username, patches } = payload as SavePatchesPayload['payload'];
55 bulkAddPatches(username, patches).then(() => {
56 this.sendResponse(id)
57 }).catch((e) => {
58 console.error(`[patch-worker] caught error: ${e.message}`)
59 this.sendError(id, e)
60 });
61 break;
62 }
63 /**
64 * Single-record cols extraction for LRU re-hydration (SPEC ยง2.5).
65 */
66 case WorkerCommands.GetRecord: {
67 const { objectType, id: recordId } = payload as GetRecordPayload['payload'];
68 try {
69 this.sendResponse(id, getRecordCols(objectType, recordId) as any)
70 } catch (e: any) {
71 console.error(`[patch-worker] caught error: ${e.message}`)
72 this.sendError(id, e)
73 }
74 break;
75 }
76 case WorkerCommands.LoadLanguage: {
77 // @ts-ignore -- LoadLanguagePayload is handled but type check complains if it's missing from import
78 const { lang, sidecars } = payload;
79 initialLoad(sidecars, [lang]).then((result) => {
80 this.sendResponse(id, result as any)
81 }).catch((e) => {
82 console.error(`[patch-worker] LoadLanguage error: ${e.message}`)
83 this.sendError(id, e)
84 });
85 break;
86 }
87 default:
88 console.error(`[worker] unknown action: ${cmd}`)
89 }
90 }
91}
92
93const LaunchDbWorker = async (): Promise<true> => {
94 console.log('starting models_v2 patch worker...')
95 await Db()
96 new PatchWorker()
97 console.log('patch worker instantiated')
98 return true
99}
100
101LaunchDbWorker().then(console.log).catch(console.error)
102