@shuo-li/i18n 1.0.3 → 1.0.5
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/dist/{cacheEvents-CDBRPnHU.d.cts → cacheEvents-CSwgzbob.d.cts} +6 -15
- package/dist/{cacheEvents-CDBRPnHU.d.ts → cacheEvents-CSwgzbob.d.ts} +6 -15
- package/dist/{chunk-K5VX3COH.cjs → chunk-JPKSBSEO.cjs} +136 -129
- package/dist/{chunk-IWP4252C.js → chunk-X6D2MZ7M.js} +124 -117
- package/dist/index.cjs +7 -46
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -43
- package/dist/mp/index.cjs +9 -43
- package/dist/mp/index.d.cts +2 -2
- package/dist/mp/index.d.ts +2 -2
- package/dist/mp/index.js +7 -41
- package/dist/native/index.cjs +10 -44
- package/dist/native/index.d.cts +2 -2
- package/dist/native/index.d.ts +2 -2
- package/dist/native/index.js +7 -41
- package/dist/workers/preload-worker-rn.cjs +12 -55
- package/dist/workers/preload-worker-rn.js +12 -55
- package/dist/workers/preload-worker.cjs +4 -128
- package/dist/workers/preload-worker.js +3 -127
- package/package.json +1 -1
|
@@ -1,85 +1,11 @@
|
|
|
1
1
|
// src/workers/preload-worker.ts
|
|
2
|
-
function flatToNested(flat) {
|
|
3
|
-
const result = {};
|
|
4
|
-
for (const [key, value] of Object.entries(flat)) {
|
|
5
|
-
const parts = key.split(".");
|
|
6
|
-
let cur = result;
|
|
7
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
8
|
-
const part = parts[i];
|
|
9
|
-
if (typeof cur[part] !== "object" || cur[part] === null) cur[part] = {};
|
|
10
|
-
cur = cur[part];
|
|
11
|
-
}
|
|
12
|
-
cur[parts[parts.length - 1]] = value;
|
|
13
|
-
}
|
|
14
|
-
return result;
|
|
15
|
-
}
|
|
16
|
-
function deepMerge(target, source) {
|
|
17
|
-
const result = { ...target };
|
|
18
|
-
for (const [key, sv] of Object.entries(source)) {
|
|
19
|
-
const tv = result[key];
|
|
20
|
-
if (sv !== null && typeof sv === "object" && !Array.isArray(sv) && tv !== null && typeof tv === "object" && !Array.isArray(tv)) {
|
|
21
|
-
result[key] = deepMerge(tv, sv);
|
|
22
|
-
} else {
|
|
23
|
-
result[key] = sv;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
function parseI18nValues(values) {
|
|
29
|
-
const result = {};
|
|
30
|
-
for (const { termCode, langValue } of values) {
|
|
31
|
-
if (termCode && langValue !== void 0) result[termCode] = langValue ?? "";
|
|
32
|
-
}
|
|
33
|
-
return result;
|
|
34
|
-
}
|
|
35
|
-
function buildKey(moduleCode, langCode, store) {
|
|
36
|
-
return store.cacheGroupKey ? `${moduleCode}_${langCode}_${store.cacheGroupKey}` : `${moduleCode}_${langCode}`;
|
|
37
|
-
}
|
|
38
2
|
function toQueryString(params) {
|
|
39
3
|
return new URLSearchParams(
|
|
40
4
|
Object.entries(params).filter(([, v]) => v != null).map(([k, v]) => [k, String(v)])
|
|
41
5
|
).toString();
|
|
42
6
|
}
|
|
43
|
-
var db = null;
|
|
44
|
-
function openDB(dbName, storeNames) {
|
|
45
|
-
return new Promise((resolve, reject) => {
|
|
46
|
-
const req = indexedDB.open(dbName, 2);
|
|
47
|
-
req.onupgradeneeded = (event) => {
|
|
48
|
-
const database = event.target.result;
|
|
49
|
-
for (const name of storeNames) {
|
|
50
|
-
const storeName = `i18n_resources_${name}`;
|
|
51
|
-
if (!database.objectStoreNames.contains(storeName)) {
|
|
52
|
-
const s = database.createObjectStore(storeName, { keyPath: "key" });
|
|
53
|
-
s.createIndex("by_module", "moduleCode");
|
|
54
|
-
s.createIndex("by_language", "langCode");
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
if (!database.objectStoreNames.contains("i18n_meta")) {
|
|
58
|
-
database.createObjectStore("i18n_meta", { keyPath: "key" });
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
req.onsuccess = () => resolve(req.result);
|
|
62
|
-
req.onerror = () => reject(req.error);
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
function idbGet(database, storeName, key) {
|
|
66
|
-
return new Promise((resolve, reject) => {
|
|
67
|
-
const tx = database.transaction(storeName, "readonly");
|
|
68
|
-
const req = tx.objectStore(storeName).get(key);
|
|
69
|
-
req.onsuccess = () => resolve(req.result);
|
|
70
|
-
req.onerror = () => reject(req.error);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
function idbPut(database, storeName, value) {
|
|
74
|
-
return new Promise((resolve, reject) => {
|
|
75
|
-
const tx = database.transaction(storeName, "readwrite");
|
|
76
|
-
const req = tx.objectStore(storeName).put(value);
|
|
77
|
-
req.onsuccess = () => resolve();
|
|
78
|
-
req.onerror = () => reject(req.error);
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
7
|
async function processTask(task, payload) {
|
|
82
|
-
const { baseURL, headers, pullPath, pullMethod
|
|
8
|
+
const { baseURL, headers, pullPath, pullMethod } = payload;
|
|
83
9
|
const params = task.params ?? {};
|
|
84
10
|
if (!task.params) {
|
|
85
11
|
if (task.storeName) params["storeName"] = task.storeName;
|
|
@@ -100,62 +26,12 @@ async function processTask(task, payload) {
|
|
|
100
26
|
res = await fetch(qs ? `${url}?${qs}` : url, { headers });
|
|
101
27
|
}
|
|
102
28
|
if (!res.ok) throw new Error(`[i18n worker] fetch failed: ${res.status}`);
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
for (const mod of block.modules ?? []) {
|
|
106
|
-
const store = stores.find((s) => s.name === task.storeName);
|
|
107
|
-
if (!store) continue;
|
|
108
|
-
const storeIndex = stores.indexOf(store);
|
|
109
|
-
const idbStoreName = `i18n_resources_${store.name}`;
|
|
110
|
-
const key = buildKey(mod.moduleCode, block.langCode, store);
|
|
111
|
-
const parsed = parseI18nValues(mod.i18nValues ?? []);
|
|
112
|
-
let resources;
|
|
113
|
-
if (storeIndex === 0) {
|
|
114
|
-
const existing = await idbGet(
|
|
115
|
-
db,
|
|
116
|
-
idbStoreName,
|
|
117
|
-
key
|
|
118
|
-
);
|
|
119
|
-
resources = deepMerge(
|
|
120
|
-
existing?.resources ?? {},
|
|
121
|
-
flatToNested(parsed)
|
|
122
|
-
);
|
|
123
|
-
} else {
|
|
124
|
-
const existing = await idbGet(
|
|
125
|
-
db,
|
|
126
|
-
idbStoreName,
|
|
127
|
-
key
|
|
128
|
-
);
|
|
129
|
-
resources = { ...existing?.resources ?? {}, ...parsed };
|
|
130
|
-
}
|
|
131
|
-
const record = {
|
|
132
|
-
key,
|
|
133
|
-
moduleCode: mod.moduleCode,
|
|
134
|
-
langCode: block.langCode,
|
|
135
|
-
version: mod.version,
|
|
136
|
-
resources
|
|
137
|
-
};
|
|
138
|
-
await idbPut(db, idbStoreName, record);
|
|
139
|
-
self.postMessage({
|
|
140
|
-
type: "moduleLoaded",
|
|
141
|
-
storeName: task.storeName,
|
|
142
|
-
langCode: block.langCode,
|
|
143
|
-
moduleCode: mod.moduleCode,
|
|
144
|
-
version: mod.version
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
29
|
+
const raw = await res.json();
|
|
30
|
+
self.postMessage({ type: "rawData", task, raw });
|
|
148
31
|
}
|
|
149
32
|
self.onmessage = async (event) => {
|
|
150
33
|
if (event.data.type !== "start") return;
|
|
151
34
|
const { payload } = event.data;
|
|
152
|
-
const storeNames = payload.stores.map((s) => s.name);
|
|
153
|
-
try {
|
|
154
|
-
db = await openDB(payload.dbName, storeNames);
|
|
155
|
-
} catch (err) {
|
|
156
|
-
self.postMessage({ type: "error", message: String(err) });
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
35
|
for (const task of payload.tasks) {
|
|
160
36
|
try {
|
|
161
37
|
await processTask(task, payload);
|