@effuse/store 1.0.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/README.md +7 -0
- package/dist/index.cjs +1462 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +698 -0
- package/dist/index.d.ts +698 -0
- package/dist/index.js +1388 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
- package/src/actions/async.ts +386 -0
- package/src/actions/cancellation.ts +131 -0
- package/src/actions/index.ts +48 -0
- package/src/composition/compose.ts +194 -0
- package/src/composition/index.ts +31 -0
- package/src/config/constants.ts +110 -0
- package/src/config/index.ts +40 -0
- package/src/context/index.ts +39 -0
- package/src/context/scope.ts +136 -0
- package/src/core/index.ts +36 -0
- package/src/core/state.ts +48 -0
- package/src/core/store.ts +467 -0
- package/src/core/types.ts +107 -0
- package/src/devtools/connector.ts +115 -0
- package/src/devtools/index.ts +31 -0
- package/src/errors.ts +81 -0
- package/src/index.ts +157 -0
- package/src/middleware/index.ts +25 -0
- package/src/middleware/manager.ts +61 -0
- package/src/persistence/adapters.ts +96 -0
- package/src/persistence/index.ts +31 -0
- package/src/reactivity/derived.ts +170 -0
- package/src/reactivity/index.ts +42 -0
- package/src/reactivity/selectors.ts +180 -0
- package/src/reactivity/streams.ts +194 -0
- package/src/registry/index.ts +60 -0
- package/src/validation/index.ts +33 -0
- package/src/validation/schema.ts +142 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { signal, type Signal } from '@effuse/core';
|
|
26
|
+
import type { Store, StoreDefinition } from '../core/types.js';
|
|
27
|
+
import { createStore as createStoreImpl } from '../core/store.js';
|
|
28
|
+
import {
|
|
29
|
+
createCancellationToken,
|
|
30
|
+
type CancellationToken,
|
|
31
|
+
} from '../actions/cancellation.js';
|
|
32
|
+
|
|
33
|
+
// Composed store container
|
|
34
|
+
export interface ComposedStore<T, D extends readonly Store<unknown>[]> {
|
|
35
|
+
store: Store<T>;
|
|
36
|
+
dependencies: D;
|
|
37
|
+
computed: <R>(
|
|
38
|
+
selector: (
|
|
39
|
+
state: T,
|
|
40
|
+
deps: { [K in keyof D]: ReturnType<D[K]['getSnapshot']> }
|
|
41
|
+
) => R
|
|
42
|
+
) => Signal<R>;
|
|
43
|
+
computedAsync: <R>(
|
|
44
|
+
asyncSelector: (
|
|
45
|
+
state: T,
|
|
46
|
+
deps: { [K in keyof D]: ReturnType<D[K]['getSnapshot']> },
|
|
47
|
+
token: CancellationToken
|
|
48
|
+
) => Promise<R>,
|
|
49
|
+
initialValue: R
|
|
50
|
+
) => Signal<R> & { pending: Signal<boolean>; cleanup: () => void };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Compose stores with dependencies
|
|
54
|
+
export const composeStores = <T, D extends readonly Store<unknown>[]>(
|
|
55
|
+
mainStore: Store<T>,
|
|
56
|
+
dependencies: D
|
|
57
|
+
): ComposedStore<T, D> => {
|
|
58
|
+
const getDependencySnapshots = (): {
|
|
59
|
+
[K in keyof D]: ReturnType<D[K]['getSnapshot']>;
|
|
60
|
+
} => {
|
|
61
|
+
return dependencies.map((dep) => dep.getSnapshot()) as {
|
|
62
|
+
[K in keyof D]: ReturnType<D[K]['getSnapshot']>;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
store: mainStore,
|
|
68
|
+
dependencies,
|
|
69
|
+
computed: <R>(
|
|
70
|
+
selector: (
|
|
71
|
+
state: T,
|
|
72
|
+
deps: { [K in keyof D]: ReturnType<D[K]['getSnapshot']> }
|
|
73
|
+
) => R
|
|
74
|
+
): Signal<R> => {
|
|
75
|
+
const mainSnapshot = mainStore.getSnapshot() as T;
|
|
76
|
+
const depSnapshots = getDependencySnapshots();
|
|
77
|
+
const initialValue = selector(mainSnapshot, depSnapshots);
|
|
78
|
+
const derived = signal<R>(initialValue);
|
|
79
|
+
|
|
80
|
+
const update = () => {
|
|
81
|
+
const mainState = mainStore.getSnapshot() as T;
|
|
82
|
+
const depStates = getDependencySnapshots();
|
|
83
|
+
const newValue = selector(mainState, depStates);
|
|
84
|
+
if (derived.value !== newValue) {
|
|
85
|
+
derived.value = newValue;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
mainStore.subscribe(update);
|
|
90
|
+
for (const dep of dependencies) {
|
|
91
|
+
dep.subscribe(update);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return derived;
|
|
95
|
+
},
|
|
96
|
+
computedAsync: <R>(
|
|
97
|
+
asyncSelector: (
|
|
98
|
+
state: T,
|
|
99
|
+
deps: { [K in keyof D]: ReturnType<D[K]['getSnapshot']> },
|
|
100
|
+
token: CancellationToken
|
|
101
|
+
) => Promise<R>,
|
|
102
|
+
initialValue: R
|
|
103
|
+
): Signal<R> & { pending: Signal<boolean>; cleanup: () => void } => {
|
|
104
|
+
const derived = signal<R>(initialValue);
|
|
105
|
+
const pending = signal<boolean>(false);
|
|
106
|
+
let currentToken = createCancellationToken();
|
|
107
|
+
const unsubscribers: (() => void)[] = [];
|
|
108
|
+
|
|
109
|
+
const update = () => {
|
|
110
|
+
currentToken.cancel();
|
|
111
|
+
currentToken = createCancellationToken();
|
|
112
|
+
const myToken = currentToken;
|
|
113
|
+
pending.value = true;
|
|
114
|
+
|
|
115
|
+
const mainState = mainStore.getSnapshot() as T;
|
|
116
|
+
const depStates = getDependencySnapshots();
|
|
117
|
+
|
|
118
|
+
asyncSelector(mainState, depStates, myToken)
|
|
119
|
+
.then((newValue) => {
|
|
120
|
+
if (!myToken.isCancelled && derived.value !== newValue) {
|
|
121
|
+
derived.value = newValue;
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
.catch(() => {})
|
|
125
|
+
.finally(() => {
|
|
126
|
+
if (!myToken.isCancelled) {
|
|
127
|
+
pending.value = false;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
unsubscribers.push(mainStore.subscribe(update));
|
|
133
|
+
for (const dep of dependencies) {
|
|
134
|
+
unsubscribers.push(dep.subscribe(update));
|
|
135
|
+
}
|
|
136
|
+
update();
|
|
137
|
+
|
|
138
|
+
const result = derived as Signal<R> & {
|
|
139
|
+
pending: Signal<boolean>;
|
|
140
|
+
cleanup: () => void;
|
|
141
|
+
};
|
|
142
|
+
result.pending = pending;
|
|
143
|
+
result.cleanup = () => {
|
|
144
|
+
currentToken.cancel();
|
|
145
|
+
for (const unsub of unsubscribers) unsub();
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
return result;
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// Store slice definition
|
|
154
|
+
export interface StoreSlice<T extends object, P extends object> {
|
|
155
|
+
create: (parent: Store<P>) => Store<T>;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Define reusable store slice
|
|
159
|
+
export const defineSlice = <T extends object, P extends object>(
|
|
160
|
+
name: string,
|
|
161
|
+
factory: (parent: Store<P>) => StoreDefinition<T>
|
|
162
|
+
): StoreSlice<T, P> => {
|
|
163
|
+
return {
|
|
164
|
+
create: (parent: Store<P>): Store<T> => {
|
|
165
|
+
const definition = factory(parent);
|
|
166
|
+
return createStoreImpl<T>(name, definition);
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// Merge multiple stores
|
|
172
|
+
export const mergeStores = <A, B>(
|
|
173
|
+
storeA: Store<A>,
|
|
174
|
+
storeB: Store<B>
|
|
175
|
+
): {
|
|
176
|
+
getSnapshot: () => ReturnType<Store<A>['getSnapshot']> &
|
|
177
|
+
ReturnType<Store<B>['getSnapshot']>;
|
|
178
|
+
subscribe: (callback: () => void) => () => void;
|
|
179
|
+
} => {
|
|
180
|
+
return {
|
|
181
|
+
getSnapshot: () => ({
|
|
182
|
+
...storeA.getSnapshot(),
|
|
183
|
+
...storeB.getSnapshot(),
|
|
184
|
+
}),
|
|
185
|
+
subscribe: (callback: () => void) => {
|
|
186
|
+
const unsubA = storeA.subscribe(callback);
|
|
187
|
+
const unsubB = storeB.subscribe(callback);
|
|
188
|
+
return () => {
|
|
189
|
+
unsubA();
|
|
190
|
+
unsubB();
|
|
191
|
+
};
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export {
|
|
26
|
+
composeStores,
|
|
27
|
+
defineSlice,
|
|
28
|
+
mergeStores,
|
|
29
|
+
type ComposedStore,
|
|
30
|
+
type StoreSlice,
|
|
31
|
+
} from './compose.js';
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { Config, Duration, Effect } from 'effect';
|
|
26
|
+
|
|
27
|
+
// Storage key prefix
|
|
28
|
+
export const STORAGE_PREFIX = 'effuse-store:';
|
|
29
|
+
// Root scope identifier
|
|
30
|
+
export const ROOT_SCOPE_ID = '__root__';
|
|
31
|
+
// Scope identifier prefix
|
|
32
|
+
export const SCOPE_PREFIX = 'scope_';
|
|
33
|
+
|
|
34
|
+
// Default timeout milliseconds
|
|
35
|
+
export const DEFAULT_TIMEOUT_MS = 5000;
|
|
36
|
+
|
|
37
|
+
// Default timeout duration
|
|
38
|
+
export const DEFAULT_TIMEOUT = Duration.millis(DEFAULT_TIMEOUT_MS);
|
|
39
|
+
|
|
40
|
+
// Initial retry delay
|
|
41
|
+
export const DEFAULT_RETRY_INITIAL_DELAY_MS = 100;
|
|
42
|
+
// Maximum retry delay
|
|
43
|
+
export const DEFAULT_RETRY_MAX_DELAY_MS = 5000;
|
|
44
|
+
// Retry backoff factor
|
|
45
|
+
export const DEFAULT_RETRY_BACKOFF_FACTOR = 2;
|
|
46
|
+
|
|
47
|
+
// Internal constants collection
|
|
48
|
+
export const StoreConstants = {
|
|
49
|
+
STORAGE_PREFIX,
|
|
50
|
+
ROOT_SCOPE_ID,
|
|
51
|
+
SCOPE_PREFIX,
|
|
52
|
+
DEBUG_PREFIX: '[store]',
|
|
53
|
+
DEVTOOLS_PREFIX: 'Effuse:',
|
|
54
|
+
DEFAULT_TIMEOUT_MS,
|
|
55
|
+
} as const;
|
|
56
|
+
|
|
57
|
+
// Store configuration parameters
|
|
58
|
+
export const StoreConfigOptions = {
|
|
59
|
+
persistByDefault: Config.boolean('EFFUSE_STORE_PERSIST').pipe(
|
|
60
|
+
Config.withDefault(false)
|
|
61
|
+
),
|
|
62
|
+
storagePrefix: Config.string('EFFUSE_STORE_PREFIX').pipe(
|
|
63
|
+
Config.withDefault(STORAGE_PREFIX)
|
|
64
|
+
),
|
|
65
|
+
debug: Config.boolean('EFFUSE_STORE_DEBUG').pipe(Config.withDefault(false)),
|
|
66
|
+
devtools: Config.boolean('EFFUSE_STORE_DEVTOOLS').pipe(
|
|
67
|
+
Config.withDefault(false)
|
|
68
|
+
),
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// Store configuration values
|
|
72
|
+
export interface StoreConfigValues {
|
|
73
|
+
persistByDefault: boolean;
|
|
74
|
+
storagePrefix: string;
|
|
75
|
+
debug: boolean;
|
|
76
|
+
devtools: boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Load configuration values
|
|
80
|
+
export const loadStoreConfigValues: Effect.Effect<StoreConfigValues> =
|
|
81
|
+
Effect.all({
|
|
82
|
+
persistByDefault: StoreConfigOptions.persistByDefault,
|
|
83
|
+
storagePrefix: StoreConfigOptions.storagePrefix,
|
|
84
|
+
debug: StoreConfigOptions.debug,
|
|
85
|
+
devtools: StoreConfigOptions.devtools,
|
|
86
|
+
}).pipe(
|
|
87
|
+
Effect.catchAll(() =>
|
|
88
|
+
Effect.succeed({
|
|
89
|
+
persistByDefault: false,
|
|
90
|
+
storagePrefix: STORAGE_PREFIX,
|
|
91
|
+
debug: false,
|
|
92
|
+
devtools: false,
|
|
93
|
+
})
|
|
94
|
+
)
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
let cachedConfig: StoreConfigValues | null = null;
|
|
98
|
+
|
|
99
|
+
// Access store configuration
|
|
100
|
+
export const getStoreConfig = (): StoreConfigValues => {
|
|
101
|
+
if (!cachedConfig) {
|
|
102
|
+
cachedConfig = Effect.runSync(loadStoreConfigValues);
|
|
103
|
+
}
|
|
104
|
+
return cachedConfig;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// Reset configuration cache
|
|
108
|
+
export const resetStoreConfigCache = (): void => {
|
|
109
|
+
cachedConfig = null;
|
|
110
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export {
|
|
26
|
+
STORAGE_PREFIX,
|
|
27
|
+
ROOT_SCOPE_ID,
|
|
28
|
+
SCOPE_PREFIX,
|
|
29
|
+
DEFAULT_TIMEOUT_MS,
|
|
30
|
+
DEFAULT_RETRY_INITIAL_DELAY_MS,
|
|
31
|
+
DEFAULT_RETRY_MAX_DELAY_MS,
|
|
32
|
+
DEFAULT_RETRY_BACKOFF_FACTOR,
|
|
33
|
+
StoreConstants,
|
|
34
|
+
StoreConfigOptions,
|
|
35
|
+
loadStoreConfigValues,
|
|
36
|
+
getStoreConfig,
|
|
37
|
+
resetStoreConfigCache,
|
|
38
|
+
DEFAULT_TIMEOUT,
|
|
39
|
+
type StoreConfigValues,
|
|
40
|
+
} from './constants.js';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export {
|
|
26
|
+
createScope,
|
|
27
|
+
disposeScope,
|
|
28
|
+
enterScope,
|
|
29
|
+
exitScope,
|
|
30
|
+
getCurrentScope,
|
|
31
|
+
getRootScope,
|
|
32
|
+
registerScopedStore,
|
|
33
|
+
getScopedStore,
|
|
34
|
+
hasScopedStore,
|
|
35
|
+
runInScope,
|
|
36
|
+
withScope,
|
|
37
|
+
type ScopeNode,
|
|
38
|
+
type ScopeId,
|
|
39
|
+
} from './scope.js';
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { Store } from '../core/types.js';
|
|
26
|
+
|
|
27
|
+
// Scope identifier
|
|
28
|
+
export type ScopeId = string;
|
|
29
|
+
|
|
30
|
+
// Scope tree node
|
|
31
|
+
export interface ScopeNode {
|
|
32
|
+
id: ScopeId;
|
|
33
|
+
parent: ScopeNode | null;
|
|
34
|
+
stores: Map<string, Store<unknown>>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const ROOT_SCOPE: ScopeNode = {
|
|
38
|
+
id: '__root__',
|
|
39
|
+
parent: null,
|
|
40
|
+
stores: new Map(),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
let currentScope: ScopeNode = ROOT_SCOPE;
|
|
44
|
+
const scopeRegistry = new Map<ScopeId, ScopeNode>([
|
|
45
|
+
[ROOT_SCOPE.id, ROOT_SCOPE],
|
|
46
|
+
]);
|
|
47
|
+
let scopeCounter = 0;
|
|
48
|
+
|
|
49
|
+
// Initialize child scope
|
|
50
|
+
export const createScope = (parentScope?: ScopeNode): ScopeNode => {
|
|
51
|
+
scopeCounter++;
|
|
52
|
+
const id = `scope_${String(scopeCounter)}`;
|
|
53
|
+
const node: ScopeNode = {
|
|
54
|
+
id,
|
|
55
|
+
parent: parentScope ?? currentScope,
|
|
56
|
+
stores: new Map(),
|
|
57
|
+
};
|
|
58
|
+
scopeRegistry.set(id, node);
|
|
59
|
+
return node;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// Dispose scope
|
|
63
|
+
export const disposeScope = (scope: ScopeNode): void => {
|
|
64
|
+
scope.stores.clear();
|
|
65
|
+
scopeRegistry.delete(scope.id);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// Activate scope
|
|
69
|
+
export const enterScope = (scope: ScopeNode): void => {
|
|
70
|
+
currentScope = scope;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// Exit active scope
|
|
74
|
+
export const exitScope = (): void => {
|
|
75
|
+
if (currentScope.parent) {
|
|
76
|
+
currentScope = currentScope.parent;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// Current scope access
|
|
81
|
+
export const getCurrentScope = (): ScopeNode => currentScope;
|
|
82
|
+
|
|
83
|
+
// Root scope access
|
|
84
|
+
export const getRootScope = (): ScopeNode => ROOT_SCOPE;
|
|
85
|
+
|
|
86
|
+
// Register store in scope
|
|
87
|
+
export const registerScopedStore = <T>(
|
|
88
|
+
name: string,
|
|
89
|
+
store: Store<T>,
|
|
90
|
+
scope: ScopeNode = currentScope
|
|
91
|
+
): void => {
|
|
92
|
+
scope.stores.set(name, store as Store<unknown>);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// Find store in scope hierarchy
|
|
96
|
+
export const getScopedStore = <T>(
|
|
97
|
+
name: string,
|
|
98
|
+
scope: ScopeNode = currentScope
|
|
99
|
+
): Store<T> | null => {
|
|
100
|
+
let current: ScopeNode | null = scope;
|
|
101
|
+
while (current) {
|
|
102
|
+
const store = current.stores.get(name);
|
|
103
|
+
if (store) return store as Store<T>;
|
|
104
|
+
current = current.parent;
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// Scope store detection
|
|
110
|
+
export const hasScopedStore = (
|
|
111
|
+
name: string,
|
|
112
|
+
scope: ScopeNode = currentScope
|
|
113
|
+
): boolean => {
|
|
114
|
+
return getScopedStore(name, scope) !== null;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// Execute in scope
|
|
118
|
+
export const runInScope = <R>(scope: ScopeNode, fn: () => R): R => {
|
|
119
|
+
const prev = currentScope;
|
|
120
|
+
currentScope = scope;
|
|
121
|
+
try {
|
|
122
|
+
return fn();
|
|
123
|
+
} finally {
|
|
124
|
+
currentScope = prev;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// Execute in transient scope
|
|
129
|
+
export const withScope = <R>(fn: (scope: ScopeNode) => R): R => {
|
|
130
|
+
const scope = createScope();
|
|
131
|
+
try {
|
|
132
|
+
return runInScope(scope, () => fn(scope));
|
|
133
|
+
} finally {
|
|
134
|
+
disposeScope(scope);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export { createStore, type CreateStoreOptions } from './store.js';
|
|
26
|
+
export { createAtomicState, type AtomicState } from './state.js';
|
|
27
|
+
export {
|
|
28
|
+
type Store,
|
|
29
|
+
type StoreState,
|
|
30
|
+
type StoreContext,
|
|
31
|
+
type StoreDefinition,
|
|
32
|
+
type StoreOptions,
|
|
33
|
+
type ActionContext,
|
|
34
|
+
type Middleware,
|
|
35
|
+
type InferStoreState,
|
|
36
|
+
} from './types.js';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Chris M. Perez
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { Effect, SubscriptionRef } from 'effect';
|
|
26
|
+
|
|
27
|
+
// Atomic state management with synchronous accessors
|
|
28
|
+
export interface AtomicState<T extends Record<string, unknown>> {
|
|
29
|
+
get: () => T;
|
|
30
|
+
set: (value: T) => void;
|
|
31
|
+
update: (fn: (state: T) => T) => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Build atomic state container
|
|
35
|
+
export const createAtomicState = <T extends Record<string, unknown>>(
|
|
36
|
+
initial: T
|
|
37
|
+
): AtomicState<T> => {
|
|
38
|
+
const ref = Effect.runSync(SubscriptionRef.make(initial));
|
|
39
|
+
return {
|
|
40
|
+
get: () => Effect.runSync(SubscriptionRef.get(ref)),
|
|
41
|
+
set: (value: T) => {
|
|
42
|
+
Effect.runSync(SubscriptionRef.set(ref, value));
|
|
43
|
+
},
|
|
44
|
+
update: (fn: (state: T) => T) => {
|
|
45
|
+
Effect.runSync(SubscriptionRef.update(ref, fn));
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
};
|