context-scoped-state 0.0.13 → 0.0.14
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 +50 -0
- package/dist/index.js +112 -104
- package/dist/lib/createStore.d.ts +3 -0
- package/dist/lib/createStore.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -221,6 +221,38 @@ function App() {
|
|
|
221
221
|
}
|
|
222
222
|
```
|
|
223
223
|
|
|
224
|
+
### Optional Store Access
|
|
225
|
+
|
|
226
|
+
Sometimes a component needs to work both inside and outside a store provider. Instead of throwing an error, `useStore.optional()` returns `undefined` when no provider is found:
|
|
227
|
+
|
|
228
|
+
```tsx
|
|
229
|
+
function UserName({ userId }: { userId: string }) {
|
|
230
|
+
const store = useUserStore.optional();
|
|
231
|
+
|
|
232
|
+
if (!store) {
|
|
233
|
+
return <span>{userId}</span>;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return <span>{store.state.users.get(userId) ?? userId}</span>;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Works inside a provider — shows resolved user name
|
|
240
|
+
function Dashboard() {
|
|
241
|
+
return (
|
|
242
|
+
<useUserStore.Context>
|
|
243
|
+
<UserName userId="42" />
|
|
244
|
+
</useUserStore.Context>
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Also works without a provider — falls back to showing the raw ID
|
|
249
|
+
function SimpleList() {
|
|
250
|
+
return <UserName userId="42" />;
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
This is useful for shared components that are rendered in different parts of your app, where a provider may or may not be present.
|
|
255
|
+
|
|
224
256
|
## Why context-scoped-state Over Other Libraries?
|
|
225
257
|
|
|
226
258
|
| Feature | context-scoped-state | Redux | Zustand |
|
|
@@ -351,6 +383,24 @@ Both `Context` and `MockContext` accept props, but they work differently:
|
|
|
351
383
|
|
|
352
384
|
**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
385
|
|
|
386
|
+
### `useStore()` vs `useStore.optional()`
|
|
387
|
+
|
|
388
|
+
By default, calling the hook outside a provider throws an error:
|
|
389
|
+
|
|
390
|
+
```tsx
|
|
391
|
+
const store = useCounterStore(); // Throws if no <useCounterStore.Context> above
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
This is intentional — it catches missing providers early during development. Most components _require_ the store to function, so a loud failure is better than a silent `undefined`.
|
|
395
|
+
|
|
396
|
+
For components that genuinely need to work with or without a provider, use `.optional()`:
|
|
397
|
+
|
|
398
|
+
```tsx
|
|
399
|
+
const store = useCounterStore.optional(); // Returns undefined if no provider
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
**Why two variants instead of a config flag?** Type safety. `useStore()` returns `TWithState` — you can use the store directly without null checks. `useStore.optional()` returns `TWithState | undefined` — TypeScript forces you to handle the `undefined` case. This keeps the common path clean while making the optional path explicitly safe.
|
|
403
|
+
|
|
354
404
|
### Why `getInitialState()` Method Instead of a Property?
|
|
355
405
|
|
|
356
406
|
Instead of:
|
package/dist/index.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { jsx as C } from "react/jsx-runtime";
|
|
2
|
-
import
|
|
3
|
-
var
|
|
4
|
-
return
|
|
2
|
+
import f, { useSyncExternalStore as $ } from "react";
|
|
3
|
+
var w = function(e, r) {
|
|
4
|
+
return w = 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
|
+
}, w(e, r);
|
|
9
9
|
};
|
|
10
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
|
+
w(e, r);
|
|
14
14
|
function t() {
|
|
15
15
|
this.constructor = e;
|
|
16
16
|
}
|
|
17
17
|
e.prototype = r === null ? Object.create(r) : (t.prototype = r.prototype, new t());
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function E(e) {
|
|
20
20
|
var r = typeof Symbol == "function" && Symbol.iterator, t = r && e[r], n = 0;
|
|
21
21
|
if (t) return t.call(e);
|
|
22
22
|
if (e && typeof e.length == "number") return {
|
|
@@ -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 g(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 x(e, r) {
|
|
|
43
43
|
}
|
|
44
44
|
return i;
|
|
45
45
|
}
|
|
46
|
-
function
|
|
46
|
+
function x(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));
|
|
@@ -57,7 +57,7 @@ function M(e) {
|
|
|
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 = 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) {
|
|
@@ -66,13 +66,13 @@ var _ = M(function(e) {
|
|
|
66
66
|
`) : "", this.name = "UnsubscriptionError", this.errors = t;
|
|
67
67
|
};
|
|
68
68
|
});
|
|
69
|
-
function
|
|
69
|
+
function O(e, r) {
|
|
70
70
|
if (e) {
|
|
71
71
|
var t = e.indexOf(r);
|
|
72
72
|
0 <= t && e.splice(t, 1);
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
|
-
var
|
|
75
|
+
var _ = (function() {
|
|
76
76
|
function e(r) {
|
|
77
77
|
this.initialTeardown = r, this.closed = !1, this._parentage = null, this._finalizers = null;
|
|
78
78
|
}
|
|
@@ -84,9 +84,9 @@ var S = (function() {
|
|
|
84
84
|
if (s)
|
|
85
85
|
if (this._parentage = null, Array.isArray(s))
|
|
86
86
|
try {
|
|
87
|
-
for (var u =
|
|
88
|
-
var
|
|
89
|
-
|
|
87
|
+
for (var u = E(s), c = u.next(); !c.done; c = u.next()) {
|
|
88
|
+
var p = c.value;
|
|
89
|
+
p.remove(this);
|
|
90
90
|
}
|
|
91
91
|
} catch (a) {
|
|
92
92
|
r = { error: a };
|
|
@@ -99,37 +99,37 @@ var S = (function() {
|
|
|
99
99
|
}
|
|
100
100
|
else
|
|
101
101
|
s.remove(this);
|
|
102
|
-
var
|
|
103
|
-
if (l(
|
|
102
|
+
var b = this.initialTeardown;
|
|
103
|
+
if (l(b))
|
|
104
104
|
try {
|
|
105
|
-
|
|
105
|
+
b();
|
|
106
106
|
} catch (a) {
|
|
107
|
-
i = a instanceof
|
|
107
|
+
i = a instanceof m ? a.errors : [a];
|
|
108
108
|
}
|
|
109
109
|
var P = this._finalizers;
|
|
110
110
|
if (P) {
|
|
111
111
|
this._finalizers = null;
|
|
112
112
|
try {
|
|
113
|
-
for (var
|
|
114
|
-
var
|
|
113
|
+
for (var d = E(P), h = d.next(); !h.done; h = d.next()) {
|
|
114
|
+
var H = h.value;
|
|
115
115
|
try {
|
|
116
|
-
A(
|
|
116
|
+
A(H);
|
|
117
117
|
} catch (a) {
|
|
118
|
-
i = i ?? [], a instanceof
|
|
118
|
+
i = i ?? [], a instanceof m ? i = x(x([], g(i)), g(a.errors)) : i.push(a);
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
121
|
} catch (a) {
|
|
122
122
|
n = { error: a };
|
|
123
123
|
} finally {
|
|
124
124
|
try {
|
|
125
|
-
|
|
125
|
+
h && !h.done && (o = d.return) && o.call(d);
|
|
126
126
|
} finally {
|
|
127
127
|
if (n) throw n.error;
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
if (i)
|
|
132
|
-
throw new
|
|
132
|
+
throw new m(i);
|
|
133
133
|
}
|
|
134
134
|
}, e.prototype.add = function(r) {
|
|
135
135
|
var t;
|
|
@@ -152,52 +152,52 @@ var S = (function() {
|
|
|
152
152
|
this._parentage = Array.isArray(t) ? (t.push(r), t) : t ? [t, r] : r;
|
|
153
153
|
}, e.prototype._removeParent = function(r) {
|
|
154
154
|
var t = this._parentage;
|
|
155
|
-
t === r ? this._parentage = null : Array.isArray(t) &&
|
|
155
|
+
t === r ? this._parentage = null : Array.isArray(t) && O(t, r);
|
|
156
156
|
}, e.prototype.remove = function(r) {
|
|
157
157
|
var t = this._finalizers;
|
|
158
|
-
t &&
|
|
158
|
+
t && O(t, r), r instanceof e && r._removeParent(this);
|
|
159
159
|
}, e.EMPTY = (function() {
|
|
160
160
|
var r = new e();
|
|
161
161
|
return r.closed = !0, r;
|
|
162
162
|
})(), e;
|
|
163
|
-
})(), Y =
|
|
163
|
+
})(), Y = _.EMPTY;
|
|
164
164
|
function R(e) {
|
|
165
|
-
return e instanceof
|
|
165
|
+
return e instanceof _ || e && "closed" in e && l(e.remove) && l(e.add) && l(e.unsubscribe);
|
|
166
166
|
}
|
|
167
167
|
function A(e) {
|
|
168
168
|
l(e) ? e() : e.unsubscribe();
|
|
169
169
|
}
|
|
170
|
-
var
|
|
170
|
+
var W = {
|
|
171
171
|
Promise: void 0
|
|
172
|
-
},
|
|
172
|
+
}, q = {
|
|
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, x([e, r], g(t)));
|
|
177
177
|
},
|
|
178
178
|
clearTimeout: function(e) {
|
|
179
179
|
return clearTimeout(e);
|
|
180
180
|
},
|
|
181
181
|
delegate: void 0
|
|
182
182
|
};
|
|
183
|
-
function
|
|
184
|
-
|
|
183
|
+
function D(e) {
|
|
184
|
+
q.setTimeout(function() {
|
|
185
185
|
throw e;
|
|
186
186
|
});
|
|
187
187
|
}
|
|
188
188
|
function T() {
|
|
189
189
|
}
|
|
190
|
-
function
|
|
190
|
+
function S(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 = K, n;
|
|
198
198
|
}
|
|
199
199
|
return r.create = function(t, n, o) {
|
|
200
|
-
return new
|
|
200
|
+
return new j(t, n, o);
|
|
201
201
|
}, r.prototype.next = function(t) {
|
|
202
202
|
this.isStopped || this._next(t);
|
|
203
203
|
}, r.prototype.error = function(t) {
|
|
@@ -221,7 +221,7 @@ var F = (function(e) {
|
|
|
221
221
|
this.unsubscribe();
|
|
222
222
|
}
|
|
223
223
|
}, r;
|
|
224
|
-
})(
|
|
224
|
+
})(_), G = (function() {
|
|
225
225
|
function e(r) {
|
|
226
226
|
this.partialObserver = r;
|
|
227
227
|
}
|
|
@@ -231,7 +231,7 @@ var F = (function(e) {
|
|
|
231
231
|
try {
|
|
232
232
|
t.next(r);
|
|
233
233
|
} catch (n) {
|
|
234
|
-
|
|
234
|
+
y(n);
|
|
235
235
|
}
|
|
236
236
|
}, e.prototype.error = function(r) {
|
|
237
237
|
var t = this.partialObserver;
|
|
@@ -239,20 +239,20 @@ var F = (function(e) {
|
|
|
239
239
|
try {
|
|
240
240
|
t.error(r);
|
|
241
241
|
} catch (n) {
|
|
242
|
-
|
|
242
|
+
y(n);
|
|
243
243
|
}
|
|
244
244
|
else
|
|
245
|
-
|
|
245
|
+
y(r);
|
|
246
246
|
}, e.prototype.complete = function() {
|
|
247
247
|
var r = this.partialObserver;
|
|
248
248
|
if (r.complete)
|
|
249
249
|
try {
|
|
250
250
|
r.complete();
|
|
251
251
|
} catch (t) {
|
|
252
|
-
|
|
252
|
+
y(t);
|
|
253
253
|
}
|
|
254
254
|
}, e;
|
|
255
|
-
})(),
|
|
255
|
+
})(), j = (function(e) {
|
|
256
256
|
v(r, e);
|
|
257
257
|
function r(t, n, o) {
|
|
258
258
|
var i = e.call(this) || this, s;
|
|
@@ -260,29 +260,29 @@ var F = (function(e) {
|
|
|
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 G(s), i;
|
|
264
264
|
}
|
|
265
265
|
return r;
|
|
266
266
|
})(F);
|
|
267
|
-
function
|
|
268
|
-
|
|
267
|
+
function y(e) {
|
|
268
|
+
D(e);
|
|
269
269
|
}
|
|
270
|
-
function
|
|
270
|
+
function J(e) {
|
|
271
271
|
throw e;
|
|
272
272
|
}
|
|
273
|
-
var
|
|
273
|
+
var K = {
|
|
274
274
|
closed: !0,
|
|
275
275
|
next: T,
|
|
276
|
-
error:
|
|
276
|
+
error: J,
|
|
277
277
|
complete: T
|
|
278
|
-
},
|
|
278
|
+
}, L = (function() {
|
|
279
279
|
return typeof Symbol == "function" && Symbol.observable || "@@observable";
|
|
280
280
|
})();
|
|
281
|
-
function
|
|
281
|
+
function Q(e) {
|
|
282
282
|
return e;
|
|
283
283
|
}
|
|
284
|
-
function
|
|
285
|
-
return e.length === 0 ?
|
|
284
|
+
function X(e) {
|
|
285
|
+
return e.length === 0 ? Q : e.length === 1 ? e[0] : function(t) {
|
|
286
286
|
return e.reduce(function(n, o) {
|
|
287
287
|
return o(n);
|
|
288
288
|
}, t);
|
|
@@ -296,8 +296,8 @@ var I = (function() {
|
|
|
296
296
|
var t = new e();
|
|
297
297
|
return t.source = this, t.operator = r, t;
|
|
298
298
|
}, e.prototype.subscribe = function(r, t, n) {
|
|
299
|
-
var o = this, i =
|
|
300
|
-
return
|
|
299
|
+
var o = this, i = z(r) ? r : new j(r, t, n);
|
|
300
|
+
return S(function() {
|
|
301
301
|
var s = o, u = s.operator, c = s.source;
|
|
302
302
|
i.add(u ? u.call(i, c) : c ? o._subscribe(i) : o._trySubscribe(i));
|
|
303
303
|
}), i;
|
|
@@ -310,7 +310,7 @@ var I = (function() {
|
|
|
310
310
|
}, e.prototype.forEach = function(r, t) {
|
|
311
311
|
var n = this;
|
|
312
312
|
return t = k(t), new t(function(o, i) {
|
|
313
|
-
var s = new
|
|
313
|
+
var s = new j({
|
|
314
314
|
next: function(u) {
|
|
315
315
|
try {
|
|
316
316
|
r(u);
|
|
@@ -326,12 +326,12 @@ var I = (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[L] = 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 X(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) {
|
|
@@ -350,15 +350,15 @@ var I = (function() {
|
|
|
350
350
|
})();
|
|
351
351
|
function k(e) {
|
|
352
352
|
var r;
|
|
353
|
-
return (r = e ??
|
|
353
|
+
return (r = e ?? W.Promise) !== null && r !== void 0 ? r : Promise;
|
|
354
354
|
}
|
|
355
|
-
function
|
|
355
|
+
function Z(e) {
|
|
356
356
|
return e && l(e.next) && l(e.error) && l(e.complete);
|
|
357
357
|
}
|
|
358
|
-
function
|
|
359
|
-
return e && e instanceof F ||
|
|
358
|
+
function z(e) {
|
|
359
|
+
return e && e instanceof F || Z(e) && R(e);
|
|
360
360
|
}
|
|
361
|
-
var
|
|
361
|
+
var V = M(function(e) {
|
|
362
362
|
return function() {
|
|
363
363
|
e(this), this.name = "ObjectUnsubscribedError", this.message = "object unsubscribed";
|
|
364
364
|
};
|
|
@@ -373,20 +373,20 @@ var z = M(function(e) {
|
|
|
373
373
|
return n.operator = t, n;
|
|
374
374
|
}, r.prototype._throwIfClosed = function() {
|
|
375
375
|
if (this.closed)
|
|
376
|
-
throw new
|
|
376
|
+
throw new V();
|
|
377
377
|
}, r.prototype.next = function(t) {
|
|
378
378
|
var n = this;
|
|
379
|
-
|
|
379
|
+
S(function() {
|
|
380
380
|
var o, i;
|
|
381
381
|
if (n._throwIfClosed(), !n.isStopped) {
|
|
382
382
|
n.currentObservers || (n.currentObservers = Array.from(n.observers));
|
|
383
383
|
try {
|
|
384
|
-
for (var s =
|
|
384
|
+
for (var s = E(n.currentObservers), u = s.next(); !u.done; u = s.next()) {
|
|
385
385
|
var c = u.value;
|
|
386
386
|
c.next(t);
|
|
387
387
|
}
|
|
388
|
-
} catch (
|
|
389
|
-
o = { error:
|
|
388
|
+
} catch (p) {
|
|
389
|
+
o = { error: p };
|
|
390
390
|
} finally {
|
|
391
391
|
try {
|
|
392
392
|
u && !u.done && (i = s.return) && i.call(s);
|
|
@@ -398,7 +398,7 @@ var z = M(function(e) {
|
|
|
398
398
|
});
|
|
399
399
|
}, r.prototype.error = function(t) {
|
|
400
400
|
var n = this;
|
|
401
|
-
|
|
401
|
+
S(function() {
|
|
402
402
|
if (n._throwIfClosed(), !n.isStopped) {
|
|
403
403
|
n.hasError = n.isStopped = !0, n.thrownError = t;
|
|
404
404
|
for (var o = n.observers; o.length; )
|
|
@@ -407,7 +407,7 @@ var z = M(function(e) {
|
|
|
407
407
|
});
|
|
408
408
|
}, r.prototype.complete = function() {
|
|
409
409
|
var t = this;
|
|
410
|
-
|
|
410
|
+
S(function() {
|
|
411
411
|
if (t._throwIfClosed(), !t.isStopped) {
|
|
412
412
|
t.isStopped = !0;
|
|
413
413
|
for (var n = t.observers; n.length; )
|
|
@@ -429,8 +429,8 @@ var z = M(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 ? Y : (this.currentObservers = null, u.push(t), new
|
|
433
|
-
n.currentObservers = null,
|
|
432
|
+
return i || s ? Y : (this.currentObservers = null, u.push(t), new _(function() {
|
|
433
|
+
n.currentObservers = null, O(u, t);
|
|
434
434
|
}));
|
|
435
435
|
}, r.prototype._checkFinalizedStatuses = function(t) {
|
|
436
436
|
var n = this, o = n.hasError, i = n.thrownError, s = n.isStopped;
|
|
@@ -509,12 +509,29 @@ class et {
|
|
|
509
509
|
function nt(e) {
|
|
510
510
|
const r = class extends e {
|
|
511
511
|
state;
|
|
512
|
-
}, t =
|
|
512
|
+
}, t = f.createContext(
|
|
513
513
|
void 0
|
|
514
514
|
);
|
|
515
|
-
function n() {
|
|
516
|
-
const
|
|
517
|
-
|
|
515
|
+
function n(i) {
|
|
516
|
+
const s = f.useCallback(
|
|
517
|
+
(u) => {
|
|
518
|
+
if (!i) return () => {
|
|
519
|
+
};
|
|
520
|
+
const c = i.state$().subscribe(u);
|
|
521
|
+
return () => c.unsubscribe();
|
|
522
|
+
},
|
|
523
|
+
[i]
|
|
524
|
+
);
|
|
525
|
+
if ($(
|
|
526
|
+
s,
|
|
527
|
+
() => i?.getState(),
|
|
528
|
+
() => i?.getState()
|
|
529
|
+
), !!i)
|
|
530
|
+
return i.state = i.getState(), i;
|
|
531
|
+
}
|
|
532
|
+
function o() {
|
|
533
|
+
const i = f.useContext(t), s = n(i);
|
|
534
|
+
if (!s)
|
|
518
535
|
throw new Error(
|
|
519
536
|
`Store hook used outside of its Context provider.
|
|
520
537
|
|
|
@@ -526,42 +543,33 @@ Then wrap your component with:
|
|
|
526
543
|
<YourComponent />
|
|
527
544
|
</useYourStore.Context>`
|
|
528
545
|
);
|
|
529
|
-
|
|
530
|
-
(u) => {
|
|
531
|
-
const c = o.state$().subscribe(u);
|
|
532
|
-
return () => c.unsubscribe();
|
|
533
|
-
},
|
|
534
|
-
[o]
|
|
535
|
-
), s = H(
|
|
536
|
-
i,
|
|
537
|
-
() => o.getState(),
|
|
538
|
-
() => o.getState()
|
|
539
|
-
// getServerSnapshot for SSR
|
|
540
|
-
);
|
|
541
|
-
return o.state = s, o;
|
|
546
|
+
return s;
|
|
542
547
|
}
|
|
543
|
-
return
|
|
544
|
-
|
|
545
|
-
|
|
548
|
+
return o.optional = function() {
|
|
549
|
+
const s = f.useContext(t);
|
|
550
|
+
return n(s);
|
|
551
|
+
}, o.Context = function({
|
|
552
|
+
children: s,
|
|
553
|
+
value: u
|
|
546
554
|
}) {
|
|
547
|
-
const [
|
|
548
|
-
return /* @__PURE__ */ C(t.Provider, { value:
|
|
549
|
-
},
|
|
550
|
-
children:
|
|
551
|
-
state:
|
|
555
|
+
const [c] = f.useState(() => new r(u));
|
|
556
|
+
return /* @__PURE__ */ C(t.Provider, { value: c, children: s });
|
|
557
|
+
}, o.MockContext = function({
|
|
558
|
+
children: s,
|
|
559
|
+
state: u
|
|
552
560
|
}) {
|
|
553
|
-
const
|
|
554
|
-
if (
|
|
561
|
+
const c = () => {
|
|
562
|
+
if (u === void 0)
|
|
555
563
|
return new r();
|
|
556
|
-
const
|
|
564
|
+
const b = class extends r {
|
|
557
565
|
getInitialState() {
|
|
558
|
-
return
|
|
566
|
+
return u;
|
|
559
567
|
}
|
|
560
568
|
};
|
|
561
|
-
return new
|
|
562
|
-
},
|
|
563
|
-
return /* @__PURE__ */ C(t.Provider, { value:
|
|
564
|
-
},
|
|
569
|
+
return new b();
|
|
570
|
+
}, p = f.useRef(c());
|
|
571
|
+
return /* @__PURE__ */ C(t.Provider, { value: p.current, children: s });
|
|
572
|
+
}, o;
|
|
565
573
|
}
|
|
566
574
|
export {
|
|
567
575
|
et as Store,
|
|
@@ -4,6 +4,9 @@ declare function createStoreHook<T extends Store<any, any>>(storeClass: new (con
|
|
|
4
4
|
(): T & {
|
|
5
5
|
readonly state: ReturnType<T["getState"]>;
|
|
6
6
|
};
|
|
7
|
+
optional(): (T & {
|
|
8
|
+
readonly state: ReturnType<T["getState"]>;
|
|
9
|
+
}) | undefined;
|
|
7
10
|
Context({ children, value, }: {
|
|
8
11
|
children: React.ReactNode;
|
|
9
12
|
value?: T extends Store<any, infer C> ? C : never;
|
|
@@ -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,EAAE,GAAG,CAAC,EAChD,UAAU,EAAE,KAAK,YAAY,CAAC,EAAE,GAAG,KAAK,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;;;;gBAgEQ;;SAAa,SAAS;kCAQpE;QACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;QAC1B,KAAK,CAAC,6BAtE2C,CAAC,cAsEzB;KAC1B;sCAWE;QACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;QAC1B,KAAK,CAAC,4BAAY;KACnB;EAwBF;AACD,OAAO,EAAE,eAAe,EAAE,CAAC"}
|