@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/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@coherent.js/state",
3
+ "version": "1.0.0-beta.2",
4
+ "description": "Reactive state management for Coherent.js applications",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "exports": {
8
+ ".": "./dist/index.js",
9
+ "./reactive": "./dist/reactive-state.js",
10
+ "./persistence": "./dist/state-persistence.js",
11
+ "./validation": "./dist/state-validation.js",
12
+ "./manager": "./dist/state-manager.js"
13
+ },
14
+ "keywords": [
15
+ "coherent",
16
+ "state",
17
+ "reactive",
18
+ "state-management",
19
+ "persistence",
20
+ "validation"
21
+ ],
22
+ "author": "Coherent.js Team",
23
+ "license": "MIT",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/Tomdrouv1/coherent.js.git"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "peerDependencies": {
32
+ "@coherent.js/core": "1.0.0-beta.2"
33
+ },
34
+ "types": "./types/index.d.ts",
35
+ "files": [
36
+ "LICENSE",
37
+ "README.md",
38
+ "types/"
39
+ ],
40
+ "scripts": {
41
+ "build": "node build.mjs",
42
+ "clean": "rm -rf dist"
43
+ }
44
+ }
@@ -0,0 +1,140 @@
1
+ /**
2
+ * Coherent.js State Management TypeScript Definitions
3
+ * @module @coherent.js/state
4
+ */
5
+
6
+ // ===== Reactive State Types =====
7
+
8
+ export interface Observer<T = any> {
9
+ (value: T, oldValue: T): void;
10
+ }
11
+
12
+ export class Observable<T = any> {
13
+ constructor(initialValue: T);
14
+ get(): T;
15
+ set(value: T): void;
16
+ subscribe(observer: Observer<T>): () => void;
17
+ unsubscribe(observer: Observer<T>): void;
18
+ notify(): void;
19
+ }
20
+
21
+ export interface ReactiveStateOptions<T = any> {
22
+ initialValue: T;
23
+ computed?: Record<string, (state: T) => any>;
24
+ watchers?: Record<string, Observer<any>>;
25
+ middleware?: Array<(state: T, action: string, payload?: any) => T | void>;
26
+ }
27
+
28
+ export class ReactiveState<T = any> {
29
+ constructor(options: ReactiveStateOptions<T>);
30
+ get<K extends keyof T>(key: K): T[K];
31
+ set<K extends keyof T>(key: K, value: T[K]): void;
32
+ update(partial: Partial<T>): void;
33
+ subscribe(observer: Observer<T>): () => void;
34
+ watch<K extends keyof T>(key: K, observer: Observer<T[K]>): () => void;
35
+ getState(): T;
36
+ reset(): void;
37
+ }
38
+
39
+ export function createReactiveState<T = any>(options: ReactiveStateOptions<T>): ReactiveState<T>;
40
+ export function observable<T = any>(initialValue: T): Observable<T>;
41
+ export function computed<T = any>(fn: () => T, dependencies: Observable<any>[]): Observable<T>;
42
+
43
+ export const stateUtils: {
44
+ batch<T>(fn: () => T): T;
45
+ transaction<T>(fn: () => T): T;
46
+ freeze<T>(state: T): Readonly<T>;
47
+ clone<T>(state: T): T;
48
+ };
49
+
50
+ // ===== SSR-Compatible State Manager Types =====
51
+
52
+ export interface StateManagerOptions<T = any> {
53
+ initialState?: T;
54
+ persist?: boolean;
55
+ key?: string;
56
+ middleware?: Array<(state: T, action: string) => T | void>;
57
+ }
58
+
59
+ export interface State<T = any> {
60
+ get(): T;
61
+ set(value: T): void;
62
+ update(partial: Partial<T>): void;
63
+ subscribe(listener: (state: T) => void): () => void;
64
+ reset(): void;
65
+ }
66
+
67
+ export function createState<T = any>(initialState: T, options?: StateManagerOptions<T>): State<T>;
68
+
69
+ export const globalStateManager: {
70
+ getState<T = any>(key: string): T | undefined;
71
+ setState<T = any>(key: string, value: T): void;
72
+ subscribe<T = any>(key: string, listener: (state: T) => void): () => void;
73
+ clear(key?: string): void;
74
+ };
75
+
76
+ // ===== Context API Types =====
77
+
78
+ export interface ContextValue<T = any> {
79
+ value: T;
80
+ subscribers: Set<(value: T) => void>;
81
+ }
82
+
83
+ export function provideContext<T = any>(key: string, value: T): void;
84
+ export function createContextProvider<T = any>(key: string, value: T): { key: string; value: T };
85
+ export function useContext<T = any>(key: string, defaultValue?: T): T;
86
+ export function restoreContext(contexts: Record<string, any>): void;
87
+ export function clearAllContexts(): void;
88
+
89
+ // ===== Persistent State Types =====
90
+
91
+ export interface PersistenceAdapter {
92
+ getItem(key: string): Promise<string | null> | string | null;
93
+ setItem(key: string, value: string): Promise<void> | void;
94
+ removeItem(key: string): Promise<void> | void;
95
+ }
96
+
97
+ export interface PersistentStateOptions<T = any> extends StateManagerOptions<T> {
98
+ key: string;
99
+ storage?: PersistenceAdapter;
100
+ serialize?: (state: T) => string;
101
+ deserialize?: (data: string) => T;
102
+ debounce?: number;
103
+ }
104
+
105
+ export function createPersistentState<T = any>(options: PersistentStateOptions<T>): State<T>;
106
+ export function withLocalStorage<T = any>(state: State<T>, key: string): State<T>;
107
+ export function withSessionStorage<T = any>(state: State<T>, key: string): State<T>;
108
+ export function withIndexedDB<T = any>(state: State<T>, key: string, dbName?: string): Promise<State<T>>;
109
+
110
+ // ===== Validated State Types =====
111
+
112
+ export interface ValidationRule<T = any> {
113
+ (value: T): boolean | string;
114
+ }
115
+
116
+ export interface ValidatedStateOptions<T = any> extends StateManagerOptions<T> {
117
+ validators: Record<keyof T, ValidationRule<T[keyof T]>[]>;
118
+ validateOnChange?: boolean;
119
+ strict?: boolean;
120
+ }
121
+
122
+ export interface ValidatedState<T = any> extends State<T> {
123
+ validate(): { valid: boolean; errors: Record<keyof T, string[]> };
124
+ isValid(): boolean;
125
+ getErrors(): Record<keyof T, string[]>;
126
+ }
127
+
128
+ export function createValidatedState<T = any>(options: ValidatedStateOptions<T>): ValidatedState<T>;
129
+
130
+ export const validators: {
131
+ required(message?: string): ValidationRule;
132
+ minLength(length: number, message?: string): ValidationRule;
133
+ maxLength(length: number, message?: string): ValidationRule;
134
+ min(value: number, message?: string): ValidationRule;
135
+ max(value: number, message?: string): ValidationRule;
136
+ pattern(regex: RegExp, message?: string): ValidationRule;
137
+ email(message?: string): ValidationRule;
138
+ url(message?: string): ValidationRule;
139
+ custom(fn: (value: any) => boolean | string): ValidationRule;
140
+ };