📄 src/models/worker-patch-main.ts
D-OPEN SOVEREIGN

Documentation & Insights

LRU limit per objectType — default 10 000 (use ~2 000 on mobile).
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 { WorkerCommands } from "./enums.ts";
16import type { LocalOp, QueueItem, SidecarUrls, Lang } from "./interfaces.ts";
17import { isEvmPatch } from "./interfaces.ts";
18import { initWorker, Thread } from "./worker-client.ts";
19import { Patches, PatchInstance } from "./patcher.ts";
20import { PopulateStatsStore } from "./stats.ts";
21import { getAllPatchesByUsername } from "./indexdb/patch_storage.ts";
22import { configureDb } from "./db.ts";
23import type { SidecarSet } from "./worker/libs/initialLoad.ts";
24
25let _worker: Thread | null = null;
26
27export interface InitWorkersOptions {
28    workerPath?: string | URL,
29    sidecars: Array<SidecarUrls>,
30    selectedLangs: Lang[],
31    /** LRU limit per objectType — default 10 000 (use ~2 000 on mobile). */
32    cacheLimit?: number,
33}
34
35export const InitPatchWorker = async (options: InitWorkersOptions): Promise<{
36    sidecars: Array<SidecarSet>,
37    patches: { [user: string]: Array<QueueItem> },
38}> => {
39    if (options.cacheLimit) {
40        configureDb({ cacheLimit: options.cacheLimit })
41    }
42    if (_worker === null) {
43        _worker = await initWorker(options.workerPath ?? "/patch-worker.js");
44    }
45    PatchInstance.initThread(_worker)
46    const data = await _worker.sendRequest({
47        cmd: WorkerCommands.Init,
48        payload: {
49            sidecars: options.sidecars,
50            selectedLangs: options.selectedLangs,
51        }
52    }) as any;
53
54    // Local analytics ops rebuild the stats store on boot
55    const mine = await getAllPatchesByUsername("mine")
56    PopulateStatsStore(mine.filter(p => !isEvmPatch(p)) as Array<LocalOp>)
57
58    await Patches.calculateLevel("mine")
59    console.log(`InitPatchWorker() done`)
60    return data
61};
62
63export const SendPatches = async (username: string, patches: Array<QueueItem>): Promise<boolean> => {
64    if (_worker === null) {
65        console.error(`SendPatches called before InitPatchWorker`)
66        return false
67    }
68    try {
69        await _worker.sendRequest({
70            cmd: WorkerCommands.SavePatches,
71            payload: {
72                username,
73                patches
74            }
75        }) as any;
76        return true
77    } catch (e) {
78        console.error(`SendPatches failed`, e)
79        return false
80    }
81}
82
83export const LoadLanguageWorkerCmd = async (lang: string, sidecars: Array<SidecarUrls>): Promise<{ sidecars: Array<SidecarSet> }> => {
84    if (_worker === null) {
85        throw new Error(`LoadLanguageWorkerCmd called before InitPatchWorker`);
86    }
87    const data = await _worker.sendRequest({
88        cmd: WorkerCommands.LoadLanguage,
89        payload: { lang, sidecars }
90    }) as any;
91    return data;
92}
93