@qikdev/sdk 0.1.208 → 0.1.209

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.209",
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;
83
+ }
84
+
85
+ /**
86
+ * Get or create the inflate worker instance
87
+ * @private
88
+ */
89
+ async function getInflateWorker() {
90
+ if (inflateWorker) return inflateWorker;
91
+ const module = await loadWorkerModule();
92
+ inflateWorker = new module.default();
93
+ return inflateWorker;
82
94
  }
83
95
 
84
96
  /**
85
- * Inflate compressed glossary data from the /glossary/compressed endpoint
86
- * Processes in chunks to avoid blocking the main thread
97
+ * Sync fallback for SSR/Node environments where Workers don't exist
87
98
  * @private
88
99
  */
89
- async function inflateGlossary({ _s: strings, _k: keyMap, types }) {
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,45 @@ export default function (qik) {
101
112
  }
102
113
  return val;
103
114
  }
115
+ return types.map(inflate);
116
+ }
104
117
 
105
- const CHUNK_SIZE = 50;
106
- const results = [];
118
+ /**
119
+ * Inflate compressed glossary data using a Web Worker
120
+ * Runs on a separate thread to avoid blocking the UI
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
+ }
107
128
 
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));
129
+ try {
130
+ const worker = await getInflateWorker();
111
131
 
112
- // Yield to main thread between chunks
113
- if (i + CHUNK_SIZE < types.length) {
114
- await yieldToMain();
115
- }
116
- }
132
+ return new Promise((resolve) => {
133
+ const handler = (e) => {
134
+ worker.removeEventListener("message", handler);
135
+ worker.removeEventListener("error", errorHandler);
136
+ resolve(e.data);
137
+ };
117
138
 
118
- return results;
139
+ const errorHandler = () => {
140
+ worker.removeEventListener("message", handler);
141
+ worker.removeEventListener("error", errorHandler);
142
+ // Fallback to sync on error
143
+ resolve(inflateGlossarySync(data));
144
+ };
145
+
146
+ worker.addEventListener("message", handler);
147
+ worker.addEventListener("error", errorHandler);
148
+ worker.postMessage(data);
149
+ });
150
+ } catch {
151
+ // Dynamic import failed (Node.js or unsupported environment)
152
+ return inflateGlossarySync(data);
153
+ }
119
154
  }
120
155
 
121
156
  /**
package/src/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.1.208";
1
+ export const version = "0.1.209";
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Web Worker for inflating compressed glossary data
3
+ * Runs on a separate thread to avoid blocking the main UI
4
+ */
5
+ self.onmessage = 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
+ const result = types.map(inflate);
24
+ self.postMessage(result);
25
+ };