redgin-store 0.1.2 → 0.1.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.
Files changed (2) hide show
  1. package/README.md +61 -32
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,25 +1,31 @@
1
1
  # RedGin Store
2
- A Lightweight, Reactive, and Persistent State Management for Web Components.
3
- RedGin Store is a surgically optimized Pub/Sub state manager designed to complement the [RedGin Library](https://www.npmjs.com/package/redgin). It provides global reactivity with zero Virtual DOM overhead, built-in LocalStorage persistence, and automatic batching.
4
2
 
3
+ **A Lightweight, Reactive, and Persistent State Management for Web Components.**
5
4
 
6
- # The Pain Points
7
- Building complex Web Components usually leads to three major headaches:
8
- Prop Drilling: Passing data through five layers of nested components just to update a header.
9
- Zombie Listeners (Memory Leaks): Global event listeners that stay in RAM after a component is destroyed, leading to "ghost" updates and performance degradation.
10
- State Loss on Refresh: Disappearing shopping carts or user sessions because the state only lives in volatile memory.
11
- Redundant Re-renders: Most stores trigger a full UI update on every change. If you update the user, you shouldn't re-render the productList.
5
+ RedGin Store is a surgically optimized Pub/Sub state manager designed specifically for the [RedGin Library](https://www.npmjs.com). It provides global reactivity with **zero Virtual DOM overhead**, built-in **LocalStorage persistence**, and **automatic microtask batching**.
12
6
 
7
+ ---
13
8
 
14
- # Purpose & Features
15
- RedGin Store was built to provide Single Source of Truth that is:
16
- * 🚀 Surgically Reactive: Leverages queueMicrotask to batch multiple updates into a single tick.
17
- * 💾 Persistent by Design: Optional LocalStorage integration—hydrate your state automatically on page load.
18
- * 🛡️ Memory Safe: Implements the Auto-Unsubscribe Pattern to ensure components are garbage-collected when removed from the DOM.
19
- * ⚛️ Immutable & Type-Safe: Uses reference-based dirty checking to ensure high-performance updates.
20
- * 📦 Zero Dependencies: Extremely small footprint (~1kb).
9
+ ## The Pain Points
10
+ Building complex Web Components usually leads to several major headaches:
11
+ * **Prop Drilling:** Passing data through five layers of nested components just to update a single value.
12
+ * **Zombie Listeners (Memory Leaks):** Global store listeners that stay in RAM after a component is destroyed, leading to "ghost" updates and performance degradation.
13
+ * **State Loss on Refresh:** Disappearing shopping carts or user sessions because the state only lives in volatile memory.
14
+ * **Redundant Re-renders:** Most stores trigger a full UI update on every change. If you update the `user`, you shouldn't re-render the `productList`.
21
15
 
22
- # Installation
16
+ ---
17
+
18
+ ## Purpose & Features
19
+ RedGin Store provides a **Single Source of Truth** that is:
20
+ * 🚀 **Surgically Reactive:** Leverages `queueMicrotask` to batch multiple updates into a single tick.
21
+ * 💾 **Persistent by Design:** Optional LocalStorage integration—hydrate your state automatically on page load.
22
+ * 🛡️ **Memory Safe:** Returns an **Unsubscribe function** to ensure components are garbage-collected when removed from the DOM.
23
+ * 🔍 **Built-in Debugger:** Concise console logging that **auto-detects** which component triggered the update.
24
+ * 📦 **Zero Dependencies:** Extremely small footprint (~1kb).
25
+
26
+ ---
27
+
28
+ ## Installation
23
29
 
24
30
  ### Via npm
25
31
  ```bash
@@ -39,16 +45,19 @@ Create a singleton instance. Pass a storageKey as the second argument to enable
39
45
 
40
46
  ```ts
41
47
  // store.ts
42
- import { Store } from './redgin-store';
48
+ // store.ts
49
+ import { Store } from 'redgin-store';
43
50
 
44
51
  const initialState = {
45
52
  cart: [],
46
- user: 'Guest',
47
- theme: 'light'
53
+ user: 'Guest'
48
54
  };
49
55
 
50
- // 'market_storage' is the key used in LocalStorage
51
- export const store = new Store(initialState, 'market_storage');
56
+ // Second argument is the Options Object
57
+ export const store = new Store(initialState, {
58
+ storageKey: 'my_market_app', // Syncs state to LocalStorage
59
+ debug: true // Enables component-trace logging
60
+ });
52
61
 
53
62
  ```
54
63
 
@@ -57,7 +66,7 @@ export const store = new Store(initialState, 'market_storage');
57
66
  Use onInit to subscribe and disconnectedCallback to clean up.
58
67
 
59
68
  ```ts
60
- import { RedGin, watch, getset, html } from "redgin";
69
+ import { RedGin, watch, getset, html, on } from "redgin";
61
70
  import { store } from "./store";
62
71
 
63
72
  class ShoppingCart extends RedGin {
@@ -65,7 +74,7 @@ class ShoppingCart extends RedGin {
65
74
  private _unsub?: () => void;
66
75
 
67
76
  onInit() {
68
- // Subscribe and store the cleanup function
77
+ // Subscribe and save the cleanup function
69
78
  this._unsub = store.subscribe(newState => this.global = newState);
70
79
  }
71
80
 
@@ -75,24 +84,36 @@ class ShoppingCart extends RedGin {
75
84
  super.disconnectedCallback();
76
85
  }
77
86
 
78
- addItem() {
79
- const newItem = { id: Date.now(), name: 'New Item' };
80
- // Update global state
81
- store.set('cart', [...this.global.cart, newItem]);
82
- }
83
-
84
87
  render() {
85
88
  return html`
86
89
  <div>
87
90
  <h3>Items: ${watch(['global'], () => this.global.cart.length)}</h3>
88
- <button onclick="${() => this.addItem()}">Add Item</button>
91
+ <button ${on('click', () => store.set('cart', [...this.global.cart, { id: Date.now() }]))}>
92
+ Add Item
93
+ </button>
89
94
  </div>
90
95
  `;
91
96
  }
92
97
  }
93
98
 
99
+ customElements.define('shopping-cart', ShoppingCart);
100
+
101
+ ```
102
+
103
+ # Surgical Logging
104
+ When debug: true is enabled, RedGin Store auto-detects the component instance using the RedGin Context Bridge. Your console will show exactly who triggered the change in a concise, one-line format:
105
+
106
+ `🔴 RedGin Store: cart by <shopping-cart> updated to: [{id: 12345}]`
107
+
108
+ For async actions or external scripts where context might be lost, you can provide a manual label:
109
+
110
+ ```ts
111
+ store.set('user', 'Admin', 'Auth_Service');
112
+ // Console: 🔴 RedGin Store: user by Auth_Service updated to: Admin
94
113
  ```
95
114
 
115
+
116
+
96
117
  ### API Reference
97
118
  `new Store(initialState, storageKey?)`
98
119
 
@@ -100,11 +121,15 @@ class ShoppingCart extends RedGin {
100
121
  | :--- | :--- | :--- |
101
122
  | `initialState` | Object | The starting data for your store. |
102
123
  | `storageKey` | string | (Optional) The key name for LocalStorage persistence |
124
+ | `debug` | boolen | (Optional) Enables styled console logs with source tracking |
103
125
 
104
- `store.set(key, value)`
126
+ `store.set(key, value, label?)`
105
127
 
106
128
  Updates a specific top-level key in the state. Triggers a batched notification to all subscribers.
107
- * Note: Use immutable patterns (spread operator) for arrays and objects to ensure reactivity.
129
+
130
+ * key: The property name in your state.
131
+ * value: The new value (must use immutable patterns like [...arr] for reactivity).
132
+ * label: (Optional) A string to identify the source in logs
108
133
 
109
134
  `store.subscribe(callback)`
110
135
 
@@ -115,6 +140,10 @@ const unsub = store.subscribe(state => console.log(state));
115
140
  unsub(); // Stop listening
116
141
  ```
117
142
 
143
+ `store.clear()`
144
+ Wipes the LocalStorage data and reloads the page to reset the application state.
145
+
146
+
118
147
  # Performance Tip: Batching
119
148
  RedGin Store uses Microtask Batching. If you execute:
120
149
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "redgin-store",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "main": "./dist/redgin-store.min.js",
6
6
  "module": "./dist/redgin-store.min.js",