context-scoped-state 0.0.11 → 0.0.13
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 +158 -3
- package/dist/index.js +76 -70
- package/dist/lib/Store.d.ts +3 -4
- package/dist/lib/Store.d.ts.map +1 -1
- package/dist/lib/createStore.d.ts +6 -3
- package/dist/lib/createStore.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<img src="logo.svg" alt="context-scoped-state logo" width="120" height="120">
|
|
2
|
+
<img src="./logo.svg" alt="context-scoped-state logo" width="120" height="120">
|
|
3
3
|
<h1>context-scoped-state</h1>
|
|
4
4
|
<p><strong>State management that respects component boundaries.</strong></p>
|
|
5
5
|
</div>
|
|
@@ -40,6 +40,8 @@ pnpm add context-scoped-state
|
|
|
40
40
|
|
|
41
41
|
### 1. Create Your Store (one file, one export)
|
|
42
42
|
|
|
43
|
+
> Wondering why classes? See [API Design Choices](#api-design-choices).
|
|
44
|
+
|
|
43
45
|
```tsx
|
|
44
46
|
// counterStore.ts
|
|
45
47
|
import { Store, createStoreHook } from 'context-scoped-state';
|
|
@@ -51,7 +53,7 @@ class CounterStore extends Store<{ count: number }> {
|
|
|
51
53
|
|
|
52
54
|
increment() {
|
|
53
55
|
// Callback-based: receives current state, returns new state
|
|
54
|
-
this.setState(state => ({ count: state.count + 1 }));
|
|
56
|
+
this.setState((state) => ({ count: state.count + 1 }));
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
decrement() {
|
|
@@ -109,7 +111,7 @@ class UserStore extends Store<{ name: string; age: number; email: string }> {
|
|
|
109
111
|
|
|
110
112
|
incrementAge() {
|
|
111
113
|
// Callback-based: receives current state, returns partial update
|
|
112
|
-
this.patchState(state => ({ age: state.age + 1 }));
|
|
114
|
+
this.patchState((state) => ({ age: state.age + 1 }));
|
|
113
115
|
}
|
|
114
116
|
}
|
|
115
117
|
```
|
|
@@ -180,6 +182,45 @@ test('shows normal status when balance is healthy', () => {
|
|
|
180
182
|
|
|
181
183
|
No mocking libraries. No global state cleanup. Just render with the state you need.
|
|
182
184
|
|
|
185
|
+
### Dynamic Initial State with Context Value
|
|
186
|
+
|
|
187
|
+
Pass a `value` prop to Context to provide data for `getInitialState()`. This is useful when you need to initialize store state from React props:
|
|
188
|
+
|
|
189
|
+
```tsx
|
|
190
|
+
type CounterState = { count: number };
|
|
191
|
+
|
|
192
|
+
class CounterStore extends Store<CounterState> {
|
|
193
|
+
protected getInitialState(contextValue?: Partial<CounterState>) {
|
|
194
|
+
return { count: contextValue?.count ?? 0 };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
increment() {
|
|
198
|
+
this.setState((s) => ({ count: s.count + 1 }));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const useCounterStore = createStoreHook(CounterStore);
|
|
203
|
+
|
|
204
|
+
// Initialize store state from a React prop
|
|
205
|
+
function CounterWidget({ initialCount }: { initialCount: number }) {
|
|
206
|
+
return (
|
|
207
|
+
<useCounterStore.Context value={{ count: initialCount }}>
|
|
208
|
+
<Counter />
|
|
209
|
+
</useCounterStore.Context>
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Now you can render multiple widgets with different starting values
|
|
214
|
+
function App() {
|
|
215
|
+
return (
|
|
216
|
+
<>
|
|
217
|
+
<CounterWidget initialCount={0} />
|
|
218
|
+
<CounterWidget initialCount={100} />
|
|
219
|
+
</>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
183
224
|
## Why context-scoped-state Over Other Libraries?
|
|
184
225
|
|
|
185
226
|
| Feature | context-scoped-state | Redux | Zustand |
|
|
@@ -230,4 +271,118 @@ No mocking libraries. No global state cleanup. Just render with the state you ne
|
|
|
230
271
|
|
|
231
272
|
---
|
|
232
273
|
|
|
274
|
+
## API Design Choices
|
|
275
|
+
|
|
276
|
+
These design decisions are intentional trade-offs that optimize for debuggability, clarity, and simplicity.
|
|
277
|
+
|
|
278
|
+
### Why Classes for Stores?
|
|
279
|
+
|
|
280
|
+
Classes let us use `protected` on state-setting methods (`setState`, `patchState`). This means all state updates must go through the store class — components cannot directly modify state.
|
|
281
|
+
|
|
282
|
+
**Why this matters:** When debugging, you can set a single breakpoint in your store's action methods to see exactly who is changing state and when. No more hunting through components to find where state got mutated.
|
|
283
|
+
|
|
284
|
+
```tsx
|
|
285
|
+
class CounterStore extends Store<{ count: number }> {
|
|
286
|
+
increment() {
|
|
287
|
+
// Set a breakpoint here to catch ALL count changes
|
|
288
|
+
this.setState((state) => ({ count: state.count + 1 }));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Why Can't I Destructure Actions?
|
|
294
|
+
|
|
295
|
+
This won't work:
|
|
296
|
+
|
|
297
|
+
```tsx
|
|
298
|
+
const { increment } = useCounterStore(); // ❌ Breaks 'this' binding
|
|
299
|
+
increment(); // Error: cannot read setState of undefined
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
You must use:
|
|
303
|
+
|
|
304
|
+
```tsx
|
|
305
|
+
const store = useCounterStore(); // ✅
|
|
306
|
+
store.increment();
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
**This is a feature, not a bug.** The store is an external dependency — it should look like one. When you see `store.increment()`, it's clear you're calling a method on an external object. If you just saw `increment()`, it would look like a local function, hiding the fact that it's modifying external state.
|
|
310
|
+
|
|
311
|
+
### Why `useStore.Context` Instead of Separate Exports?
|
|
312
|
+
|
|
313
|
+
Instead of:
|
|
314
|
+
|
|
315
|
+
```tsx
|
|
316
|
+
// Two exports to manage
|
|
317
|
+
export const useCounterStore = createStoreHook(CounterStore);
|
|
318
|
+
export const CounterStoreContext = useCounterStore.Context;
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
We have:
|
|
322
|
+
|
|
323
|
+
```tsx
|
|
324
|
+
// One export does it all
|
|
325
|
+
export const useCounterStore = createStoreHook(CounterStore);
|
|
326
|
+
|
|
327
|
+
// Usage
|
|
328
|
+
<useCounterStore.Context>
|
|
329
|
+
<App />
|
|
330
|
+
</useCounterStore.Context>;
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
**Simplicity:** One export per store file. The hook and its context travel together — you can't accidentally import one without having access to the other.
|
|
334
|
+
|
|
335
|
+
### Context `value` vs MockContext `state`
|
|
336
|
+
|
|
337
|
+
Both `Context` and `MockContext` accept props, but they work differently:
|
|
338
|
+
|
|
339
|
+
```tsx
|
|
340
|
+
// Context: value is passed TO getInitialState() for computation
|
|
341
|
+
<useCounterStore.Context value={{ count: 10 }}>
|
|
342
|
+
|
|
343
|
+
// MockContext: state REPLACES getInitialState() entirely
|
|
344
|
+
<useCounterStore.MockContext state={{ count: 10 }}>
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
**Why the distinction?**
|
|
348
|
+
|
|
349
|
+
- `Context.value` — Provides input data for `getInitialState()` to use. Your `getInitialState()` method is still the single source of truth for how state is computed.
|
|
350
|
+
- `MockContext.state` — Bypasses `getInitialState()` completely and sets the state directly. This is only for tests where you need to put the store in a specific state.
|
|
351
|
+
|
|
352
|
+
**Debuggability:** In production code, `getInitialState()` is always called. You can set a breakpoint there to see exactly how initial state is computed. With `MockContext`, the state is set directly for test convenience.
|
|
353
|
+
|
|
354
|
+
### Why `getInitialState()` Method Instead of a Property?
|
|
355
|
+
|
|
356
|
+
Instead of:
|
|
357
|
+
|
|
358
|
+
```tsx
|
|
359
|
+
class CounterStore extends Store<{ count: number }> {
|
|
360
|
+
initialState = { count: 0 }; // Static value
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
We use:
|
|
365
|
+
|
|
366
|
+
```tsx
|
|
367
|
+
class CounterStore extends Store<{ count: number }> {
|
|
368
|
+
protected getInitialState() {
|
|
369
|
+
// Can include logic!
|
|
370
|
+
return { count: 0 };
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
**Flexibility:** A method lets you compute initial state dynamically:
|
|
376
|
+
|
|
377
|
+
```tsx
|
|
378
|
+
protected getInitialState() {
|
|
379
|
+
return {
|
|
380
|
+
count: parseInt(localStorage.getItem('count') ?? '0'),
|
|
381
|
+
timestamp: Date.now(),
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
---
|
|
387
|
+
|
|
233
388
|
**context-scoped-state** — Because not all state needs to be global.
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as C } from "react/jsx-runtime";
|
|
2
|
-
import
|
|
2
|
+
import h, { useSyncExternalStore as H } from "react";
|
|
3
3
|
var m = function(e, r) {
|
|
4
4
|
return m = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(t, n) {
|
|
5
5
|
t.__proto__ = n;
|
|
@@ -7,7 +7,7 @@ var m = function(e, r) {
|
|
|
7
7
|
for (var o in n) Object.prototype.hasOwnProperty.call(n, o) && (t[o] = n[o]);
|
|
8
8
|
}, m(e, r);
|
|
9
9
|
};
|
|
10
|
-
function
|
|
10
|
+
function v(e, r) {
|
|
11
11
|
if (typeof r != "function" && r !== null)
|
|
12
12
|
throw new TypeError("Class extends value " + String(r) + " is not a constructor or null");
|
|
13
13
|
m(e, r);
|
|
@@ -85,8 +85,8 @@ var S = (function() {
|
|
|
85
85
|
if (this._parentage = null, Array.isArray(s))
|
|
86
86
|
try {
|
|
87
87
|
for (var u = w(s), c = u.next(); !c.done; c = u.next()) {
|
|
88
|
-
var
|
|
89
|
-
|
|
88
|
+
var f = c.value;
|
|
89
|
+
f.remove(this);
|
|
90
90
|
}
|
|
91
91
|
} catch (a) {
|
|
92
92
|
r = { error: a };
|
|
@@ -110,8 +110,8 @@ var S = (function() {
|
|
|
110
110
|
if (P) {
|
|
111
111
|
this._finalizers = null;
|
|
112
112
|
try {
|
|
113
|
-
for (var b = w(P),
|
|
114
|
-
var V =
|
|
113
|
+
for (var b = w(P), p = b.next(); !p.done; p = b.next()) {
|
|
114
|
+
var V = p.value;
|
|
115
115
|
try {
|
|
116
116
|
A(V);
|
|
117
117
|
} catch (a) {
|
|
@@ -122,7 +122,7 @@ var S = (function() {
|
|
|
122
122
|
n = { error: a };
|
|
123
123
|
} finally {
|
|
124
124
|
try {
|
|
125
|
-
|
|
125
|
+
p && !p.done && (o = b.return) && o.call(b);
|
|
126
126
|
} finally {
|
|
127
127
|
if (n) throw n.error;
|
|
128
128
|
}
|
|
@@ -169,7 +169,7 @@ function A(e) {
|
|
|
169
169
|
}
|
|
170
170
|
var $ = {
|
|
171
171
|
Promise: void 0
|
|
172
|
-
},
|
|
172
|
+
}, W = {
|
|
173
173
|
setTimeout: function(e, r) {
|
|
174
174
|
for (var t = [], n = 2; n < arguments.length; n++)
|
|
175
175
|
t[n - 2] = arguments[n];
|
|
@@ -180,21 +180,21 @@ var $ = {
|
|
|
180
180
|
},
|
|
181
181
|
delegate: void 0
|
|
182
182
|
};
|
|
183
|
-
function
|
|
184
|
-
|
|
183
|
+
function q(e) {
|
|
184
|
+
W.setTimeout(function() {
|
|
185
185
|
throw e;
|
|
186
186
|
});
|
|
187
187
|
}
|
|
188
|
-
function
|
|
188
|
+
function T() {
|
|
189
189
|
}
|
|
190
190
|
function y(e) {
|
|
191
191
|
e();
|
|
192
192
|
}
|
|
193
193
|
var F = (function(e) {
|
|
194
|
-
|
|
194
|
+
v(r, e);
|
|
195
195
|
function r(t) {
|
|
196
196
|
var n = e.call(this) || this;
|
|
197
|
-
return n.isStopped = !1, t ? (n.destination = t, R(t) && t.add(n)) : n.destination =
|
|
197
|
+
return n.isStopped = !1, t ? (n.destination = t, R(t) && t.add(n)) : n.destination = J, n;
|
|
198
198
|
}
|
|
199
199
|
return r.create = function(t, n, o) {
|
|
200
200
|
return new O(t, n, o);
|
|
@@ -221,7 +221,7 @@ var F = (function(e) {
|
|
|
221
221
|
this.unsubscribe();
|
|
222
222
|
}
|
|
223
223
|
}, r;
|
|
224
|
-
})(S),
|
|
224
|
+
})(S), D = (function() {
|
|
225
225
|
function e(r) {
|
|
226
226
|
this.partialObserver = r;
|
|
227
227
|
}
|
|
@@ -253,42 +253,42 @@ var F = (function(e) {
|
|
|
253
253
|
}
|
|
254
254
|
}, e;
|
|
255
255
|
})(), O = (function(e) {
|
|
256
|
-
|
|
256
|
+
v(r, e);
|
|
257
257
|
function r(t, n, o) {
|
|
258
258
|
var i = e.call(this) || this, s;
|
|
259
259
|
return l(t) || !t ? s = {
|
|
260
260
|
next: t ?? void 0,
|
|
261
261
|
error: n ?? void 0,
|
|
262
262
|
complete: o ?? void 0
|
|
263
|
-
} : s = t, i.destination = new
|
|
263
|
+
} : s = t, i.destination = new D(s), i;
|
|
264
264
|
}
|
|
265
265
|
return r;
|
|
266
266
|
})(F);
|
|
267
267
|
function d(e) {
|
|
268
|
-
|
|
268
|
+
q(e);
|
|
269
269
|
}
|
|
270
|
-
function
|
|
270
|
+
function G(e) {
|
|
271
271
|
throw e;
|
|
272
272
|
}
|
|
273
|
-
var
|
|
273
|
+
var J = {
|
|
274
274
|
closed: !0,
|
|
275
|
-
next:
|
|
276
|
-
error:
|
|
277
|
-
complete:
|
|
278
|
-
},
|
|
275
|
+
next: T,
|
|
276
|
+
error: G,
|
|
277
|
+
complete: T
|
|
278
|
+
}, K = (function() {
|
|
279
279
|
return typeof Symbol == "function" && Symbol.observable || "@@observable";
|
|
280
280
|
})();
|
|
281
|
-
function
|
|
281
|
+
function L(e) {
|
|
282
282
|
return e;
|
|
283
283
|
}
|
|
284
|
-
function
|
|
285
|
-
return e.length === 0 ?
|
|
284
|
+
function Q(e) {
|
|
285
|
+
return e.length === 0 ? L : e.length === 1 ? e[0] : function(t) {
|
|
286
286
|
return e.reduce(function(n, o) {
|
|
287
287
|
return o(n);
|
|
288
288
|
}, t);
|
|
289
289
|
};
|
|
290
290
|
}
|
|
291
|
-
var
|
|
291
|
+
var I = (function() {
|
|
292
292
|
function e(r) {
|
|
293
293
|
r && (this._subscribe = r);
|
|
294
294
|
}
|
|
@@ -326,12 +326,12 @@ var T = (function() {
|
|
|
326
326
|
}, e.prototype._subscribe = function(r) {
|
|
327
327
|
var t;
|
|
328
328
|
return (t = this.source) === null || t === void 0 ? void 0 : t.subscribe(r);
|
|
329
|
-
}, e.prototype[
|
|
329
|
+
}, e.prototype[K] = function() {
|
|
330
330
|
return this;
|
|
331
331
|
}, e.prototype.pipe = function() {
|
|
332
332
|
for (var r = [], t = 0; t < arguments.length; t++)
|
|
333
333
|
r[t] = arguments[t];
|
|
334
|
-
return
|
|
334
|
+
return Q(r)(this);
|
|
335
335
|
}, e.prototype.toPromise = function(r) {
|
|
336
336
|
var t = this;
|
|
337
337
|
return r = k(r), new r(function(n, o) {
|
|
@@ -363,7 +363,7 @@ var z = M(function(e) {
|
|
|
363
363
|
e(this), this.name = "ObjectUnsubscribedError", this.message = "object unsubscribed";
|
|
364
364
|
};
|
|
365
365
|
}), B = (function(e) {
|
|
366
|
-
|
|
366
|
+
v(r, e);
|
|
367
367
|
function r() {
|
|
368
368
|
var t = e.call(this) || this;
|
|
369
369
|
return t.closed = !1, t.currentObservers = null, t.observers = [], t.isStopped = !1, t.hasError = !1, t.thrownError = null, t;
|
|
@@ -385,8 +385,8 @@ var z = M(function(e) {
|
|
|
385
385
|
var c = u.value;
|
|
386
386
|
c.next(t);
|
|
387
387
|
}
|
|
388
|
-
} catch (
|
|
389
|
-
o = { error:
|
|
388
|
+
} catch (f) {
|
|
389
|
+
o = { error: f };
|
|
390
390
|
} finally {
|
|
391
391
|
try {
|
|
392
392
|
u && !u.done && (i = s.return) && i.call(s);
|
|
@@ -436,13 +436,13 @@ var z = M(function(e) {
|
|
|
436
436
|
var n = this, o = n.hasError, i = n.thrownError, s = n.isStopped;
|
|
437
437
|
o ? t.error(i) : s && t.complete();
|
|
438
438
|
}, r.prototype.asObservable = function() {
|
|
439
|
-
var t = new
|
|
439
|
+
var t = new I();
|
|
440
440
|
return t.source = this, t;
|
|
441
441
|
}, r.create = function(t, n) {
|
|
442
442
|
return new U(t, n);
|
|
443
443
|
}, r;
|
|
444
|
-
})(
|
|
445
|
-
|
|
444
|
+
})(I), U = (function(e) {
|
|
445
|
+
v(r, e);
|
|
446
446
|
function r(t, n) {
|
|
447
447
|
var o = e.call(this) || this;
|
|
448
448
|
return o.destination = t, o.source = n, o;
|
|
@@ -461,7 +461,7 @@ var z = M(function(e) {
|
|
|
461
461
|
return (o = (n = this.source) === null || n === void 0 ? void 0 : n.subscribe(t)) !== null && o !== void 0 ? o : Y;
|
|
462
462
|
}, r;
|
|
463
463
|
})(B), N = (function(e) {
|
|
464
|
-
|
|
464
|
+
v(r, e);
|
|
465
465
|
function r(t) {
|
|
466
466
|
var n = e.call(this) || this;
|
|
467
467
|
return n._value = t, n;
|
|
@@ -486,8 +486,10 @@ var z = M(function(e) {
|
|
|
486
486
|
})(B);
|
|
487
487
|
class et {
|
|
488
488
|
_stateSubject;
|
|
489
|
-
constructor() {
|
|
490
|
-
this.
|
|
489
|
+
constructor(r) {
|
|
490
|
+
this._stateSubject = new N(
|
|
491
|
+
this.getInitialState(r)
|
|
492
|
+
);
|
|
491
493
|
}
|
|
492
494
|
getState() {
|
|
493
495
|
return this._stateSubject.value;
|
|
@@ -495,7 +497,6 @@ class et {
|
|
|
495
497
|
state$() {
|
|
496
498
|
return this._stateSubject.asObservable();
|
|
497
499
|
}
|
|
498
|
-
state;
|
|
499
500
|
setState(r) {
|
|
500
501
|
const t = typeof r == "function" ? r(this._stateSubject.value) : r;
|
|
501
502
|
this._stateSubject.next(t);
|
|
@@ -506,10 +507,14 @@ class et {
|
|
|
506
507
|
}
|
|
507
508
|
}
|
|
508
509
|
function nt(e) {
|
|
509
|
-
const r =
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
510
|
+
const r = class extends e {
|
|
511
|
+
state;
|
|
512
|
+
}, t = h.createContext(
|
|
513
|
+
void 0
|
|
514
|
+
);
|
|
515
|
+
function n() {
|
|
516
|
+
const o = h.useContext(t);
|
|
517
|
+
if (!o)
|
|
513
518
|
throw new Error(
|
|
514
519
|
`Store hook used outside of its Context provider.
|
|
515
520
|
|
|
@@ -521,41 +526,42 @@ Then wrap your component with:
|
|
|
521
526
|
<YourComponent />
|
|
522
527
|
</useYourStore.Context>`
|
|
523
528
|
);
|
|
524
|
-
const
|
|
525
|
-
(
|
|
526
|
-
const
|
|
527
|
-
return () =>
|
|
529
|
+
const i = h.useCallback(
|
|
530
|
+
(u) => {
|
|
531
|
+
const c = o.state$().subscribe(u);
|
|
532
|
+
return () => c.unsubscribe();
|
|
528
533
|
},
|
|
529
|
-
[
|
|
530
|
-
),
|
|
531
|
-
|
|
532
|
-
() =>
|
|
533
|
-
() =>
|
|
534
|
+
[o]
|
|
535
|
+
), s = H(
|
|
536
|
+
i,
|
|
537
|
+
() => o.getState(),
|
|
538
|
+
() => o.getState()
|
|
534
539
|
// getServerSnapshot for SSR
|
|
535
540
|
);
|
|
536
|
-
return
|
|
541
|
+
return o.state = s, o;
|
|
537
542
|
}
|
|
538
|
-
return
|
|
539
|
-
children:
|
|
543
|
+
return n.Context = function({
|
|
544
|
+
children: i,
|
|
545
|
+
value: s
|
|
540
546
|
}) {
|
|
541
|
-
const [
|
|
542
|
-
return /* @__PURE__ */ C(
|
|
543
|
-
},
|
|
544
|
-
children:
|
|
545
|
-
state:
|
|
547
|
+
const [u] = h.useState(() => new r(s));
|
|
548
|
+
return /* @__PURE__ */ C(t.Provider, { value: u, children: i });
|
|
549
|
+
}, n.MockContext = function({
|
|
550
|
+
children: i,
|
|
551
|
+
state: s
|
|
546
552
|
}) {
|
|
547
|
-
const
|
|
548
|
-
if (
|
|
549
|
-
return new
|
|
550
|
-
class
|
|
553
|
+
const u = () => {
|
|
554
|
+
if (s === void 0)
|
|
555
|
+
return new r();
|
|
556
|
+
const f = class extends r {
|
|
551
557
|
getInitialState() {
|
|
552
|
-
return
|
|
558
|
+
return s;
|
|
553
559
|
}
|
|
554
|
-
}
|
|
555
|
-
return new
|
|
556
|
-
},
|
|
557
|
-
return /* @__PURE__ */ C(
|
|
558
|
-
},
|
|
560
|
+
};
|
|
561
|
+
return new f();
|
|
562
|
+
}, c = h.useRef(u());
|
|
563
|
+
return /* @__PURE__ */ C(t.Provider, { value: c.current, children: i });
|
|
564
|
+
}, n;
|
|
559
565
|
}
|
|
560
566
|
export {
|
|
561
567
|
et as Store,
|
package/dist/lib/Store.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
declare abstract class Store<T
|
|
2
|
-
protected abstract getInitialState(): T;
|
|
1
|
+
declare abstract class Store<T, C = Partial<T>> {
|
|
2
|
+
protected abstract getInitialState(contextValue?: C): T;
|
|
3
3
|
private readonly _stateSubject;
|
|
4
|
-
constructor();
|
|
4
|
+
constructor(contextValue?: C);
|
|
5
5
|
getState(): T;
|
|
6
6
|
state$(): import('rxjs').Observable<T>;
|
|
7
|
-
state: T;
|
|
8
7
|
protected setState(newState: T | ((currentState: T) => T)): void;
|
|
9
8
|
protected patchState(partialState: Partial<T> | ((currentState: T) => Partial<T>)): void;
|
|
10
9
|
}
|
package/dist/lib/Store.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Store.d.ts","sourceRoot":"","sources":["../../src/lib/Store.ts"],"names":[],"mappings":"AAEA,uBAAe,KAAK,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"Store.d.ts","sourceRoot":"","sources":["../../src/lib/Store.ts"],"names":[],"mappings":"AAEA,uBAAe,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IACpC,SAAS,CAAC,QAAQ,CAAC,eAAe,CAAC,YAAY,CAAC,EAAE,CAAC,GAAG,CAAC;IAEvD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqB;gBAEvC,YAAY,CAAC,EAAE,CAAC;IAM5B,QAAQ,IAAI,CAAC;IAIb,MAAM;IAIN,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC,YAAY,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,IAAI;IAQhE,SAAS,CAAC,UAAU,CAClB,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,YAAY,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,GAC3D,IAAI;CAQR;AAED,OAAO,EAAE,KAAK,EAAE,CAAC"}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { default as React } from 'react';
|
|
2
2
|
import { Store } from './Store';
|
|
3
|
-
declare function createStoreHook<T extends Store<any>>(storeClass: new () => T): {
|
|
4
|
-
(): T
|
|
5
|
-
|
|
3
|
+
declare function createStoreHook<T extends Store<any, any>>(storeClass: new (contextValue?: any) => T): {
|
|
4
|
+
(): T & {
|
|
5
|
+
readonly state: ReturnType<T["getState"]>;
|
|
6
|
+
};
|
|
7
|
+
Context({ children, value, }: {
|
|
6
8
|
children: React.ReactNode;
|
|
9
|
+
value?: T extends Store<any, infer C> ? C : never;
|
|
7
10
|
}): import("react/jsx-runtime").JSX.Element;
|
|
8
11
|
MockContext({ children, state, }: {
|
|
9
12
|
children: React.ReactNode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createStore.d.ts","sourceRoot":"","sources":["../../src/lib/createStore.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA+B,MAAM,OAAO,CAAC;AACpD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC,iBAAS,eAAe,CAAC,CAAC,SAAS,KAAK,CAAC,GAAG,CAAC,
|
|
1
|
+
{"version":3,"file":"createStore.d.ts","sourceRoot":"","sources":["../../src/lib/createStore.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA+B,MAAM,OAAO,CAAC;AACpD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC,iBAAS,eAAe,CAAC,CAAC,SAAS,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,EAChD,UAAU,EAAE,KAAK,YAAY,CAAC,EAAE,GAAG,KAAK,CAAC;;;;kCAyDtC;QACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;QAC1B,KAAK,CAAC,6BAvD2C,CAAC,cAuDzB;KAC1B;sCAWE;QACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;QAC1B,KAAK,CAAC,4BAAY;KACnB;EAwBF;AACD,OAAO,EAAE,eAAe,EAAE,CAAC"}
|