stunk 2.2.2 β 2.3.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 +161 -0
- package/package.json +16 -5
package/README.md
CHANGED
|
@@ -32,6 +32,167 @@ Read Docs:
|
|
|
32
32
|
|
|
33
33
|
[Stunk](https://stunk.vercel.app/)
|
|
34
34
|
|
|
35
|
+
## Creating a Chunk
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { chunk } from "stunk";
|
|
39
|
+
|
|
40
|
+
// Create a chunk holding a number
|
|
41
|
+
const count = chunk(0);
|
|
42
|
+
|
|
43
|
+
// Create a chunk holding a string
|
|
44
|
+
const name = chunk("Stunky, chunky");
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
π [See full explanation in docs](https://stunk.vercel.app/chunk.html)
|
|
48
|
+
|
|
49
|
+
## Interacting with a Chunk
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
// Get value
|
|
53
|
+
console.log(count.get()); // 0
|
|
54
|
+
|
|
55
|
+
// Set a new value
|
|
56
|
+
count.set(10);
|
|
57
|
+
|
|
58
|
+
// Update based on the previous value
|
|
59
|
+
count.set((prev) => prev + 1);
|
|
60
|
+
|
|
61
|
+
// Reset to the initial value
|
|
62
|
+
count.reset();
|
|
63
|
+
|
|
64
|
+
// Destroy the chunk and all its subscribers.
|
|
65
|
+
count.destroy();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
π [See full explanation in docs](https://stunk.vercel.app/chunk.html)
|
|
69
|
+
|
|
70
|
+
## React via useChunk
|
|
71
|
+
|
|
72
|
+
The `useChunk` hook, enables components to reactively read and update state from a Chunk. The counter example below depicts
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { chunk } from "stunk";
|
|
76
|
+
import { useChunk } from "stunk/react";
|
|
77
|
+
|
|
78
|
+
const count = chunk(0);
|
|
79
|
+
|
|
80
|
+
const Counter = () => {
|
|
81
|
+
const [value, set, reset] = useChunk(count);
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<div>
|
|
85
|
+
<p>Count: {value}</p>
|
|
86
|
+
<button onClick={() => set((prev) => prev + 1)}>Increment</button>
|
|
87
|
+
<button onClick={() => reset()}>Reset</button>
|
|
88
|
+
</div>
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
π [See full explanation in docs](https://stunk.vercel.app/useChunk.html)
|
|
94
|
+
|
|
95
|
+
## React via useDerive
|
|
96
|
+
|
|
97
|
+
Hook that lets you create a read-only derived state from a Chunk. It keeps the derived value reactive, automatically updating whenever the source Chunk changes.
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { chunk } from "stunk";
|
|
101
|
+
import { useDerive } from "stunk/react";
|
|
102
|
+
|
|
103
|
+
const count = chunk(0);
|
|
104
|
+
|
|
105
|
+
const DoubledCount = () => {
|
|
106
|
+
const double = useDerive(count, (value) => value * 2);
|
|
107
|
+
|
|
108
|
+
return <p>Double: {double}</p>;
|
|
109
|
+
};
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
π [See full explanation in docs](https://stunk.vercel.app/useDerive.html)
|
|
113
|
+
|
|
114
|
+
## React via useComputed
|
|
115
|
+
|
|
116
|
+
Hook that derives a computed value from one or more Chunks. It automatically re-evaluates whenever any of its dependencies change, ensuring efficient and reactive updates.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { chunk } from "stunk";
|
|
120
|
+
import { useComputed } from "stunk/react";
|
|
121
|
+
|
|
122
|
+
const count = chunk(2);
|
|
123
|
+
const multiplier = chunk(3);
|
|
124
|
+
|
|
125
|
+
const ComputedExample = () => {
|
|
126
|
+
const product = useComputed([count, multiplier], (c, m) => c * m);
|
|
127
|
+
|
|
128
|
+
return <p>Product: {product}</p>;
|
|
129
|
+
};
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
π [See full explanation in docs](https://stunk.vercel.app/useComputed.html)
|
|
133
|
+
|
|
134
|
+
## React via useAsyncChunk
|
|
135
|
+
|
|
136
|
+
Hook that manages that manages asynchronous state. It offers built-in reactivity, handling loading, error, and data states, ensuring the UI stays in sync with asynchronous operations.
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { asyncChunk } from "stunk";
|
|
140
|
+
import { useAsyncChunk } from "stunk/react";
|
|
141
|
+
|
|
142
|
+
const fetchUser = asyncChunk(async () => {
|
|
143
|
+
const res = await fetch("https://jsonplaceholder.typicode.com/users/1");
|
|
144
|
+
return res.json();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const UserProfile = () => {
|
|
148
|
+
const { data, loading, error, reload } = useAsyncChunk(fetchUser);
|
|
149
|
+
|
|
150
|
+
if (loading) return <p>Loading...</p>;
|
|
151
|
+
if (error) return <p>Error: {error.message}</p>;
|
|
152
|
+
|
|
153
|
+
return (
|
|
154
|
+
<div>
|
|
155
|
+
<h2>{data.name}</h2>
|
|
156
|
+
<p>{data.email}</p>
|
|
157
|
+
<button onClick={reload}>Reload</button>
|
|
158
|
+
</div>
|
|
159
|
+
);
|
|
160
|
+
};
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
π [See full explanation in docs](https://stunk.vercel.app/useAysncChunk.html)
|
|
164
|
+
|
|
165
|
+
## React via useChunkValue
|
|
166
|
+
|
|
167
|
+
Hook that subscribes to a Chunk and returns its current value. It is useful for read-only components that donβt need to modify the state.
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { chunk } from "stunk";
|
|
171
|
+
import { useChunkValue } from "stunk/react";
|
|
172
|
+
|
|
173
|
+
const count = chunk(0);
|
|
174
|
+
|
|
175
|
+
const CounterDisplay = () => {
|
|
176
|
+
const value = useChunkValue(count);
|
|
177
|
+
|
|
178
|
+
return <p>Count: {value}</p>;
|
|
179
|
+
};
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
π [See full explanation in docs](https://stunk.vercel.app/read-only-values.html)
|
|
183
|
+
|
|
184
|
+
Live Examples:
|
|
185
|
+
|
|
186
|
+
π [Visit](https://stunk-examples.vercel.app/)
|
|
187
|
+
|
|
188
|
+
Coding Examples:
|
|
189
|
+
|
|
190
|
+
π [Visit](https://stunk.vercel.app/examples.html)
|
|
191
|
+
|
|
192
|
+
Further Examples:
|
|
193
|
+
|
|
194
|
+
π [Visit](https://github.com/I-am-abdulazeez/stunk-examples/)
|
|
195
|
+
|
|
35
196
|
## Contributing
|
|
36
197
|
|
|
37
198
|
Contributions are welcome! Please feel free to submit a Pull Request.
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stunk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Stunk is a lightweight, framework-agnostic state management library for JavaScript and TypeScript. It uses chunk-based state units for efficient updates, reactivity, and performance optimization in React, Vue, Svelte, and Vanilla JS/TS applications.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "tsc --project tsconfig.json && npm run build:cjs",
|
|
7
7
|
"build:cjs": "echo 'module.exports = require(\"./index.js\");' > dist/index.cjs",
|
|
8
8
|
"test": "tsc --project tsconfig.test.json && vitest",
|
|
9
|
+
"test:react17": "npm install react@^17.0.0 react-dom@^17.0.0 @types/react@^17.0.0 && npm test",
|
|
10
|
+
"test:react18": "npm install react@^18.0.0 react-dom@^18.0.0 @types/react@^18.0.0 && npm test",
|
|
11
|
+
"test:react19": "npm install react@^19.0.0 react-dom@^19.0.0 @types/react@^19.0.0 && npm test",
|
|
9
12
|
"prepublishOnly": "vitest && tsc",
|
|
10
13
|
"prepare": "npm run build",
|
|
11
14
|
"lint": "eslint . --ext .js,.ts,.tsx,.vue"
|
|
@@ -79,7 +82,7 @@
|
|
|
79
82
|
"@testing-library/dom": "^10.4.0",
|
|
80
83
|
"@testing-library/react": "^16.2.0",
|
|
81
84
|
"@testing-library/vue": "^8.1.0",
|
|
82
|
-
"@types/react": "^
|
|
85
|
+
"@types/react": "^18.0.0",
|
|
83
86
|
"@typescript-eslint/eslint-plugin": "^8.26.1",
|
|
84
87
|
"@typescript-eslint/parser": "^8.27.0",
|
|
85
88
|
"@vitejs/plugin-react": "^4.3.4",
|
|
@@ -87,14 +90,22 @@
|
|
|
87
90
|
"eslint": "^9.22.0",
|
|
88
91
|
"eslint-plugin-react-hooks": "^5.2.0",
|
|
89
92
|
"jsdom": "^26.0.0",
|
|
90
|
-
"react": "^
|
|
91
|
-
"react-dom": "^
|
|
93
|
+
"react": "^18.0.0",
|
|
94
|
+
"react-dom": "^18.0.0",
|
|
92
95
|
"typescript": "^5.0.0",
|
|
93
96
|
"vitest": "^3.0.8",
|
|
94
97
|
"vue": "^3.5.13"
|
|
95
98
|
},
|
|
96
99
|
"peerDependencies": {
|
|
97
|
-
"react": "^19.0.0",
|
|
100
|
+
"react": "^17.0.0 || ^18.0.0 || ^19.0.0",
|
|
98
101
|
"vue": "^3.5.13"
|
|
102
|
+
},
|
|
103
|
+
"peerDependenciesMeta": {
|
|
104
|
+
"react": {
|
|
105
|
+
"optional": true
|
|
106
|
+
},
|
|
107
|
+
"vue": {
|
|
108
|
+
"optional": true
|
|
109
|
+
}
|
|
99
110
|
}
|
|
100
111
|
}
|