@warpfx/nui-core 0.3.2 → 0.3.4

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/index.d.ts CHANGED
@@ -13,6 +13,11 @@
13
13
  type MessageHandler = (data: any) => void;
14
14
  type Unsubscribe = () => void;
15
15
  type VisibilityHandler = (panel?: string) => void;
16
+ /** Structured error with a machine-readable code. */
17
+ declare class WarpError extends Error {
18
+ code: string;
19
+ constructor(code: string, message: string);
20
+ }
16
21
  declare class WarpBridge {
17
22
  private state;
18
23
  private syncHandlers;
@@ -88,4 +93,4 @@ declare class WarpBridge {
88
93
  private handleVisibility;
89
94
  }
90
95
 
91
- export { WarpBridge };
96
+ export { WarpBridge, WarpError };
package/dist/index.js CHANGED
@@ -1,4 +1,11 @@
1
1
  // src/index.ts
2
+ var WarpError = class extends Error {
3
+ code;
4
+ constructor(code, message) {
5
+ super(message);
6
+ this.code = code;
7
+ }
8
+ };
2
9
  var WarpBridge = class _WarpBridge {
3
10
  state = /* @__PURE__ */ new Map();
4
11
  syncHandlers = /* @__PURE__ */ new Map();
@@ -83,7 +90,8 @@ var WarpBridge = class _WarpBridge {
83
90
  if (current !== void 0) {
84
91
  try {
85
92
  callback(current);
86
- } catch {
93
+ } catch (e) {
94
+ console.error(`[WarpBridge] onSync handler error (${key}):`, e);
87
95
  }
88
96
  }
89
97
  return () => set.delete(callback);
@@ -111,9 +119,7 @@ var WarpBridge = class _WarpBridge {
111
119
  });
112
120
  const data = await res.json();
113
121
  if (!data.ok) {
114
- const err = new Error(data.error?.message ?? "RPC call failed");
115
- err.code = data.error?.code ?? "RPC_ERROR";
116
- throw err;
122
+ throw new WarpError(data.error?.code ?? "RPC_ERROR", data.error?.message ?? "RPC call failed");
117
123
  }
118
124
  return data.result;
119
125
  }
@@ -173,7 +179,8 @@ var WarpBridge = class _WarpBridge {
173
179
  for (const fn of handlers) {
174
180
  try {
175
181
  fn(data);
176
- } catch {
182
+ } catch (e) {
183
+ console.error(`[WarpBridge] Message handler error (${type}):`, e);
177
184
  }
178
185
  }
179
186
  }
@@ -182,25 +189,45 @@ var WarpBridge = class _WarpBridge {
182
189
  if (!deltas || !Array.isArray(deltas)) return;
183
190
  const modified = /* @__PURE__ */ new Set();
184
191
  const working = /* @__PURE__ */ new Map();
192
+ const indexes = /* @__PURE__ */ new Map();
185
193
  for (const delta of deltas) {
186
194
  const { table, op, key, row, rows } = delta;
187
195
  const keyField = key ?? "id";
188
196
  if (op === "full") {
189
197
  working.set(table, rows ?? []);
198
+ const idx = /* @__PURE__ */ new Map();
199
+ const arr = working.get(table);
200
+ for (let i = 0; i < arr.length; i++) idx.set(arr[i][keyField], i);
201
+ indexes.set(table, idx);
190
202
  } else {
191
203
  if (!working.has(table)) {
192
- working.set(table, [...this.state.get(table) ?? []]);
204
+ const src = this.state.get(table) ?? [];
205
+ const arr2 = [...src];
206
+ working.set(table, arr2);
207
+ const idx2 = /* @__PURE__ */ new Map();
208
+ for (let i = 0; i < arr2.length; i++) idx2.set(arr2[i][keyField], i);
209
+ indexes.set(table, idx2);
193
210
  }
194
211
  const arr = working.get(table);
212
+ const idx = indexes.get(table);
195
213
  if (op === "insert") {
214
+ idx.set(row[keyField], arr.length);
196
215
  arr.push(row);
197
216
  } else if (op === "update") {
198
- const idx = arr.findIndex((r) => r[keyField] === row[keyField]);
199
- if (idx >= 0) arr[idx] = row;
200
- else arr.push(row);
217
+ const pos = idx.get(row[keyField]);
218
+ if (pos !== void 0) {
219
+ arr[pos] = row;
220
+ } else {
221
+ idx.set(row[keyField], arr.length);
222
+ arr.push(row);
223
+ }
201
224
  } else if (op === "delete") {
202
- const idx = arr.findIndex((r) => r[keyField] === row[keyField]);
203
- if (idx >= 0) arr.splice(idx, 1);
225
+ const pos = idx.get(row[keyField]);
226
+ if (pos !== void 0) {
227
+ arr.splice(pos, 1);
228
+ idx.clear();
229
+ for (let i = 0; i < arr.length; i++) idx.set(arr[i][keyField], i);
230
+ }
204
231
  }
205
232
  }
206
233
  modified.add(table);
@@ -215,7 +242,8 @@ var WarpBridge = class _WarpBridge {
215
242
  for (const fn of handlers) {
216
243
  try {
217
244
  fn(value);
218
- } catch {
245
+ } catch (e) {
246
+ console.error(`[WarpBridge] Sync handler error (${table}):`, e);
219
247
  }
220
248
  }
221
249
  }
@@ -228,19 +256,22 @@ var WarpBridge = class _WarpBridge {
228
256
  for (const fn of this.showHandlers) {
229
257
  try {
230
258
  fn(panel);
231
- } catch {
259
+ } catch (e) {
260
+ console.error("[WarpBridge] onShow handler error:", e);
232
261
  }
233
262
  }
234
263
  } else {
235
264
  for (const fn of this.hideHandlers) {
236
265
  try {
237
266
  fn();
238
- } catch {
267
+ } catch (e) {
268
+ console.error("[WarpBridge] onHide handler error:", e);
239
269
  }
240
270
  }
241
271
  }
242
272
  }
243
273
  };
244
274
  export {
245
- WarpBridge
275
+ WarpBridge,
276
+ WarpError
246
277
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warpfx/nui-core",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "Warp Framework — Framework-agnostic NUI bridge for FiveM",
5
5
  "keywords": [
6
6
  "fivem",