@warpfx/client 0.3.0 → 0.3.2

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
@@ -1,14 +1,21 @@
1
1
  /**
2
- * @warpfx/client — Client-side SDK for Warp module development.
2
+ * @warpfx/client — Client-side SDK for Warp resource development.
3
3
  *
4
4
  * Clean, minimal API for building FiveM client features with Warp.
5
5
  * All calls delegate to the Warp core via FiveM resource exports.
6
6
  *
7
7
  * Usage:
8
8
  * import { accounts } from './db'; // auto-generated reactive store
9
- * import { action, call } from '@warpfx/client';
9
+ * import { action, call, watch } from '@warpfx/client';
10
+ *
11
+ * accounts.watch((newVal, oldVal) => {
12
+ * if (newVal[0]?.balance < oldVal[0]?.balance) showNotification('Balance decreased!');
13
+ * });
14
+ *
15
+ * watch([accounts, inventory], ([acc, inv], [prevAcc, prevInv]) => {
16
+ * console.log('Something changed');
17
+ * });
10
18
  *
11
- * accounts.watch((data) => console.log('Balance:', data[0].balance));
12
19
  * action('deposit', { amount: 100 });
13
20
  * const { balance } = await call('getBalance', { targetId: 'xyz' });
14
21
  */
@@ -75,6 +82,24 @@ declare function call<T = any>(name: string, payload?: any, timeout?: number): P
75
82
  * console.log(accounts.value);
76
83
  */
77
84
  declare function sync<T = any>(key: string, defaultValue?: T): SyncRef<T>;
85
+ /**
86
+ * Watch multiple sync refs and react when any of them change.
87
+ * Batches changes from the same tick into a single callback.
88
+ * Returns an unsubscribe function.
89
+ *
90
+ * @example
91
+ * const accounts = sync<Account[]>('accounts');
92
+ * const inventory = sync<Item[]>('inventory');
93
+ *
94
+ * const unsub = watch([accounts, inventory], ([acc, inv], [prevAcc, prevInv]) => {
95
+ * console.log('Something changed');
96
+ * });
97
+ */
98
+ declare function watch<T extends readonly SyncRef[]>(refs: readonly [...T], handler: (newValues: {
99
+ [K in keyof T]: T[K] extends SyncRef<infer V> ? V : never;
100
+ }, oldValues: {
101
+ [K in keyof T]: T[K] extends SyncRef<infer V> ? V : never;
102
+ }) => void): () => void;
78
103
  /**
79
104
  * Register a global error handler for all action errors.
80
105
  * Returns an unsubscribe function.
@@ -120,4 +145,4 @@ declare function onNUI<T = any>(name: string, handler: (data: T) => any): void;
120
145
  */
121
146
  declare function registerKeybind(name: string, options: KeybindOptions): void;
122
147
 
123
- export { type ActionError, type ClientEventBus, type KeybindOptions, type ShowNUIOptions, type SyncRef, action, call, getEvents, hideNUI, isReady, onError, onNUI, onReady, registerKeybind, sendNUI, setNUIFocus, showNUI, sync };
148
+ export { type ActionError, type ClientEventBus, type KeybindOptions, type ShowNUIOptions, type SyncRef, action, call, getEvents, hideNUI, isReady, onError, onNUI, onReady, registerKeybind, sendNUI, setNUIFocus, showNUI, sync, watch };
package/dist/index.js CHANGED
@@ -32,7 +32,8 @@ __export(index_exports, {
32
32
  sendNUI: () => sendNUI,
33
33
  setNUIFocus: () => setNUIFocus,
34
34
  showNUI: () => showNUI,
35
- sync: () => sync
35
+ sync: () => sync,
36
+ watch: () => watch
36
37
  });
37
38
  module.exports = __toCommonJS(index_exports);
38
39
  function warp() {
@@ -47,6 +48,24 @@ function call(name, payload, timeout) {
47
48
  function sync(key, defaultValue) {
48
49
  return warp().sync(key, defaultValue);
49
50
  }
51
+ function watch(refs, handler) {
52
+ let scheduled = false;
53
+ let oldSnapshot = refs.map((r) => r.value);
54
+ const unsubs = refs.map(
55
+ (ref) => ref.watch(() => {
56
+ if (!scheduled) {
57
+ scheduled = true;
58
+ Promise.resolve().then(() => {
59
+ scheduled = false;
60
+ const newSnapshot = refs.map((r) => r.value);
61
+ handler(newSnapshot, oldSnapshot);
62
+ oldSnapshot = newSnapshot;
63
+ });
64
+ }
65
+ })
66
+ );
67
+ return () => unsubs.forEach((fn) => fn());
68
+ }
50
69
  function onError(handler) {
51
70
  return warp().onError(handler);
52
71
  }
@@ -91,5 +110,6 @@ function registerKeybind(name, options) {
91
110
  sendNUI,
92
111
  setNUIFocus,
93
112
  showNUI,
94
- sync
113
+ sync,
114
+ watch
95
115
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warpfx/client",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Warp Framework SDK for client-side FiveM module development",
5
5
  "keywords": [
6
6
  "client",