stunk 2.0.0 → 2.2.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 +27 -27
- package/README.md +43 -43
- package/dist/src/core/asyncChunk.d.ts +28 -0
- package/dist/{core → src/core}/asyncChunk.js +8 -4
- package/dist/{core → src/core}/computed.d.ts +5 -0
- package/dist/src/core/computed.js +58 -0
- package/dist/{core → src/core}/core.d.ts +4 -0
- package/dist/{core → src/core}/core.js +41 -33
- package/dist/src/core/selector.d.ts +17 -0
- package/dist/src/core/selector.js +45 -0
- package/dist/{core → src/core}/types.d.ts +3 -3
- package/dist/{use-react → src/use-react}/hooks/useAsyncChunk.d.ts +3 -3
- package/dist/{use-react → src/use-react}/hooks/useAsyncChunk.js +1 -1
- package/dist/{use-react → src/use-react}/hooks/useDerive.js +8 -2
- package/dist/{utils.d.ts → src/utils.d.ts} +1 -0
- package/dist/{utils.js → src/utils.js} +34 -0
- package/dist/tests/async-chunk.test.d.ts +1 -0
- package/dist/tests/async-chunk.test.js +164 -0
- package/dist/tests/batch-chunk.test.d.ts +1 -0
- package/dist/tests/batch-chunk.test.js +89 -0
- package/dist/tests/chunk.test.d.ts +1 -0
- package/dist/tests/chunk.test.js +215 -0
- package/dist/tests/computed.test.d.ts +1 -0
- package/dist/tests/computed.test.js +192 -0
- package/dist/tests/diamond-dep.test.d.ts +1 -0
- package/dist/tests/diamond-dep.test.js +74 -0
- package/dist/tests/history.test.d.ts +1 -0
- package/dist/tests/history.test.js +73 -0
- package/dist/tests/middleware.test.d.ts +1 -0
- package/dist/tests/middleware.test.js +30 -0
- package/dist/tests/persist.test.d.ts +1 -0
- package/dist/tests/persist.test.js +43 -0
- package/dist/tests/select-chunk.test.d.ts +1 -0
- package/dist/tests/select-chunk.test.js +167 -0
- package/package.json +97 -79
- package/dist/core/asyncChunk.d.ts +0 -22
- package/dist/core/computed.js +0 -42
- package/dist/core/selector.d.ts +0 -2
- package/dist/core/selector.js +0 -23
- package/jest.config.js +0 -4
- package/src/core/asyncChunk.ts +0 -83
- package/src/core/computed.ts +0 -65
- package/src/core/core.ts +0 -128
- package/src/core/selector.ts +0 -27
- package/src/core/types.ts +0 -17
- package/src/index.ts +0 -10
- package/src/middleware/history.ts +0 -113
- package/src/middleware/index.ts +0 -7
- package/src/middleware/logger.ts +0 -6
- package/src/middleware/persistence.ts +0 -45
- package/src/middleware/validator.ts +0 -8
- package/src/use-react/hooks/useAsyncChunk.ts +0 -38
- package/src/use-react/hooks/useChunk.ts +0 -40
- package/src/use-react/hooks/useChunkProperty.ts +0 -21
- package/src/use-react/hooks/useChunkValue.ts +0 -15
- package/src/use-react/hooks/useChunkValues.ts +0 -35
- package/src/use-react/hooks/useComputed.ts +0 -34
- package/src/use-react/hooks/useDerive.ts +0 -15
- package/src/use-react/index.ts +0 -9
- package/src/utils.ts +0 -103
- package/tests/async-chunk.test.ts +0 -215
- package/tests/batch-chunk.test.ts +0 -108
- package/tests/chunk.test.ts +0 -189
- package/tests/computed.test.ts +0 -93
- package/tests/history.test.ts +0 -99
- package/tests/middleware.test.ts +0 -37
- package/tests/persist.test.ts +0 -57
- package/tests/select-chunk.test.ts +0 -133
- package/tests/update.test.ts +0 -70
- package/tsconfig.json +0 -23
- /package/dist/{core → src/core}/types.js +0 -0
- /package/dist/{index.d.ts → src/index.d.ts} +0 -0
- /package/dist/{index.js → src/index.js} +0 -0
- /package/dist/{middleware → src/middleware}/history.d.ts +0 -0
- /package/dist/{middleware → src/middleware}/history.js +0 -0
- /package/dist/{middleware → src/middleware}/index.d.ts +0 -0
- /package/dist/{middleware → src/middleware}/index.js +0 -0
- /package/dist/{middleware → src/middleware}/logger.d.ts +0 -0
- /package/dist/{middleware → src/middleware}/logger.js +0 -0
- /package/dist/{middleware → src/middleware}/persistence.d.ts +0 -0
- /package/dist/{middleware → src/middleware}/persistence.js +0 -0
- /package/dist/{middleware → src/middleware}/validator.d.ts +0 -0
- /package/dist/{middleware → src/middleware}/validator.js +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useChunk.d.ts +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useChunk.js +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useChunkProperty.d.ts +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useChunkProperty.js +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useChunkValue.d.ts +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useChunkValue.js +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useChunkValues.d.ts +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useChunkValues.js +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useComputed.d.ts +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useComputed.js +0 -0
- /package/dist/{use-react → src/use-react}/hooks/useDerive.d.ts +0 -0
- /package/dist/{use-react → src/use-react}/index.d.ts +0 -0
- /package/dist/{use-react → src/use-react}/index.js +0 -0
package/LICENSE
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
---
|
|
2
|
-
|
|
3
|
-
### **`LICENSE`**
|
|
4
|
-
|
|
5
|
-
```text
|
|
6
|
-
MIT License
|
|
7
|
-
|
|
8
|
-
Copyright (c) 2025 AbdulAzeez
|
|
9
|
-
|
|
10
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in all
|
|
18
|
-
copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
-
SOFTWARE.
|
|
27
|
-
```
|
|
1
|
+
---
|
|
2
|
+
|
|
3
|
+
### **`LICENSE`**
|
|
4
|
+
|
|
5
|
+
```text
|
|
6
|
+
MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 AbdulAzeez
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
```
|
package/README.md
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
# Stunk
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
- **Pronunciation**: _Stunk_ (A playful blend of "state" and "chunk")
|
|
6
|
-
|
|
7
|
-
**Stunk** is like dividing your jar into many smaller containers, each holding a single piece of state. These smaller containers are called **chunks**. Each **chunk** can be updated and accessed easily, and any part of your app can subscribe to changes in a chunk so it gets updated automatically.
|
|
8
|
-
|
|
9
|
-
## Features
|
|
10
|
-
|
|
11
|
-
- 🚀 **Lightweight and Fast**: No dependencies, minimal overhead
|
|
12
|
-
- 🔄 **Reactive**: Automatic updates when state changes
|
|
13
|
-
- 📦 **Batch Updates**: Group multiple state updates together
|
|
14
|
-
- 🎯 **Atomic State Management**: Break down state into manageable chunks
|
|
15
|
-
- 🎭 **State Selection**: Select and derive specific parts of the state
|
|
16
|
-
- 🔄 **Async Support**: Handle async state with built-in loading and error states
|
|
17
|
-
- 🔌 **Middleware Support**: Extend functionality with custom middleware
|
|
18
|
-
- ⏱️ **Time Travel**: Undo/redo state changes
|
|
19
|
-
- 🔍 **Type-Safe**: Written in TypeScript with full type inference
|
|
20
|
-
|
|
21
|
-
## Installation
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
npm install stunk
|
|
25
|
-
# or
|
|
26
|
-
yarn add stunk
|
|
27
|
-
# or
|
|
28
|
-
pnpm install stunk
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
Read Docs:
|
|
32
|
-
|
|
33
|
-
[Stunk](https://stunk.vercel.app/)
|
|
34
|
-
|
|
35
|
-
## Contributing
|
|
36
|
-
|
|
37
|
-
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
38
|
-
|
|
39
|
-
[Pull Request](https://github.com/I-am-abdulazeez/stunk/pulls)
|
|
40
|
-
|
|
41
|
-
## License
|
|
42
|
-
|
|
43
|
-
This is licence under MIT
|
|
1
|
+
# Stunk
|
|
2
|
+
|
|
3
|
+
Stunk is a lightweight, framework-agnostic state management library built on atomic state principles. It simplifies state management by breaking state into manageable "chunks", ensuring efficient updates and reactivity.
|
|
4
|
+
|
|
5
|
+
- **Pronunciation**: _Stunk_ (A playful blend of "state" and "chunk")
|
|
6
|
+
|
|
7
|
+
**Stunk** is like dividing your jar into many smaller containers, each holding a single piece of state. These smaller containers are called **chunks**. Each **chunk** can be updated and accessed easily, and any part of your app can subscribe to changes in a chunk so it gets updated automatically.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- 🚀 **Lightweight and Fast**: No dependencies, minimal overhead
|
|
12
|
+
- 🔄 **Reactive**: Automatic updates when state changes
|
|
13
|
+
- 📦 **Batch Updates**: Group multiple state updates together
|
|
14
|
+
- 🎯 **Atomic State Management**: Break down state into manageable chunks
|
|
15
|
+
- 🎭 **State Selection**: Select and derive specific parts of the state
|
|
16
|
+
- 🔄 **Async Support**: Handle async state with built-in loading and error states
|
|
17
|
+
- 🔌 **Middleware Support**: Extend functionality with custom middleware
|
|
18
|
+
- ⏱️ **Time Travel**: Undo/redo state changes
|
|
19
|
+
- 🔍 **Type-Safe**: Written in TypeScript with full type inference
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install stunk
|
|
25
|
+
# or
|
|
26
|
+
yarn add stunk
|
|
27
|
+
# or
|
|
28
|
+
pnpm install stunk
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Read Docs:
|
|
32
|
+
|
|
33
|
+
[Stunk](https://stunk.vercel.app/)
|
|
34
|
+
|
|
35
|
+
## Contributing
|
|
36
|
+
|
|
37
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
38
|
+
|
|
39
|
+
[Pull Request](https://github.com/I-am-abdulazeez/stunk/pulls)
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
This is licence under MIT
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Chunk } from "./core";
|
|
2
|
+
import { AsyncChunkOpt } from "./types";
|
|
3
|
+
export interface AsyncState<T, E extends Error> {
|
|
4
|
+
loading: boolean;
|
|
5
|
+
error: E | null;
|
|
6
|
+
data: T | null;
|
|
7
|
+
}
|
|
8
|
+
export interface AsyncChunk<T, E extends Error = Error> extends Chunk<AsyncState<T, E>> {
|
|
9
|
+
/**
|
|
10
|
+
* Reload the data from the source.
|
|
11
|
+
*/
|
|
12
|
+
reload: () => Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Mutate the data directly.
|
|
15
|
+
*/
|
|
16
|
+
mutate: (mutator: (currentData: T | null) => T) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Reset the state to the initial value.
|
|
19
|
+
*/
|
|
20
|
+
reset: () => void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates an async chunk that handles loading, error, and retry logic.
|
|
24
|
+
* @param fetcher The async function to fetch data.
|
|
25
|
+
* @param options Configuration options for the async chunk.
|
|
26
|
+
* @returns An async chunk instance.
|
|
27
|
+
*/
|
|
28
|
+
export declare function asyncChunk<T, E extends Error = Error>(fetcher: () => Promise<T>, options?: AsyncChunkOpt<T, E>): AsyncChunk<T, E>;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { chunk } from "./core";
|
|
2
|
+
/**
|
|
3
|
+
* Creates an async chunk that handles loading, error, and retry logic.
|
|
4
|
+
* @param fetcher The async function to fetch data.
|
|
5
|
+
* @param options Configuration options for the async chunk.
|
|
6
|
+
* @returns An async chunk instance.
|
|
7
|
+
*/
|
|
2
8
|
export function asyncChunk(fetcher, options = {}) {
|
|
3
9
|
const { initialData = null, onError, retryCount = 0, retryDelay = 1000, } = options;
|
|
4
10
|
const initialState = {
|
|
@@ -18,14 +24,12 @@ export function asyncChunk(fetcher, options = {}) {
|
|
|
18
24
|
await new Promise(resolve => setTimeout(resolve, retryDelay));
|
|
19
25
|
return fetchData(retries - 1);
|
|
20
26
|
}
|
|
21
|
-
|
|
22
|
-
baseChunk.set({ loading: false, error: errorObj, data: baseChunk.get().data });
|
|
27
|
+
baseChunk.set({ loading: false, error: error, data: baseChunk.get().data });
|
|
23
28
|
if (onError) {
|
|
24
|
-
onError(
|
|
29
|
+
onError(error);
|
|
25
30
|
}
|
|
26
31
|
}
|
|
27
32
|
};
|
|
28
|
-
// Initial fetch
|
|
29
33
|
fetchData();
|
|
30
34
|
const asyncChunkInstance = {
|
|
31
35
|
...baseChunk,
|
|
@@ -4,7 +4,12 @@ export type DependencyValues<T extends Chunk<any>[]> = {
|
|
|
4
4
|
[K in keyof T]: T[K] extends Chunk<any> ? ChunkValue<T[K]> : never;
|
|
5
5
|
};
|
|
6
6
|
export interface Computed<T> extends Chunk<T> {
|
|
7
|
+
/**
|
|
8
|
+
* Checks if the computed value needs to be recalculated due to dependency changes.
|
|
9
|
+
* @returns True if the computed value is dirty, false otherwise.
|
|
10
|
+
*/
|
|
7
11
|
isDirty: () => boolean;
|
|
12
|
+
/** Manually forces recalculation of the computed value from its dependencies. */
|
|
8
13
|
recompute: () => void;
|
|
9
14
|
}
|
|
10
15
|
export declare function computed<TDeps extends Chunk<any>[], TResult>(dependencies: [...TDeps], computeFn: (...args: DependencyValues<TDeps>) => TResult): Computed<TResult>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { chunk } from "./core";
|
|
2
|
+
import { shallowEqual } from "../utils";
|
|
3
|
+
export function computed(dependencies, computeFn) {
|
|
4
|
+
const dependencyValues = dependencies.map(dep => dep.get());
|
|
5
|
+
let cachedValue = computeFn(...dependencyValues);
|
|
6
|
+
const computedChunk = chunk(cachedValue);
|
|
7
|
+
const originalSet = computedChunk.set;
|
|
8
|
+
let isDirty = false;
|
|
9
|
+
// Direct synchronous recomputation
|
|
10
|
+
const recompute = () => {
|
|
11
|
+
let hasChanges = false;
|
|
12
|
+
for (let i = 0; i < dependencies.length; i++) {
|
|
13
|
+
const newValue = dependencies[i].get();
|
|
14
|
+
if (newValue !== dependencyValues[i]) {
|
|
15
|
+
dependencyValues[i] = newValue;
|
|
16
|
+
hasChanges = true;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (hasChanges) {
|
|
20
|
+
const newValue = computeFn(...dependencyValues);
|
|
21
|
+
// Fast path for primitives only. Avoids shallowEqual for performance.
|
|
22
|
+
if (newValue !== cachedValue) {
|
|
23
|
+
// Only use shallowEqual for objects when needed
|
|
24
|
+
if (typeof newValue !== 'object' || typeof cachedValue !== 'object' || !shallowEqual(newValue, cachedValue)) {
|
|
25
|
+
cachedValue = newValue;
|
|
26
|
+
originalSet(newValue);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
isDirty = false;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const unsubs = dependencies.map(dep => dep.subscribe(() => {
|
|
33
|
+
isDirty = true;
|
|
34
|
+
recompute();
|
|
35
|
+
}));
|
|
36
|
+
return {
|
|
37
|
+
...computedChunk,
|
|
38
|
+
get: () => {
|
|
39
|
+
if (isDirty)
|
|
40
|
+
recompute();
|
|
41
|
+
return cachedValue;
|
|
42
|
+
},
|
|
43
|
+
recompute,
|
|
44
|
+
isDirty: () => isDirty,
|
|
45
|
+
set: () => { throw new Error('Cannot set values directly on computed. Modify the source chunk instead.'); },
|
|
46
|
+
reset: () => {
|
|
47
|
+
dependencies.forEach(dep => {
|
|
48
|
+
if (typeof dep.reset === 'function') {
|
|
49
|
+
dep.reset();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
isDirty = true;
|
|
53
|
+
recompute();
|
|
54
|
+
return cachedValue;
|
|
55
|
+
},
|
|
56
|
+
destroy: () => { unsubs.forEach(unsub => unsub()); computedChunk.destroy?.(); }
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -14,5 +14,9 @@ export interface Chunk<T> {
|
|
|
14
14
|
/** Destroy the chunk and all its subscribers. */
|
|
15
15
|
destroy: () => void;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Batch multiple chunk updates into a single re-render.
|
|
19
|
+
* Useful for updating multiple chunks at once without causing multiple re-renders.
|
|
20
|
+
*/
|
|
17
21
|
export declare function batch(callback: () => void): void;
|
|
18
22
|
export declare function chunk<T>(initialValue: T, middleware?: Middleware<T>[]): Chunk<T>;
|
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
import { processMiddleware } from "../utils";
|
|
2
|
-
let
|
|
3
|
-
const
|
|
2
|
+
let isBatching = false;
|
|
3
|
+
const dirtyChunks = new Set();
|
|
4
|
+
const chunkRegistry = new Map();
|
|
5
|
+
let chunkIdCounter = 0;
|
|
6
|
+
/**
|
|
7
|
+
* Batch multiple chunk updates into a single re-render.
|
|
8
|
+
* Useful for updating multiple chunks at once without causing multiple re-renders.
|
|
9
|
+
*/
|
|
4
10
|
export function batch(callback) {
|
|
5
|
-
|
|
11
|
+
const wasBatchingBefore = isBatching;
|
|
12
|
+
isBatching = true;
|
|
6
13
|
try {
|
|
7
14
|
callback();
|
|
8
15
|
}
|
|
9
16
|
finally {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
//
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
if (!wasBatchingBefore) {
|
|
18
|
+
isBatching = false;
|
|
19
|
+
const chunks = Array.from(dirtyChunks); // Snapshot to avoid mutation issues
|
|
20
|
+
dirtyChunks.clear(); // Clear early to prevent re-adds
|
|
21
|
+
chunks.forEach(id => {
|
|
22
|
+
const chunk = chunkRegistry.get(id);
|
|
23
|
+
if (chunk)
|
|
24
|
+
chunk.notify();
|
|
25
|
+
});
|
|
15
26
|
}
|
|
16
27
|
}
|
|
17
28
|
}
|
|
@@ -21,32 +32,28 @@ export function chunk(initialValue, middleware = []) {
|
|
|
21
32
|
}
|
|
22
33
|
let value = initialValue;
|
|
23
34
|
const subscribers = new Set();
|
|
24
|
-
|
|
35
|
+
const chunkId = chunkIdCounter++;
|
|
36
|
+
const notify = () => {
|
|
37
|
+
subscribers.forEach(subscriber => subscriber(value));
|
|
38
|
+
};
|
|
39
|
+
chunkRegistry.set(chunkId, { notify });
|
|
25
40
|
const notifySubscribers = () => {
|
|
26
|
-
if (
|
|
27
|
-
if
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (isDirty) {
|
|
31
|
-
subscribers.forEach((subscriber) => subscriber(value));
|
|
32
|
-
isDirty = false;
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
41
|
+
if (subscribers.size === 0)
|
|
42
|
+
return; // Skip if no subscribers
|
|
43
|
+
if (isBatching) {
|
|
44
|
+
dirtyChunks.add(chunkId);
|
|
36
45
|
}
|
|
37
46
|
else {
|
|
38
|
-
|
|
47
|
+
notify();
|
|
39
48
|
}
|
|
40
49
|
};
|
|
41
50
|
const get = () => value;
|
|
42
51
|
const set = (newValueOrUpdater) => {
|
|
43
52
|
let newValue;
|
|
44
53
|
if (typeof newValueOrUpdater === 'function') {
|
|
45
|
-
// Handle updater function
|
|
46
54
|
newValue = newValueOrUpdater(value);
|
|
47
55
|
}
|
|
48
56
|
else {
|
|
49
|
-
// Handle direct value assignment
|
|
50
57
|
newValue = newValueOrUpdater;
|
|
51
58
|
}
|
|
52
59
|
const processedValue = processMiddleware(newValue, middleware);
|
|
@@ -59,35 +66,36 @@ export function chunk(initialValue, middleware = []) {
|
|
|
59
66
|
if (typeof callback !== "function") {
|
|
60
67
|
throw new Error("Callback must be a function.");
|
|
61
68
|
}
|
|
62
|
-
if (subscribers.has(callback)) {
|
|
63
|
-
console.warn("Callback is already subscribed. This may lead to duplicate updates.");
|
|
64
|
-
}
|
|
65
69
|
subscribers.add(callback);
|
|
66
70
|
callback(value);
|
|
67
71
|
return () => subscribers.delete(callback);
|
|
68
72
|
};
|
|
69
73
|
const reset = () => {
|
|
70
74
|
value = initialValue;
|
|
71
|
-
|
|
75
|
+
notifySubscribers();
|
|
72
76
|
};
|
|
73
77
|
const destroy = () => {
|
|
74
|
-
if (subscribers.size > 0) {
|
|
75
|
-
console.warn("Destroying chunk with active subscribers. This may lead to memory leaks.");
|
|
76
|
-
}
|
|
77
|
-
// Just clear subscribers without calling unsubscribe
|
|
78
78
|
subscribers.clear();
|
|
79
79
|
value = initialValue;
|
|
80
|
+
dirtyChunks.delete(chunkId);
|
|
81
|
+
chunkRegistry.delete(chunkId);
|
|
80
82
|
};
|
|
81
83
|
const derive = (fn) => {
|
|
82
84
|
if (typeof fn !== "function") {
|
|
83
85
|
throw new Error("Derive function must be a function.");
|
|
84
86
|
}
|
|
85
|
-
const
|
|
86
|
-
const derivedChunk = chunk(
|
|
87
|
-
subscribe(() => {
|
|
87
|
+
const initialDerivedValue = fn(value);
|
|
88
|
+
const derivedChunk = chunk(initialDerivedValue);
|
|
89
|
+
const unsubscribe = subscribe(() => {
|
|
88
90
|
const newDerivedValue = fn(value);
|
|
89
91
|
derivedChunk.set(newDerivedValue);
|
|
90
92
|
});
|
|
93
|
+
// Add a cleanup method to the derived chunk
|
|
94
|
+
const originalDestroy = derivedChunk.destroy;
|
|
95
|
+
derivedChunk.destroy = () => {
|
|
96
|
+
unsubscribe();
|
|
97
|
+
originalDestroy();
|
|
98
|
+
};
|
|
91
99
|
return derivedChunk;
|
|
92
100
|
};
|
|
93
101
|
return { get, set, subscribe, derive, reset, destroy };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Chunk } from "./core";
|
|
2
|
+
export interface SelectOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for selector functions.
|
|
5
|
+
* @property {boolean} [useShallowEqual] - When true, performs a shallow equality check
|
|
6
|
+
* on the derived selector results to prevent unnecessary updates.
|
|
7
|
+
*/
|
|
8
|
+
useShallowEqual?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a derived read-only chunk based on a selector function.
|
|
12
|
+
* @param sourceChunk The source chunk to derive from.
|
|
13
|
+
* @param selector A function that extracts part of the source value.
|
|
14
|
+
* @param options Optional settings for shallow equality comparison.
|
|
15
|
+
* @returns A read-only derived chunk.
|
|
16
|
+
*/
|
|
17
|
+
export declare function select<T, S>(sourceChunk: Chunk<T>, selector: (value: T) => S, options?: SelectOptions): Chunk<S>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { shallowEqual } from "../utils";
|
|
2
|
+
import { chunk } from "./core";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a derived read-only chunk based on a selector function.
|
|
5
|
+
* @param sourceChunk The source chunk to derive from.
|
|
6
|
+
* @param selector A function that extracts part of the source value.
|
|
7
|
+
* @param options Optional settings for shallow equality comparison.
|
|
8
|
+
* @returns A read-only derived chunk.
|
|
9
|
+
*/
|
|
10
|
+
export function select(sourceChunk, selector, options = {}) {
|
|
11
|
+
const { useShallowEqual = false } = options;
|
|
12
|
+
let prevSourceValue = sourceChunk.get();
|
|
13
|
+
let currentResult = selector(prevSourceValue);
|
|
14
|
+
const derivedChunk = chunk(currentResult);
|
|
15
|
+
const update = () => {
|
|
16
|
+
const newSourceValue = sourceChunk.get();
|
|
17
|
+
const newResult = selector(newSourceValue);
|
|
18
|
+
// Always update the reference to source value
|
|
19
|
+
prevSourceValue = newSourceValue;
|
|
20
|
+
// Check if the result has changed
|
|
21
|
+
const resultChanged = useShallowEqual
|
|
22
|
+
? !shallowEqual(newResult, currentResult)
|
|
23
|
+
: newResult !== currentResult;
|
|
24
|
+
if (resultChanged) {
|
|
25
|
+
currentResult = newResult;
|
|
26
|
+
derivedChunk.set(newResult);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const unsubscribe = sourceChunk.subscribe(update);
|
|
30
|
+
return {
|
|
31
|
+
get: () => derivedChunk.get(),
|
|
32
|
+
set: () => {
|
|
33
|
+
throw new Error('Cannot set values directly on a selector. Modify the source chunk instead.');
|
|
34
|
+
},
|
|
35
|
+
subscribe: derivedChunk.subscribe,
|
|
36
|
+
derive: (fn) => select(derivedChunk, fn, options), // Pass options to nested selectors
|
|
37
|
+
reset: () => {
|
|
38
|
+
throw new Error('Cannot reset a selector chunk. Reset the source chunk instead.');
|
|
39
|
+
},
|
|
40
|
+
destroy: () => {
|
|
41
|
+
unsubscribe();
|
|
42
|
+
derivedChunk.destroy();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { AsyncChunk } from "./asyncChunk";
|
|
2
|
-
export type AsyncChunkOpt<T> = {
|
|
2
|
+
export type AsyncChunkOpt<T, E extends Error> = {
|
|
3
3
|
initialData?: T | null;
|
|
4
|
-
onError?: (error:
|
|
4
|
+
onError?: (error: E) => void;
|
|
5
5
|
retryCount?: number;
|
|
6
6
|
retryDelay?: number;
|
|
7
7
|
};
|
|
8
|
-
export type InferAsyncData<T> = T extends AsyncChunk<infer U> ? U : never;
|
|
8
|
+
export type InferAsyncData<T> = T extends AsyncChunk<infer U, Error> ? U : never;
|
|
9
9
|
export type CombinedData<T> = {
|
|
10
10
|
[K in keyof T]: InferAsyncData<T[K]> | null;
|
|
11
11
|
};
|
|
@@ -3,11 +3,11 @@ import { AsyncChunk, AsyncState } from "../../core/asyncChunk";
|
|
|
3
3
|
* A hook that handles asynchronous state with built-in reactivity.
|
|
4
4
|
* Provides loading, error, and data states.
|
|
5
5
|
*/
|
|
6
|
-
export declare function useAsyncChunk<T>(asyncChunk: AsyncChunk<T>): {
|
|
7
|
-
state: AsyncState<T>;
|
|
6
|
+
export declare function useAsyncChunk<T, E extends Error>(asyncChunk: AsyncChunk<T, E>): {
|
|
7
|
+
state: AsyncState<T, E>;
|
|
8
8
|
data: T | null;
|
|
9
9
|
loading: boolean;
|
|
10
|
-
error:
|
|
10
|
+
error: E | null;
|
|
11
11
|
reload: () => Promise<void>;
|
|
12
12
|
mutate: (mutator: (currentData: T | null) => T) => void;
|
|
13
13
|
reset: () => void;
|
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
1
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
2
2
|
import { useChunk } from "./useChunk";
|
|
3
3
|
/**
|
|
4
4
|
* A hook for creating a read-only derived value from a chunk.
|
|
5
5
|
* Ensures reactivity and updates when the source chunk changes.
|
|
6
6
|
*/
|
|
7
7
|
export function useDerive(chunk, fn) {
|
|
8
|
-
const
|
|
8
|
+
const fnRef = useRef(fn);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
fnRef.current = fn;
|
|
11
|
+
}, [fn]);
|
|
12
|
+
const derivedChunk = useMemo(() => {
|
|
13
|
+
return chunk.derive((value) => fnRef.current(value));
|
|
14
|
+
}, [chunk]);
|
|
9
15
|
const [derivedValue] = useChunk(derivedChunk);
|
|
10
16
|
return derivedValue;
|
|
11
17
|
}
|
|
@@ -77,3 +77,37 @@ export function processMiddleware(initialValue, middleware = []) {
|
|
|
77
77
|
}
|
|
78
78
|
return currentValue;
|
|
79
79
|
}
|
|
80
|
+
export function shallowEqual(a, b) {
|
|
81
|
+
if (a === b) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
if (!a || !b || typeof a !== typeof b) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
88
|
+
if (a.length !== b.length) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
for (let i = 0; i < a.length; i++) {
|
|
92
|
+
if (a[i] !== b[i]) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
if (typeof a === 'object' && typeof b === 'object') {
|
|
99
|
+
const keysA = Object.keys(a);
|
|
100
|
+
const keysB = Object.keys(b);
|
|
101
|
+
if (keysA.length !== keysB.length) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
for (const key of keysA) {
|
|
105
|
+
if (!Object.prototype.hasOwnProperty.call(b, key) || a[key] !== b[key]) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
// For primitive types, return false. Strict equality already handled by initial check
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|