@thefoxieflow/signalctx 0.0.1
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 +379 -0
- package/dist/index.d.mts +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +119 -0
- package/dist/index.mjs +97 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
# signal-ctx
|
|
2
|
+
|
|
3
|
+
A tiny, signal-based state utility for React that **solves the `useContext` re-render problem** using
|
|
4
|
+
**`useSyncExternalStore`**.
|
|
5
|
+
|
|
6
|
+
If you’ve ever had this issue:
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
{ count, book }
|
|
10
|
+
// updating count re-renders book components 😡
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`signal-ctx` is designed specifically to fix that.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## ✨ Why signal-ctx?
|
|
18
|
+
|
|
19
|
+
### ❌ The Problem with `useContext`
|
|
20
|
+
|
|
21
|
+
React Context subscribes to the **entire value**.
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
const { book } = useContext(StoreCtx)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
When **any property changes**, **every consumer re-renders** — even if they don’t use it.
|
|
28
|
+
|
|
29
|
+
This is not a bug. Context has **no selector mechanism**.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
### ✅ The Solution
|
|
34
|
+
|
|
35
|
+
`signal-ctx`:
|
|
36
|
+
|
|
37
|
+
* Moves state **outside React**
|
|
38
|
+
* Uses **external subscriptions**
|
|
39
|
+
* Allows **selector-based updates**
|
|
40
|
+
|
|
41
|
+
So only the components that *actually use* the changed data re-render.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## ✨ Features
|
|
46
|
+
|
|
47
|
+
* ⚡ Signal-style state container
|
|
48
|
+
* 🎯 Selector-based subscriptions
|
|
49
|
+
* 🧵 React 18 concurrent-safe
|
|
50
|
+
* 🧩 Context-backed but not context-driven
|
|
51
|
+
* 📦 Very small bundle size
|
|
52
|
+
* 🌳 Tree-shakable
|
|
53
|
+
* 🧠 Explicit and predictable
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 📦 Installation
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm install @thefoxieflow/signal-ctx
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
> **Peer dependency:** React 18+
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## 🧠 Core Idea
|
|
68
|
+
|
|
69
|
+
Context **does not store state**.
|
|
70
|
+
|
|
71
|
+
It stores a **stable signal reference**.
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
<Provider value={store} />
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The state lives **outside React**, and components subscribe **directly to the signal**.
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## 🔹 Signal Store
|
|
82
|
+
|
|
83
|
+
A signal is:
|
|
84
|
+
|
|
85
|
+
* A function that returns state
|
|
86
|
+
* Can be subscribed to
|
|
87
|
+
* Can be updated imperatively
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
type Store<T> = {
|
|
91
|
+
(): T
|
|
92
|
+
subscribe(fn: () => void): () => void
|
|
93
|
+
set(action: SetStateAction<T>): void
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## 🔹 Low-Level Hooks
|
|
100
|
+
|
|
101
|
+
### `createStore(()=>{})`
|
|
102
|
+
|
|
103
|
+
### `useStoreValue(store, selector?)`
|
|
104
|
+
|
|
105
|
+
Subscribe to a signal.
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
const count = useStoreValue(store, s => s.count)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
* Uses `useSyncExternalStore`
|
|
112
|
+
* Re-renders only when the selected value changes
|
|
113
|
+
* Selector is optional
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
### `useSetStore(store, selector?)`
|
|
118
|
+
|
|
119
|
+
Returns a setter function.
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
const set = useSetStore(store)
|
|
123
|
+
|
|
124
|
+
set(prev => ({ ...prev, count: prev.count + 1 }))
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Scoped update:
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
// book must be object for selector
|
|
131
|
+
const setBook = useSetSignal(store, s => s.book)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
// put: update an object
|
|
135
|
+
setBook({
|
|
136
|
+
title : "book"
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
// patch: update partially
|
|
140
|
+
setBook((b)=>{
|
|
141
|
+
b.title = "book"
|
|
142
|
+
})
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
⚠️ Updates are **mutation-based**. Spread manually if you want immutability.
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## 🔹 Context-Based API
|
|
150
|
+
|
|
151
|
+
### `createCtx(init)`
|
|
152
|
+
|
|
153
|
+
Creates a **context-backed signal store hook**.
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
import { createCtx } from "@your-scope/signal-ctx"
|
|
157
|
+
|
|
158
|
+
export const useStore = createCtx(() => ({
|
|
159
|
+
count: 0,
|
|
160
|
+
book: { title: "1984" }
|
|
161
|
+
}))
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
## 🚀 Usage
|
|
167
|
+
|
|
168
|
+
### 1. Create a Provider
|
|
169
|
+
|
|
170
|
+
```tsx
|
|
171
|
+
// use default initial value from useStore
|
|
172
|
+
type Props = {
|
|
173
|
+
children: React.ReactNode
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function StoreProvider({ children }: Props) {
|
|
177
|
+
const Provider = useStore.provide()
|
|
178
|
+
return <Provider>{children}</Provider>
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// overwrite value
|
|
182
|
+
export function StoreProvider({ children }: Props) {
|
|
183
|
+
const Provider = useStore.provide({
|
|
184
|
+
value: {
|
|
185
|
+
count: 10,
|
|
186
|
+
book: { title: "Brave New World" }
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
return <Provider>{children}</Provider>
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
```tsx
|
|
195
|
+
<StoreProvider>
|
|
196
|
+
<App />
|
|
197
|
+
</StoreProvider>
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
### 2. Read only what you need
|
|
203
|
+
|
|
204
|
+
```tsx
|
|
205
|
+
function Count() {
|
|
206
|
+
const count = useStore(s => s.count)
|
|
207
|
+
return <div>{count}</div>
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function Book() {
|
|
211
|
+
const book = useStore(s => s.book)
|
|
212
|
+
return <div>{book.title}</div>
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
### 3. Update state
|
|
219
|
+
|
|
220
|
+
```tsx
|
|
221
|
+
function Increment() {
|
|
222
|
+
const set = useStore.useSetter()
|
|
223
|
+
|
|
224
|
+
return (
|
|
225
|
+
<button onClick={() =>
|
|
226
|
+
set(s => ({ ...s, count: s.count + 1 }))
|
|
227
|
+
}>
|
|
228
|
+
+
|
|
229
|
+
</button>
|
|
230
|
+
)
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
✅ Updating `count` **does NOT re-render** `Book`.
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## 🧩 Why This Works
|
|
239
|
+
|
|
240
|
+
* Context value never changes
|
|
241
|
+
* React does not re-render on context updates
|
|
242
|
+
* `useSyncExternalStore` compares selected snapshots
|
|
243
|
+
* Only changed selectors trigger re-renders
|
|
244
|
+
|
|
245
|
+
This is the **same model** used by:
|
|
246
|
+
|
|
247
|
+
* Redux `useSelector`
|
|
248
|
+
* Zustand selectors
|
|
249
|
+
* React’s official external store docs
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
## ⚠️ Important Rule
|
|
254
|
+
|
|
255
|
+
> **Never destructure the entire state.**
|
|
256
|
+
> Always select the smallest possible slice.
|
|
257
|
+
|
|
258
|
+
❌ Bad:
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
const state = useStore(s => s)
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
✅ Good:
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
const count = useStore(s => s.count)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## 🧩 Multiple Stores
|
|
273
|
+
|
|
274
|
+
You can create isolated stores using `storeName`.
|
|
275
|
+
|
|
276
|
+
```tsx
|
|
277
|
+
type Props = {
|
|
278
|
+
children: React.ReactNode
|
|
279
|
+
storeName: string
|
|
280
|
+
initialValue?: { count: number; book: { title: string } }
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function StoreProvider({ children, storeName, initialValue }: Props) {
|
|
284
|
+
const Provider = useStore.provide({
|
|
285
|
+
storeName,
|
|
286
|
+
value: initialValue || { count: 0, book: { title: "1984" } }
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
return <Provider>{children}</Provider>
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Usage
|
|
294
|
+
```tsx
|
|
295
|
+
<StoreProvider storeName="storeA" initialValue={{ count: 1, book: { title: "A" } }}>
|
|
296
|
+
<AppA />
|
|
297
|
+
<StoreProvider storeName="storeB" initialValue={{ count: 5, book: { title: "B" } }}>
|
|
298
|
+
<AppB />
|
|
299
|
+
</StoreProvider>
|
|
300
|
+
</StoreProvider>
|
|
301
|
+
|
|
302
|
+
function AppB(){
|
|
303
|
+
// book from parent context; parent = "storeA" so book.title = "A"
|
|
304
|
+
const book = useStore(s => s.book)
|
|
305
|
+
|
|
306
|
+
// but i want a book from storeA; so it requires storeName
|
|
307
|
+
// book.title = "B"
|
|
308
|
+
const storeABook = useStore(s => s.book,{ storeName :"storeA" })
|
|
309
|
+
|
|
310
|
+
// same apply to setter
|
|
311
|
+
const setBookStoreA = useStore.useSetter((s)=>s.book, {storeName : "storeA"})
|
|
312
|
+
|
|
313
|
+
const handleClick = (text)=>{
|
|
314
|
+
setCountStoreA((s) => {
|
|
315
|
+
s.title = "change A to AA"
|
|
316
|
+
return s
|
|
317
|
+
})
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
Each store is independent.
|
|
325
|
+
|
|
326
|
+
---
|
|
327
|
+
|
|
328
|
+
## 🌐 Server-Side Rendering (SSR)
|
|
329
|
+
|
|
330
|
+
`signal-ctx` is SSR-safe.
|
|
331
|
+
|
|
332
|
+
* Uses `useSyncExternalStore`
|
|
333
|
+
* Identical snapshot logic on server & client
|
|
334
|
+
* No shared global state between requests
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
338
|
+
## ⚠️ Caveats
|
|
339
|
+
|
|
340
|
+
* No middleware
|
|
341
|
+
* No devtools
|
|
342
|
+
* No persistence
|
|
343
|
+
* Mutation-based updates by design
|
|
344
|
+
|
|
345
|
+
Best suited for:
|
|
346
|
+
|
|
347
|
+
* UI state
|
|
348
|
+
* App-level shared state
|
|
349
|
+
* Lightweight global stores
|
|
350
|
+
|
|
351
|
+
---
|
|
352
|
+
|
|
353
|
+
## 🧪 TypeScript
|
|
354
|
+
|
|
355
|
+
Fully typed with generics and inferred selectors.
|
|
356
|
+
|
|
357
|
+
```ts
|
|
358
|
+
const count = useStore(s => s.count) // number
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
---
|
|
362
|
+
|
|
363
|
+
## 📄 License
|
|
364
|
+
|
|
365
|
+
MIT
|
|
366
|
+
|
|
367
|
+
---
|
|
368
|
+
|
|
369
|
+
## ⭐ Philosophy
|
|
370
|
+
|
|
371
|
+
`signal-ctx` is intentionally small.
|
|
372
|
+
|
|
373
|
+
It favors:
|
|
374
|
+
|
|
375
|
+
* Explicit ownership
|
|
376
|
+
* Predictable updates
|
|
377
|
+
* Minimal abstraction
|
|
378
|
+
|
|
379
|
+
If you understand React, you understand `signal-ctx`.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { SetStateAction, ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
type Store<T = any> = ReturnType<typeof createStore<T>>;
|
|
5
|
+
type Subscriber = () => void;
|
|
6
|
+
/**
|
|
7
|
+
* @description: create a signal with subscribe and update methods
|
|
8
|
+
*/
|
|
9
|
+
declare function createStore<T = Record<string, any>>(init: () => T): {
|
|
10
|
+
(): T;
|
|
11
|
+
subscribe(subscriber: Subscriber): () => boolean;
|
|
12
|
+
set(action: SetStateAction<T>): void;
|
|
13
|
+
};
|
|
14
|
+
declare const useStoreValue: <T extends object, Dest = T>(store: Store<T>, selector?: (state: T) => Dest) => Dest;
|
|
15
|
+
declare const useSetStore: <T extends object = any, Dest extends object = T>(store: Store<T>, selector?: (state: T) => Dest) => (action: SetStateAction<Dest>) => void;
|
|
16
|
+
type StoreOption = {
|
|
17
|
+
storeName?: string;
|
|
18
|
+
};
|
|
19
|
+
declare function createCtx<T extends object>(init: () => T): {
|
|
20
|
+
<Dest = any>(selector: (state: T) => Dest, opt?: StoreOption): Dest;
|
|
21
|
+
provide: (opt?: {
|
|
22
|
+
value?: T;
|
|
23
|
+
storeName?: string;
|
|
24
|
+
}) => ({ children }: {
|
|
25
|
+
children: ReactNode;
|
|
26
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
27
|
+
useSetter: <Dest extends object = T>(selector?: (state: T) => Dest, opt?: StoreOption) => (action: SetStateAction<Dest>) => void;
|
|
28
|
+
useStore: (opt?: StoreOption) => {
|
|
29
|
+
(): T;
|
|
30
|
+
subscribe(subscriber: Subscriber): () => boolean;
|
|
31
|
+
set(action: SetStateAction<T>): void;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { type Store, type StoreOption, type Subscriber, createCtx, useSetStore, useStoreValue };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { SetStateAction, ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
type Store<T = any> = ReturnType<typeof createStore<T>>;
|
|
5
|
+
type Subscriber = () => void;
|
|
6
|
+
/**
|
|
7
|
+
* @description: create a signal with subscribe and update methods
|
|
8
|
+
*/
|
|
9
|
+
declare function createStore<T = Record<string, any>>(init: () => T): {
|
|
10
|
+
(): T;
|
|
11
|
+
subscribe(subscriber: Subscriber): () => boolean;
|
|
12
|
+
set(action: SetStateAction<T>): void;
|
|
13
|
+
};
|
|
14
|
+
declare const useStoreValue: <T extends object, Dest = T>(store: Store<T>, selector?: (state: T) => Dest) => Dest;
|
|
15
|
+
declare const useSetStore: <T extends object = any, Dest extends object = T>(store: Store<T>, selector?: (state: T) => Dest) => (action: SetStateAction<Dest>) => void;
|
|
16
|
+
type StoreOption = {
|
|
17
|
+
storeName?: string;
|
|
18
|
+
};
|
|
19
|
+
declare function createCtx<T extends object>(init: () => T): {
|
|
20
|
+
<Dest = any>(selector: (state: T) => Dest, opt?: StoreOption): Dest;
|
|
21
|
+
provide: (opt?: {
|
|
22
|
+
value?: T;
|
|
23
|
+
storeName?: string;
|
|
24
|
+
}) => ({ children }: {
|
|
25
|
+
children: ReactNode;
|
|
26
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
27
|
+
useSetter: <Dest extends object = T>(selector?: (state: T) => Dest, opt?: StoreOption) => (action: SetStateAction<Dest>) => void;
|
|
28
|
+
useStore: (opt?: StoreOption) => {
|
|
29
|
+
(): T;
|
|
30
|
+
subscribe(subscriber: Subscriber): () => boolean;
|
|
31
|
+
set(action: SetStateAction<T>): void;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { type Store, type StoreOption, type Subscriber, createCtx, useSetStore, useStoreValue };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.tsx
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createCtx: () => createCtx,
|
|
24
|
+
useSetStore: () => useSetStore,
|
|
25
|
+
useStoreValue: () => useStoreValue
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
30
|
+
var applyAction = (target, action) => {
|
|
31
|
+
return typeof action === "function" ? action(target) : action;
|
|
32
|
+
};
|
|
33
|
+
function createStore(init) {
|
|
34
|
+
let val;
|
|
35
|
+
let isInit = false;
|
|
36
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
37
|
+
const get = () => {
|
|
38
|
+
if (!isInit) {
|
|
39
|
+
val = init();
|
|
40
|
+
isInit = true;
|
|
41
|
+
}
|
|
42
|
+
return val;
|
|
43
|
+
};
|
|
44
|
+
get.subscribe = (subscriber) => {
|
|
45
|
+
subscribers.add(subscriber);
|
|
46
|
+
const unsubscribe = () => subscribers.delete(subscriber);
|
|
47
|
+
return unsubscribe;
|
|
48
|
+
};
|
|
49
|
+
get.set = (action) => {
|
|
50
|
+
val = applyAction(val, action);
|
|
51
|
+
subscribers.forEach((subscriber) => subscriber());
|
|
52
|
+
};
|
|
53
|
+
return get;
|
|
54
|
+
}
|
|
55
|
+
var useStoreValue = (store, selector) => {
|
|
56
|
+
const getSnapShot = () => selector ? selector(store()) : store();
|
|
57
|
+
const use = (0, import_react.useSyncExternalStore)(store.subscribe, getSnapShot, getSnapShot);
|
|
58
|
+
return use;
|
|
59
|
+
};
|
|
60
|
+
var useSetStore = (store, selector) => {
|
|
61
|
+
const setter = (action) => {
|
|
62
|
+
const current = store();
|
|
63
|
+
const target = selector ? selector(current) : current;
|
|
64
|
+
applyAction(target, action);
|
|
65
|
+
store.set(current);
|
|
66
|
+
};
|
|
67
|
+
return setter;
|
|
68
|
+
};
|
|
69
|
+
function createCtx(init) {
|
|
70
|
+
const ctx = (0, import_react.createContext)(createStore(init));
|
|
71
|
+
const stores = /* @__PURE__ */ new Map();
|
|
72
|
+
const initStore = (name, init2) => {
|
|
73
|
+
if (!stores.has(name)) {
|
|
74
|
+
const store = createStore(init2);
|
|
75
|
+
stores.set(name, store);
|
|
76
|
+
}
|
|
77
|
+
return stores.get(name);
|
|
78
|
+
};
|
|
79
|
+
const useStore = (opt) => {
|
|
80
|
+
const store = (0, import_react.useContext)(ctx);
|
|
81
|
+
if (opt?.storeName && stores.has(opt.storeName)) {
|
|
82
|
+
return stores.get(opt.storeName);
|
|
83
|
+
}
|
|
84
|
+
if (!store) throw new Error("useCtx must be used within a Provider");
|
|
85
|
+
return store;
|
|
86
|
+
};
|
|
87
|
+
const provide = (opt = {}) => {
|
|
88
|
+
const value = opt.value || init();
|
|
89
|
+
const storeName = opt.storeName || "ctx__store";
|
|
90
|
+
const Provider = ({ children }) => {
|
|
91
|
+
const store = initStore(storeName, () => value);
|
|
92
|
+
(0, import_react.useEffect)(() => {
|
|
93
|
+
return () => {
|
|
94
|
+
stores.delete(storeName);
|
|
95
|
+
};
|
|
96
|
+
}, []);
|
|
97
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ctx.Provider, { value: store, children });
|
|
98
|
+
};
|
|
99
|
+
return Provider;
|
|
100
|
+
};
|
|
101
|
+
const use = (selector, opt) => {
|
|
102
|
+
const store = useStore(opt);
|
|
103
|
+
return useStoreValue(store, selector);
|
|
104
|
+
};
|
|
105
|
+
const useSetter = (selector, opt) => {
|
|
106
|
+
const store = useStore(opt);
|
|
107
|
+
return useSetStore(store, selector);
|
|
108
|
+
};
|
|
109
|
+
use.provide = provide;
|
|
110
|
+
use.useSetter = useSetter;
|
|
111
|
+
use.useStore = useStore;
|
|
112
|
+
return use;
|
|
113
|
+
}
|
|
114
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
115
|
+
0 && (module.exports = {
|
|
116
|
+
createCtx,
|
|
117
|
+
useSetStore,
|
|
118
|
+
useStoreValue
|
|
119
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// src/index.tsx
|
|
2
|
+
import {
|
|
3
|
+
createContext,
|
|
4
|
+
useContext,
|
|
5
|
+
useEffect,
|
|
6
|
+
useSyncExternalStore
|
|
7
|
+
} from "react";
|
|
8
|
+
import { jsx } from "react/jsx-runtime";
|
|
9
|
+
var applyAction = (target, action) => {
|
|
10
|
+
return typeof action === "function" ? action(target) : action;
|
|
11
|
+
};
|
|
12
|
+
function createStore(init) {
|
|
13
|
+
let val;
|
|
14
|
+
let isInit = false;
|
|
15
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
16
|
+
const get = () => {
|
|
17
|
+
if (!isInit) {
|
|
18
|
+
val = init();
|
|
19
|
+
isInit = true;
|
|
20
|
+
}
|
|
21
|
+
return val;
|
|
22
|
+
};
|
|
23
|
+
get.subscribe = (subscriber) => {
|
|
24
|
+
subscribers.add(subscriber);
|
|
25
|
+
const unsubscribe = () => subscribers.delete(subscriber);
|
|
26
|
+
return unsubscribe;
|
|
27
|
+
};
|
|
28
|
+
get.set = (action) => {
|
|
29
|
+
val = applyAction(val, action);
|
|
30
|
+
subscribers.forEach((subscriber) => subscriber());
|
|
31
|
+
};
|
|
32
|
+
return get;
|
|
33
|
+
}
|
|
34
|
+
var useStoreValue = (store, selector) => {
|
|
35
|
+
const getSnapShot = () => selector ? selector(store()) : store();
|
|
36
|
+
const use = useSyncExternalStore(store.subscribe, getSnapShot, getSnapShot);
|
|
37
|
+
return use;
|
|
38
|
+
};
|
|
39
|
+
var useSetStore = (store, selector) => {
|
|
40
|
+
const setter = (action) => {
|
|
41
|
+
const current = store();
|
|
42
|
+
const target = selector ? selector(current) : current;
|
|
43
|
+
applyAction(target, action);
|
|
44
|
+
store.set(current);
|
|
45
|
+
};
|
|
46
|
+
return setter;
|
|
47
|
+
};
|
|
48
|
+
function createCtx(init) {
|
|
49
|
+
const ctx = createContext(createStore(init));
|
|
50
|
+
const stores = /* @__PURE__ */ new Map();
|
|
51
|
+
const initStore = (name, init2) => {
|
|
52
|
+
if (!stores.has(name)) {
|
|
53
|
+
const store = createStore(init2);
|
|
54
|
+
stores.set(name, store);
|
|
55
|
+
}
|
|
56
|
+
return stores.get(name);
|
|
57
|
+
};
|
|
58
|
+
const useStore = (opt) => {
|
|
59
|
+
const store = useContext(ctx);
|
|
60
|
+
if (opt?.storeName && stores.has(opt.storeName)) {
|
|
61
|
+
return stores.get(opt.storeName);
|
|
62
|
+
}
|
|
63
|
+
if (!store) throw new Error("useCtx must be used within a Provider");
|
|
64
|
+
return store;
|
|
65
|
+
};
|
|
66
|
+
const provide = (opt = {}) => {
|
|
67
|
+
const value = opt.value || init();
|
|
68
|
+
const storeName = opt.storeName || "ctx__store";
|
|
69
|
+
const Provider = ({ children }) => {
|
|
70
|
+
const store = initStore(storeName, () => value);
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
return () => {
|
|
73
|
+
stores.delete(storeName);
|
|
74
|
+
};
|
|
75
|
+
}, []);
|
|
76
|
+
return /* @__PURE__ */ jsx(ctx.Provider, { value: store, children });
|
|
77
|
+
};
|
|
78
|
+
return Provider;
|
|
79
|
+
};
|
|
80
|
+
const use = (selector, opt) => {
|
|
81
|
+
const store = useStore(opt);
|
|
82
|
+
return useStoreValue(store, selector);
|
|
83
|
+
};
|
|
84
|
+
const useSetter = (selector, opt) => {
|
|
85
|
+
const store = useStore(opt);
|
|
86
|
+
return useSetStore(store, selector);
|
|
87
|
+
};
|
|
88
|
+
use.provide = provide;
|
|
89
|
+
use.useSetter = useSetter;
|
|
90
|
+
use.useStore = useStore;
|
|
91
|
+
return use;
|
|
92
|
+
}
|
|
93
|
+
export {
|
|
94
|
+
createCtx,
|
|
95
|
+
useSetStore,
|
|
96
|
+
useStoreValue
|
|
97
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thefoxieflow/signalctx",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Lightweight signal-based React context store",
|
|
5
|
+
"main": "dist/index.cjs",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsup src/index.tsx --format esm,cjs --dts --out-dir dist",
|
|
13
|
+
"prepublishOnly": "npm run build",
|
|
14
|
+
"publish": "npm publish"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/foxie-io/thefoxieflow",
|
|
19
|
+
"directory": "signalctx"
|
|
20
|
+
},
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/react": "^19.2.8",
|
|
27
|
+
"react": "^18.2.0",
|
|
28
|
+
"tsup": "^8.0.0",
|
|
29
|
+
"typescript": "^5.0.0"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"react",
|
|
33
|
+
"state",
|
|
34
|
+
"store",
|
|
35
|
+
"context",
|
|
36
|
+
"signal"
|
|
37
|
+
]
|
|
38
|
+
}
|