context-scoped-state 0.0.8 → 0.0.10
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 +28 -1
- package/dist/index.js +30 -25
- package/dist/lib/Store.d.ts +2 -1
- package/dist/lib/Store.d.ts.map +1 -1
- package/dist/lib/createStore.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -50,10 +50,12 @@ class CounterStore extends Store<{ count: number }> {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
increment() {
|
|
53
|
-
|
|
53
|
+
// Callback-based: receives current state, returns new state
|
|
54
|
+
this.setState(state => ({ count: state.count + 1 }));
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
decrement() {
|
|
58
|
+
// Direct value: pass the new state directly
|
|
57
59
|
this.setState({ count: this.getState().count - 1 });
|
|
58
60
|
}
|
|
59
61
|
}
|
|
@@ -90,6 +92,31 @@ function App() {
|
|
|
90
92
|
|
|
91
93
|
That's it. One hook export gives you the hook and its `.Context` provider. No extra setup needed.
|
|
92
94
|
|
|
95
|
+
### Partial State Updates with patchState
|
|
96
|
+
|
|
97
|
+
For stores with multiple properties, use `patchState` to update only specific fields:
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
class UserStore extends Store<{ name: string; age: number; email: string }> {
|
|
101
|
+
protected getInitialState() {
|
|
102
|
+
return { name: '', age: 0, email: '' };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
updateName(name: string) {
|
|
106
|
+
// Only updates name, preserves age and email
|
|
107
|
+
this.patchState({ name });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
incrementAge() {
|
|
111
|
+
// Callback-based: receives current state, returns partial update
|
|
112
|
+
this.patchState(state => ({ age: state.age + 1 }));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
- `setState` — Replaces the entire state
|
|
118
|
+
- `patchState` — Merges partial updates into existing state
|
|
119
|
+
|
|
93
120
|
## Examples
|
|
94
121
|
|
|
95
122
|
### Independent Nested Stores
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { jsx as C } from "react/jsx-runtime";
|
|
2
2
|
import b, { useSyncExternalStore as H } from "react";
|
|
3
|
-
var
|
|
4
|
-
return
|
|
3
|
+
var m = function(e, r) {
|
|
4
|
+
return m = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(t, n) {
|
|
5
5
|
t.__proto__ = n;
|
|
6
6
|
} || function(t, n) {
|
|
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
10
|
function p(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);
|
|
14
14
|
function t() {
|
|
15
15
|
this.constructor = e;
|
|
16
16
|
}
|
|
@@ -26,7 +26,7 @@ function w(e) {
|
|
|
26
26
|
};
|
|
27
27
|
throw new TypeError(r ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
28
28
|
}
|
|
29
|
-
function
|
|
29
|
+
function x(e, r) {
|
|
30
30
|
var t = typeof Symbol == "function" && e[Symbol.iterator];
|
|
31
31
|
if (!t) return e;
|
|
32
32
|
var n = t.call(e), o, i = [], s;
|
|
@@ -43,7 +43,7 @@ function E(e, r) {
|
|
|
43
43
|
}
|
|
44
44
|
return i;
|
|
45
45
|
}
|
|
46
|
-
function
|
|
46
|
+
function E(e, r, t) {
|
|
47
47
|
if (t || arguments.length === 2) for (var n = 0, o = r.length, i; n < o; n++)
|
|
48
48
|
(i || !(n in r)) && (i || (i = Array.prototype.slice.call(r, 0, n)), i[n] = r[n]);
|
|
49
49
|
return e.concat(i || Array.prototype.slice.call(r));
|
|
@@ -51,13 +51,13 @@ function x(e, r, t) {
|
|
|
51
51
|
function l(e) {
|
|
52
52
|
return typeof e == "function";
|
|
53
53
|
}
|
|
54
|
-
function
|
|
54
|
+
function M(e) {
|
|
55
55
|
var r = function(n) {
|
|
56
56
|
Error.call(n), n.stack = new Error().stack;
|
|
57
57
|
}, t = e(r);
|
|
58
58
|
return t.prototype = Object.create(Error.prototype), t.prototype.constructor = t, t;
|
|
59
59
|
}
|
|
60
|
-
var
|
|
60
|
+
var _ = M(function(e) {
|
|
61
61
|
return function(t) {
|
|
62
62
|
e(this), this.message = t ? t.length + ` errors occurred during unsubscription:
|
|
63
63
|
` + t.map(function(n, o) {
|
|
@@ -104,7 +104,7 @@ var S = (function() {
|
|
|
104
104
|
try {
|
|
105
105
|
j();
|
|
106
106
|
} catch (a) {
|
|
107
|
-
i = a instanceof
|
|
107
|
+
i = a instanceof _ ? a.errors : [a];
|
|
108
108
|
}
|
|
109
109
|
var P = this._finalizers;
|
|
110
110
|
if (P) {
|
|
@@ -115,7 +115,7 @@ var S = (function() {
|
|
|
115
115
|
try {
|
|
116
116
|
A(V);
|
|
117
117
|
} catch (a) {
|
|
118
|
-
i = i ?? [], a instanceof
|
|
118
|
+
i = i ?? [], a instanceof _ ? i = E(E([], x(i)), x(a.errors)) : i.push(a);
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
} catch (a) {
|
|
@@ -129,7 +129,7 @@ var S = (function() {
|
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
if (i)
|
|
132
|
-
throw new
|
|
132
|
+
throw new _(i);
|
|
133
133
|
}
|
|
134
134
|
}, e.prototype.add = function(r) {
|
|
135
135
|
var t;
|
|
@@ -160,8 +160,8 @@ var S = (function() {
|
|
|
160
160
|
var r = new e();
|
|
161
161
|
return r.closed = !0, r;
|
|
162
162
|
})(), e;
|
|
163
|
-
})(),
|
|
164
|
-
function
|
|
163
|
+
})(), Y = S.EMPTY;
|
|
164
|
+
function R(e) {
|
|
165
165
|
return e instanceof S || e && "closed" in e && l(e.remove) && l(e.add) && l(e.unsubscribe);
|
|
166
166
|
}
|
|
167
167
|
function A(e) {
|
|
@@ -173,7 +173,7 @@ var $ = {
|
|
|
173
173
|
setTimeout: function(e, r) {
|
|
174
174
|
for (var t = [], n = 2; n < arguments.length; n++)
|
|
175
175
|
t[n - 2] = arguments[n];
|
|
176
|
-
return setTimeout.apply(void 0,
|
|
176
|
+
return setTimeout.apply(void 0, E([e, r], x(t)));
|
|
177
177
|
},
|
|
178
178
|
clearTimeout: function(e) {
|
|
179
179
|
return clearTimeout(e);
|
|
@@ -194,7 +194,7 @@ var F = (function(e) {
|
|
|
194
194
|
p(r, e);
|
|
195
195
|
function r(t) {
|
|
196
196
|
var n = e.call(this) || this;
|
|
197
|
-
return n.isStopped = !1, t ? (n.destination = t,
|
|
197
|
+
return n.isStopped = !1, t ? (n.destination = t, R(t) && t.add(n)) : n.destination = K, n;
|
|
198
198
|
}
|
|
199
199
|
return r.create = function(t, n, o) {
|
|
200
200
|
return new O(t, n, o);
|
|
@@ -356,9 +356,9 @@ function X(e) {
|
|
|
356
356
|
return e && l(e.next) && l(e.error) && l(e.complete);
|
|
357
357
|
}
|
|
358
358
|
function Z(e) {
|
|
359
|
-
return e && e instanceof F || X(e) &&
|
|
359
|
+
return e && e instanceof F || X(e) && R(e);
|
|
360
360
|
}
|
|
361
|
-
var z =
|
|
361
|
+
var z = M(function(e) {
|
|
362
362
|
return function() {
|
|
363
363
|
e(this), this.name = "ObjectUnsubscribedError", this.message = "object unsubscribed";
|
|
364
364
|
};
|
|
@@ -369,7 +369,7 @@ var z = U(function(e) {
|
|
|
369
369
|
return t.closed = !1, t.currentObservers = null, t.observers = [], t.isStopped = !1, t.hasError = !1, t.thrownError = null, t;
|
|
370
370
|
}
|
|
371
371
|
return r.prototype.lift = function(t) {
|
|
372
|
-
var n = new
|
|
372
|
+
var n = new U(this, this);
|
|
373
373
|
return n.operator = t, n;
|
|
374
374
|
}, r.prototype._throwIfClosed = function() {
|
|
375
375
|
if (this.closed)
|
|
@@ -429,7 +429,7 @@ var z = U(function(e) {
|
|
|
429
429
|
return this._throwIfClosed(), this._checkFinalizedStatuses(t), this._innerSubscribe(t);
|
|
430
430
|
}, r.prototype._innerSubscribe = function(t) {
|
|
431
431
|
var n = this, o = this, i = o.hasError, s = o.isStopped, u = o.observers;
|
|
432
|
-
return i || s ?
|
|
432
|
+
return i || s ? Y : (this.currentObservers = null, u.push(t), new S(function() {
|
|
433
433
|
n.currentObservers = null, g(u, t);
|
|
434
434
|
}));
|
|
435
435
|
}, r.prototype._checkFinalizedStatuses = function(t) {
|
|
@@ -439,9 +439,9 @@ var z = U(function(e) {
|
|
|
439
439
|
var t = new T();
|
|
440
440
|
return t.source = this, t;
|
|
441
441
|
}, r.create = function(t, n) {
|
|
442
|
-
return new
|
|
442
|
+
return new U(t, n);
|
|
443
443
|
}, r;
|
|
444
|
-
})(T),
|
|
444
|
+
})(T), U = (function(e) {
|
|
445
445
|
p(r, e);
|
|
446
446
|
function r(t, n) {
|
|
447
447
|
var o = e.call(this) || this;
|
|
@@ -458,7 +458,7 @@ var z = U(function(e) {
|
|
|
458
458
|
(n = (t = this.destination) === null || t === void 0 ? void 0 : t.complete) === null || n === void 0 || n.call(t);
|
|
459
459
|
}, r.prototype._subscribe = function(t) {
|
|
460
460
|
var n, o;
|
|
461
|
-
return (o = (n = this.source) === null || n === void 0 ? void 0 : n.subscribe(t)) !== null && o !== void 0 ? o :
|
|
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
|
p(r, e);
|
|
@@ -497,7 +497,12 @@ class et {
|
|
|
497
497
|
}
|
|
498
498
|
state;
|
|
499
499
|
setState(r) {
|
|
500
|
-
this._stateSubject.
|
|
500
|
+
const t = typeof r == "function" ? r(this._stateSubject.value) : r;
|
|
501
|
+
this._stateSubject.next(t);
|
|
502
|
+
}
|
|
503
|
+
patchState(r) {
|
|
504
|
+
const t = this._stateSubject.value, n = typeof r == "function" ? r(t) : r;
|
|
505
|
+
this._stateSubject.next({ ...t, ...n });
|
|
501
506
|
}
|
|
502
507
|
}
|
|
503
508
|
function nt(e) {
|
|
@@ -530,8 +535,8 @@ Then wrap your component with:
|
|
|
530
535
|
return t.Context = function({
|
|
531
536
|
children: o
|
|
532
537
|
}) {
|
|
533
|
-
const i = b.
|
|
534
|
-
return /* @__PURE__ */ C(r.Provider, { value: i
|
|
538
|
+
const [i] = b.useState(() => new e());
|
|
539
|
+
return /* @__PURE__ */ C(r.Provider, { value: i, children: o });
|
|
535
540
|
}, t.MockContext = function({
|
|
536
541
|
children: o,
|
|
537
542
|
state: i
|
package/dist/lib/Store.d.ts
CHANGED
|
@@ -5,7 +5,8 @@ declare abstract class Store<T> {
|
|
|
5
5
|
getState(): T;
|
|
6
6
|
state$(): import('rxjs').Observable<T>;
|
|
7
7
|
state: T;
|
|
8
|
-
protected setState(newState: T): void;
|
|
8
|
+
protected setState(newState: T | ((currentState: T) => T)): void;
|
|
9
|
+
protected patchState(partialState: Partial<T> | ((currentState: T) => Partial<T>)): void;
|
|
9
10
|
}
|
|
10
11
|
export { Store };
|
|
11
12
|
//# sourceMappingURL=Store.d.ts.map
|
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;IACpB,SAAS,CAAC,QAAQ,CAAC,eAAe,IAAI,CAAC;IAEvC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqB;;IAOnD,QAAQ,IAAI,CAAC;IAIb,MAAM;IAIC,KAAK,EAAE,CAAC,CAAC;IAEhB,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,GAAG,IAAI;
|
|
1
|
+
{"version":3,"file":"Store.d.ts","sourceRoot":"","sources":["../../src/lib/Store.ts"],"names":[],"mappings":"AAEA,uBAAe,KAAK,CAAC,CAAC;IACpB,SAAS,CAAC,QAAQ,CAAC,eAAe,IAAI,CAAC;IAEvC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAqB;;IAOnD,QAAQ,IAAI,CAAC;IAIb,MAAM;IAIC,KAAK,EAAE,CAAC,CAAC;IAEhB,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 +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,EAAE,UAAU,EAAE,UAAU,CAAC;QAG/C,CAAC;2BA+BnB;QACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;KAC3B;
|
|
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,EAAE,UAAU,EAAE,UAAU,CAAC;QAG/C,CAAC;2BA+BnB;QACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;KAC3B;sCAWE;QACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;QAC1B,KAAK,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;KACnC;EAuBF;AACD,OAAO,EAAE,eAAe,EAAE,CAAC"}
|