@biglogic/rgs 3.9.11 → 3.9.20

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/markdown/api.md CHANGED
@@ -14,7 +14,12 @@ Creates a reactive store with a built-in typed hook in one line.
14
14
  function gstate<S extends Record<string, unknown>>(
15
15
  initialState: S,
16
16
  configOrNamespace?: string | StoreConfig<S>
17
- ): (<K extends keyof S>(key: K) => readonly [S[K] | undefined, (val: S[K] | StateUpdater<S[K]>, options?: PersistOptions) => boolean]) & IStore<S>
17
+ ): (<K extends keyof S>(
18
+ key: K
19
+ ) => readonly [
20
+ S[K] | undefined,
21
+ (val: S[K] | StateUpdater<S[K]>, options?: PersistOptions) => boolean
22
+ ]) & IStore<S>
18
23
  ```
19
24
 
20
25
  **Parameters:**
@@ -110,6 +115,61 @@ function getStore(): IStore<Record<string, unknown>> | null
110
115
 
111
116
  ---
112
117
 
118
+ ### `getStoreByNamespace`
119
+
120
+ Retrieves a store by its namespace.
121
+
122
+ ```typescript
123
+ function getStoreByNamespace(namespace: string): IStore<Record<string, unknown>> | null
124
+ ```
125
+
126
+ **Returns:** The `IStore` for the given namespace or `null`.
127
+
128
+ ---
129
+
130
+ ### `registerStore`
131
+
132
+ Registers a store in the global registry for HMR cleanup. Used internally by `gstate()`.
133
+
134
+ ```typescript
135
+ function registerStore(namespace: string, store: IStore<Record<string, unknown>>): void
136
+ ```
137
+
138
+ ---
139
+
140
+ ### `unregisterStore`
141
+
142
+ Unregisters a store from the global registry.
143
+
144
+ ```typescript
145
+ function unregisterStore(namespace: string): void
146
+ ```
147
+
148
+ ---
149
+
150
+ ### `destroyState`
151
+
152
+ Destroys a store by namespace. Safe to call multiple times.
153
+
154
+ ```typescript
155
+ function destroyState(namespace?: string): void
156
+ ```
157
+
158
+ **Parameters:**
159
+ - `namespace` - Optional namespace to destroy. If not provided, destroys the default store.
160
+
161
+ ---
162
+
163
+ ### `destroyAllStores`
164
+
165
+ Destroys all registered stores. Useful for HMR, testing, and micro-frontend teardown.
166
+
167
+ ```typescript
168
+ function destroyAllStores(): void
169
+ ```
170
+
171
+ ---
172
+
113
173
  ## Store Interface (`IStore`)
114
174
 
115
175
  ### State Operations
@@ -411,3 +471,123 @@ interface AccessRule {
411
471
  | `onRemove` | Called when a value is removed |
412
472
  | `onDestroy` | Called when store is destroyed |
413
473
  | `onTransaction` | Called during a transaction |
474
+
475
+ ---
476
+
477
+ ## SSR Hooks
478
+
479
+ ### `useHydrated`
480
+
481
+ Returns `true` when the app is hydrated on the client.
482
+
483
+ ```typescript
484
+ function useHydrated(): boolean
485
+ ```
486
+
487
+ ### `useHydrationStatus`
488
+
489
+ Returns detailed hydration status.
490
+
491
+ ```typescript
492
+ function useHydrationStatus(): { isHydrated: boolean; isHydrating: boolean }
493
+ ```
494
+
495
+ ### `useDeferredStore`
496
+
497
+ Returns a store proxy that returns defaults until hydrated.
498
+
499
+ ```typescript
500
+ function useDeferredStore<S>(store: IStore<S> & { isHydrated?: () => boolean }): IStore<S>
501
+ ```
502
+
503
+ ### `createSSRStore`
504
+
505
+ Creates an SSR-safe store with hydration methods.
506
+
507
+ ```typescript
508
+ function createSSRStore<S>(config?: SSRStoreConfig<S>): IStore<S> & {
509
+ hydrate: () => Promise<void>
510
+ getSerializedState: () => string | null
511
+ isHydrated: () => boolean
512
+ }
513
+ ```
514
+
515
+ ### `createNextStore`
516
+
517
+ Creates a Next.js App Router compatible store.
518
+
519
+ ```typescript
520
+ function createNextStore<S>(config?: SSRStoreConfig<S>)
521
+ ```
522
+
523
+ ---
524
+
525
+ ## Sync API
526
+
527
+ ### `initSync`
528
+
529
+ Initializes the sync engine for a store.
530
+
531
+ ```typescript
532
+ function initSync(
533
+ store: IStore<Record<string, unknown>>,
534
+ config: SyncConfig
535
+ ): SyncEngine<Record<string, unknown>>
536
+ ```
537
+
538
+ ### `destroySync`
539
+
540
+ Destroys the sync engine for a namespace.
541
+
542
+ ```typescript
543
+ function destroySync(namespace: string): void
544
+ ```
545
+
546
+ ### `useSyncedState`
547
+
548
+ Hook for offline-first synchronized state.
549
+
550
+ ```typescript
551
+ function useSyncedState<T>(
552
+ key: string,
553
+ store?: IStore<Record<string, unknown>>
554
+ ): readonly [
555
+ T | undefined,
556
+ (val: T | StateUpdater<T>, options?: PersistOptions) => boolean,
557
+ SyncState
558
+ ]
559
+ ```
560
+
561
+ ### `useSyncStatus`
562
+
563
+ Hook for global sync status.
564
+
565
+ ```typescript
566
+ function useSyncStatus(): SyncState
567
+ ```
568
+
569
+ ### `triggerSync`
570
+
571
+ Manually triggers sync for a namespace.
572
+
573
+ ```typescript
574
+ function triggerSync(namespace?: string): Promise<void>
575
+ ```
576
+
577
+ ---
578
+
579
+ ## Changelog (v3.9.20)
580
+
581
+ ### New APIs
582
+ - `registerStore()` / `unregisterStore()` - Store registry management
583
+ - `getStoreByNamespace()` - Get store by namespace
584
+ - `destroyAllStores()` - Destroy all registered stores
585
+ - `destroyState(namespace?)` - Destroy store by namespace
586
+
587
+ ### Improvements
588
+ - `gstate()` now auto-registers stores for HMR cleanup
589
+ - `isEqual()` now supports Date, Map, Set, RegExp, TypedArray, ArrayBuffer
590
+ - `getServerSnapshot()` uses safe Proxy for SSR selectors
591
+ - `initState()` warnings only shown in development mode
592
+ - `useSyncedState()` warns if `initSync()` not called
593
+ - `safeBtoa()` / `safeAtob()` for SSR Node.js compatibility
@@ -29,7 +29,7 @@ function Counter() {
29
29
  <div>
30
30
  <p>Hello, {name}!</p>
31
31
  <p>Count: {count}</p>
32
- <button onClick={() =u003e setCount(count + 1)}>
32
+ <button onClick={() => setCount(count + 1)}>
33
33
  +1
34
34
  </button>
35
35
  </div>
@@ -53,10 +53,41 @@ import { initState, useStore } from '@biglogic/rgs';
53
53
  // Initialize once at app root
54
54
  initState({
55
55
  namespace: 'my-awesome-app',
56
- persistence: true
56
+ persistByDefault: true
57
57
  });
58
58
 
59
59
  // Use anywhere in your app
60
60
  const [count, setCount] = useStore('count')
61
61
  const [user, setUser] = useStore('user')
62
62
  ```
63
+
64
+ ## 4. Multi-Store Pattern (Recommended for Large Apps)
65
+
66
+ For micro-frontends or large applications, use separate namespaces:
67
+
68
+ ```typescript
69
+ import { gstate } from '@biglogic/rgs';
70
+
71
+ // Cart store
72
+ export const useCart = gstate({ items: [] }, { namespace: 'cart' })
73
+
74
+ // User store
75
+ export const useUser = gstate({ profile: null }, { namespace: 'user' })
76
+
77
+ // Theme store
78
+ export const useTheme = gstate({ mode: 'light' }, { namespace: 'theme' })
79
+ ```
80
+
81
+ ## 5. Cleanup (HMR / Testing)
82
+
83
+ For Hot Module Replacement or test cleanup:
84
+
85
+ ```typescript
86
+ import { destroyState, destroyAllStores } from '@biglogic/rgs';
87
+
88
+ // Destroy specific store
89
+ destroyState('cart')
90
+
91
+ // Destroy all stores (useful for HMR)
92
+ destroyAllStores()
93
+ ```
@@ -48,3 +48,9 @@ For real-world implementations, refer to the `examples/security-best-practices`
48
48
  - **Storage Key Validation**: Keys validated before persistence to prevent injection.
49
49
  - **Production Safety**: Global window access only enabled in development mode (`NODE_ENV !== 'production'`).
50
50
  - **PBKDF2 Key Derivation**: New `deriveKeyFromPassword()` and `generateSalt()` functions for secure password-based encryption (NIST SP 800-132 compliant - 600k iterations, 32-byte salt).
51
+
52
+ ## Summary of 3.9.20 Enhancements
53
+ - **Safe Base64 for SSR**: `safeBtoa()` and `safeAtob()` functions with Node.js Buffer fallback for server-side rendering compatibility.
54
+ - **Deep Equality Enhancement**: `isEqual()` now properly compares Date, Map, Set, RegExp, TypedArray, and ArrayBuffer objects.
55
+ - **Development-Only Warnings**: `initState()` duplicate store warnings now only appear in development mode, preventing console spam in production.
56
+ - **SSR Selector Safety**: `getServerSnapshot()` now uses a safe Proxy that returns `undefined` for any property access, preventing crashes when selectors access nested properties on empty state.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@biglogic/rgs",
3
3
  "code": "argis",
4
- "version": "3.9.11",
4
+ "version": "3.9.20",
5
5
  "description": "Argis (RGS) - Reactive Global State: A react state everywhere made easy",
6
6
  "main": "./index.cjs",
7
7
  "browser": "./index.cjs",