@qikdev/sdk 0.1.208 → 0.1.210

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qikdev/sdk",
3
- "version": "0.1.208",
3
+ "version": "0.1.210",
4
4
  "type": "module",
5
5
  "description": "Promise based Javascript SDK",
6
6
  "main": "./dist/main.js",
@@ -67,26 +67,37 @@ export default function (qik) {
67
67
 
68
68
  let inflightGlossaryRequest;
69
69
 
70
+ let inflateWorker = null;
71
+ let workerLoadPromise = null;
72
+
70
73
  /**
71
- * Yield to the event loop to keep the UI responsive
74
+ * Lazily load the Worker module (Vite-specific import)
72
75
  * @private
73
76
  */
74
- function yieldToMain() {
75
- return new Promise((resolve) => {
76
- if (typeof requestIdleCallback === "function") {
77
- requestIdleCallback(resolve, { timeout: 50 });
78
- } else {
79
- setTimeout(resolve, 0);
80
- }
81
- });
77
+ async function loadWorkerModule() {
78
+ if (workerLoadPromise) return workerLoadPromise;
79
+ workerLoadPromise = import(
80
+ "../workers/inflate-glossary.worker.js?worker&inline"
81
+ );
82
+ return workerLoadPromise;
82
83
  }
83
84
 
84
85
  /**
85
- * Inflate compressed glossary data from the /glossary/compressed endpoint
86
- * Processes in chunks to avoid blocking the main thread
86
+ * Get or create the inflate worker instance
87
87
  * @private
88
88
  */
89
- async function inflateGlossary({ _s: strings, _k: keyMap, types }) {
89
+ async function getInflateWorker() {
90
+ if (inflateWorker) return inflateWorker;
91
+ const module = await loadWorkerModule();
92
+ inflateWorker = new module.default();
93
+ return inflateWorker;
94
+ }
95
+
96
+ /**
97
+ * Sync fallback for SSR/Node environments where Workers don't exist
98
+ * @private
99
+ */
100
+ function inflateGlossarySync({ _s: strings, _k: keyMap, types }) {
90
101
  function inflate(val) {
91
102
  if (typeof val === "string" && val[0] === "~") {
92
103
  return strings[parseInt(val.slice(1))];
@@ -101,21 +112,52 @@ export default function (qik) {
101
112
  }
102
113
  return val;
103
114
  }
115
+ return types.map(inflate);
116
+ }
117
+
118
+ /**
119
+ * Inflate compressed glossary data using a Web Worker
120
+ * Receives results in chunks to avoid blocking during deserialization
121
+ * @private
122
+ */
123
+ async function inflateGlossary(data) {
124
+ // Fallback for non-browser environments (SSR/Node)
125
+ if (typeof Worker === "undefined") {
126
+ return inflateGlossarySync(data);
127
+ }
104
128
 
105
- const CHUNK_SIZE = 50;
106
- const results = [];
129
+ try {
130
+ const worker = await getInflateWorker();
107
131
 
108
- for (let i = 0; i < types.length; i += CHUNK_SIZE) {
109
- const chunk = types.slice(i, i + CHUNK_SIZE);
110
- results.push(...chunk.map(inflate));
132
+ return new Promise((resolve) => {
133
+ const results = [];
111
134
 
112
- // Yield to main thread between chunks
113
- if (i + CHUNK_SIZE < types.length) {
114
- await yieldToMain();
115
- }
116
- }
135
+ const handler = (e) => {
136
+ const { chunk, done } = e.data;
137
+ results.push(...chunk);
117
138
 
118
- return results;
139
+ if (done) {
140
+ worker.removeEventListener("message", handler);
141
+ worker.removeEventListener("error", errorHandler);
142
+ resolve(results);
143
+ }
144
+ };
145
+
146
+ const errorHandler = () => {
147
+ worker.removeEventListener("message", handler);
148
+ worker.removeEventListener("error", errorHandler);
149
+ // Fallback to sync on error
150
+ resolve(inflateGlossarySync(data));
151
+ };
152
+
153
+ worker.addEventListener("message", handler);
154
+ worker.addEventListener("error", errorHandler);
155
+ worker.postMessage(data);
156
+ });
157
+ } catch {
158
+ // Dynamic import failed (Node.js or unsupported environment)
159
+ return inflateGlossarySync(data);
160
+ }
119
161
  }
120
162
 
121
163
  /**
package/src/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.1.208";
1
+ export const version = "0.1.210";
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Web Worker for inflating compressed glossary data
3
+ * Streams results back in chunks with delays to avoid blocking the main thread
4
+ */
5
+ self.onmessage = async function (e) {
6
+ const { _s: strings, _k: keyMap, types } = e.data;
7
+
8
+ function inflate(val) {
9
+ if (typeof val === "string" && val[0] === "~") {
10
+ return strings[parseInt(val.slice(1))];
11
+ }
12
+ if (Array.isArray(val)) return val.map(inflate);
13
+ if (val && typeof val === "object") {
14
+ const result = {};
15
+ for (const [k, v] of Object.entries(val)) {
16
+ result[keyMap[k] || k] = inflate(v);
17
+ }
18
+ return result;
19
+ }
20
+ return val;
21
+ }
22
+
23
+ // Send results back in chunks with small delays
24
+ // This lets the main thread process each chunk before receiving the next
25
+ const CHUNK_SIZE = 20;
26
+ const total = types.length;
27
+
28
+ for (let i = 0; i < total; i += CHUNK_SIZE) {
29
+ const chunk = types.slice(i, i + CHUNK_SIZE).map(inflate);
30
+ const done = i + CHUNK_SIZE >= total;
31
+ self.postMessage({ chunk, done });
32
+
33
+ // Small delay between chunks to let main thread breathe
34
+ if (!done) {
35
+ await new Promise((r) => setTimeout(r, 0));
36
+ }
37
+ }
38
+ };