@coherent.js/state 1.0.0-beta.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/LICENSE +21 -0
- package/README.md +102 -0
- package/dist/index.js +1696 -0
- package/package.json +44 -0
- package/types/index.d.ts +140 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Thomas Drouvin
|
|
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,102 @@
|
|
|
1
|
+
# @coherent.js/state
|
|
2
|
+
|
|
3
|
+
Reactive state management for Coherent.js applications with SSR support, persistence, and validation.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @coherent.js/state@beta
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @coherent.js/state@beta
|
|
11
|
+
# or
|
|
12
|
+
yarn add @coherent.js/state@beta
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- **Reactive State**: Observable values with computed properties and watchers
|
|
18
|
+
- **SSR-Compatible State**: Server-side state management during rendering
|
|
19
|
+
- **State Persistence**: LocalStorage, SessionStorage, and IndexedDB support
|
|
20
|
+
- **State Validation**: Built-in validation with custom validators
|
|
21
|
+
- **Context API**: Share state across components during SSR
|
|
22
|
+
- **Zero Dependencies**: Uses only @coherent.js/core as peer dependency
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### Reactive State (Client-Side)
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
import { createReactiveState, observable, computed } from '@coherent.js/state';
|
|
30
|
+
|
|
31
|
+
// Create observable values
|
|
32
|
+
const count = observable(0);
|
|
33
|
+
const doubled = computed(() => count.value * 2);
|
|
34
|
+
|
|
35
|
+
// Watch for changes
|
|
36
|
+
count.watch((newValue, oldValue) => {
|
|
37
|
+
console.log(`Count changed from ${oldValue} to ${newValue}`);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Update value
|
|
41
|
+
count.value = 5; // Triggers watcher and updates computed
|
|
42
|
+
console.log(doubled.value); // 10
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### SSR-Compatible State
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
import { createState, provideContext } from '@coherent.js/state';
|
|
49
|
+
|
|
50
|
+
// Create state container for a request
|
|
51
|
+
const state = createState({ userId: 123, theme: 'dark' });
|
|
52
|
+
|
|
53
|
+
// Provide context during SSR
|
|
54
|
+
provideContext('request', state);
|
|
55
|
+
|
|
56
|
+
// Access in components
|
|
57
|
+
import { useContext } from '@coherent.js/state';
|
|
58
|
+
|
|
59
|
+
function MyComponent() {
|
|
60
|
+
const requestState = useContext('request');
|
|
61
|
+
const userId = requestState.get('userId');
|
|
62
|
+
// ... render component
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### State Persistence
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
import { withLocalStorage, withSessionStorage } from '@coherent.js/state';
|
|
70
|
+
|
|
71
|
+
// Auto-persist to localStorage
|
|
72
|
+
const userPrefs = withLocalStorage({ theme: 'dark', lang: 'en' }, 'user-prefs');
|
|
73
|
+
|
|
74
|
+
// Auto-persist to sessionStorage
|
|
75
|
+
const sessionData = withSessionStorage({ cart: [] }, 'session-data');
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### State Validation
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
import { createValidatedState, validators } from '@coherent.js/state';
|
|
82
|
+
|
|
83
|
+
const userForm = createValidatedState(
|
|
84
|
+
{ email: '', age: 0 },
|
|
85
|
+
{
|
|
86
|
+
validators: {
|
|
87
|
+
email: validators.email(),
|
|
88
|
+
age: validators.range(18, 120)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
userForm.set('email', 'invalid-email'); // Throws validation error
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## API Reference
|
|
97
|
+
|
|
98
|
+
See the [full documentation](https://docs.coherentjs.dev/state) for detailed API reference.
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT
|