redgin-store 0.1.0
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/LICENSE +21 -0
- package/README.md +124 -0
- package/dist/redgin-store.min.js +1 -0
- package/package.json +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 josnin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
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
|
+
|
|
5
|
+
|
|
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.
|
|
12
|
+
|
|
13
|
+
|
|
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).
|
|
21
|
+
|
|
22
|
+
# Quick Start
|
|
23
|
+
1. Define your Store
|
|
24
|
+
Create a singleton instance. Pass a storageKey as the second argument to enable persistence.
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
// store.ts
|
|
28
|
+
import { Store } from './redgin-store';
|
|
29
|
+
|
|
30
|
+
const initialState = {
|
|
31
|
+
cart: [],
|
|
32
|
+
user: 'Guest',
|
|
33
|
+
theme: 'light'
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// 'market_storage' is the key used in LocalStorage
|
|
37
|
+
export const store = new Store(initialState, 'market_storage');
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
2. Connect to a RedGin Component
|
|
43
|
+
Use onInit to subscribe and disconnectedCallback to clean up.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { RedGin, watch, getset, html } from "redgin";
|
|
47
|
+
import { store } from "./store";
|
|
48
|
+
|
|
49
|
+
class ShoppingCart extends RedGin {
|
|
50
|
+
global = getset(store.state);
|
|
51
|
+
private _unsub?: () => void;
|
|
52
|
+
|
|
53
|
+
onInit() {
|
|
54
|
+
// Subscribe and store the cleanup function
|
|
55
|
+
this._unsub = store.subscribe(newState => this.global = newState);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
disconnectedCallback() {
|
|
59
|
+
// CRITICAL: Unplug from store to prevent memory leaks
|
|
60
|
+
if (this._unsub) this._unsub();
|
|
61
|
+
super.disconnectedCallback();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
addItem() {
|
|
65
|
+
const newItem = { id: Date.now(), name: 'New Item' };
|
|
66
|
+
// Update global state
|
|
67
|
+
store.set('cart', [...this.global.cart, newItem]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
render() {
|
|
71
|
+
return html`
|
|
72
|
+
<div>
|
|
73
|
+
<h3>Items: ${watch(['global'], () => this.global.cart.length)}</h3>
|
|
74
|
+
<button onclick="${() => this.addItem()}">Add Item</button>
|
|
75
|
+
</div>
|
|
76
|
+
`;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### API Reference
|
|
83
|
+
`new Store(initialState, storageKey?)`
|
|
84
|
+
|
|
85
|
+
| Parameter | Type | Description |
|
|
86
|
+
| :--- | :--- | :--- |
|
|
87
|
+
| `initialState` | Object | The starting data for your store. |
|
|
88
|
+
| `storageKey` | string | (Optional) The key name for LocalStorage persistence |
|
|
89
|
+
|
|
90
|
+
`store.set(key, value)`
|
|
91
|
+
|
|
92
|
+
Updates a specific top-level key in the state. Triggers a batched notification to all subscribers.
|
|
93
|
+
* Note: Use immutable patterns (spread operator) for arrays and objects to ensure reactivity.
|
|
94
|
+
|
|
95
|
+
`store.subscribe(callback)`
|
|
96
|
+
|
|
97
|
+
Registers a listener. Returns an unsubscribe function
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
const unsub = store.subscribe(state => console.log(state));
|
|
101
|
+
unsub(); // Stop listening
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
# Performance Tip: Batching
|
|
105
|
+
RedGin Store uses Microtask Batching. If you execute:
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
store.set('user', 'Admin');
|
|
109
|
+
store.set('theme', 'dark');
|
|
110
|
+
store.set('cart', []);
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The UI will only re-render once at the end of the execution block, preventing expensive layout thrashing.
|
|
114
|
+
|
|
115
|
+
## Help
|
|
116
|
+
|
|
117
|
+
Need help? Open an issue in: [ISSUES](https://github.com/josnin/redgin-store/issues)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
## Contributing
|
|
121
|
+
Want to improve and add feature? Fork the repo, add your changes and send a pull request.
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var i=class{_state;_listeners=new Set;_pending=!1;_storageKey;constructor(t={},s=null){if(this._storageKey=s,this._storageKey){let e=localStorage.getItem(this._storageKey);this._state=e?JSON.parse(e):t}else this._state=t}get state(){return this._state}set(t,s){this._state[t]!==s&&(this._state={...this._state,[t]:s},this._storageKey&&localStorage.setItem(this._storageKey,JSON.stringify(this._state)),this._notify())}_notify(){this._pending||(this._pending=!0,queueMicrotask(()=>{this._pending=!1,this._listeners.forEach(t=>t(this._state))}))}subscribe(t){return this._listeners.add(t),()=>this.unsubscribe(t)}unsubscribe(t){this._listeners.delete(t)}};export{i as Store};
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "redgin-store",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "vite",
|
|
7
|
+
"build": "esbuild src/store.ts --bundle --minify --format=esm --external:redgin --outfile=dist/redgin-store.min.js",
|
|
8
|
+
"check-types": "tsc --noEmit"
|
|
9
|
+
},
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"redgin": ">=0.2.2"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"esbuild": "^0.20.0",
|
|
15
|
+
"typescript": "^5.0.0",
|
|
16
|
+
"vite": "^5.0.0",
|
|
17
|
+
"redgin": "latest"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|