context-scoped-state 0.0.12 → 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 ADDED
@@ -0,0 +1,438 @@
1
+ <div align="center">
2
+ <img src="./logo.svg" alt="context-scoped-state logo" width="120" height="120">
3
+ <h1>context-scoped-state</h1>
4
+ <p><strong>State management that respects component boundaries.</strong></p>
5
+ </div>
6
+
7
+ Unlike global state libraries (Redux, Zustand), `context-scoped-state` keeps your state where it belongs — scoped to the component tree that needs it. Each context provider creates an independent store instance, making your components truly reusable and your tests truly isolated.
8
+
9
+ ## Why Scoped State?
10
+
11
+ Global state is convenient, but it comes with hidden costs:
12
+
13
+ - **Testing nightmares** — State leaks between tests, requiring complex cleanup
14
+ - **Component coupling** — Reusing components means sharing their global state
15
+ - **Implicit dependencies** — Components magically depend on global singletons
16
+
17
+ `context-scoped-state` solves this by leveraging React's Context API the right way. Same API simplicity, but with proper encapsulation.
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install context-scoped-state
23
+ ```
24
+
25
+ ```bash
26
+ yarn add context-scoped-state
27
+ ```
28
+
29
+ ```bash
30
+ pnpm add context-scoped-state
31
+ ```
32
+
33
+ > **Peer Dependencies:** React 18+
34
+
35
+ ## Try it Online
36
+
37
+ [![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/HarshRohila/context-scoped-state/tree/master/examples/playground)
38
+
39
+ ## Quick Start
40
+
41
+ ### 1. Create Your Store (one file, one export)
42
+
43
+ > Wondering why classes? See [API Design Choices](#api-design-choices).
44
+
45
+ ```tsx
46
+ // counterStore.ts
47
+ import { Store, createStoreHook } from 'context-scoped-state';
48
+
49
+ class CounterStore extends Store<{ count: number }> {
50
+ protected getInitialState() {
51
+ return { count: 0 };
52
+ }
53
+
54
+ increment() {
55
+ // Callback-based: receives current state, returns new state
56
+ this.setState((state) => ({ count: state.count + 1 }));
57
+ }
58
+
59
+ decrement() {
60
+ // Direct value: pass the new state directly
61
+ this.setState({ count: this.getState().count - 1 });
62
+ }
63
+ }
64
+
65
+ // This single export is all you need
66
+ export const useCounterStore = createStoreHook(CounterStore);
67
+ ```
68
+
69
+ ### 2. Use in Your App
70
+
71
+ ```tsx
72
+ import { useCounterStore } from './counterStore';
73
+
74
+ function Counter() {
75
+ const counterStore = useCounterStore();
76
+
77
+ return (
78
+ <div>
79
+ <span>{counterStore.state.count}</span>
80
+ <button onClick={() => counterStore.increment()}>+</button>
81
+ <button onClick={() => counterStore.decrement()}>-</button>
82
+ </div>
83
+ );
84
+ }
85
+
86
+ function App() {
87
+ return (
88
+ <useCounterStore.Context>
89
+ <Counter />
90
+ </useCounterStore.Context>
91
+ );
92
+ }
93
+ ```
94
+
95
+ That's it. One hook export gives you the hook and its `.Context` provider. No extra setup needed.
96
+
97
+ ### Partial State Updates with patchState
98
+
99
+ For stores with multiple properties, use `patchState` to update only specific fields:
100
+
101
+ ```tsx
102
+ class UserStore extends Store<{ name: string; age: number; email: string }> {
103
+ protected getInitialState() {
104
+ return { name: '', age: 0, email: '' };
105
+ }
106
+
107
+ updateName(name: string) {
108
+ // Only updates name, preserves age and email
109
+ this.patchState({ name });
110
+ }
111
+
112
+ incrementAge() {
113
+ // Callback-based: receives current state, returns partial update
114
+ this.patchState((state) => ({ age: state.age + 1 }));
115
+ }
116
+ }
117
+ ```
118
+
119
+ - `setState` — Replaces the entire state
120
+ - `patchState` — Merges partial updates into existing state
121
+
122
+ ## Examples
123
+
124
+ ### Independent Nested Stores
125
+
126
+ Each `Context` creates a completely independent store instance. Perfect for reusable widget patterns:
127
+
128
+ ```tsx
129
+ function PlayerScore() {
130
+ const store = useScoreStore();
131
+ return <span>Score: {store.state.score}</span>;
132
+ }
133
+
134
+ function Game() {
135
+ return (
136
+ <div>
137
+ {/* Player 1 has their own score */}
138
+ <useScoreStore.Context>
139
+ <h2>Player 1</h2>
140
+ <PlayerScore />
141
+ </useScoreStore.Context>
142
+
143
+ {/* Player 2 has their own score */}
144
+ <useScoreStore.Context>
145
+ <h2>Player 2</h2>
146
+ <PlayerScore />
147
+ </useScoreStore.Context>
148
+ </div>
149
+ );
150
+ }
151
+ ```
152
+
153
+ Both players have completely independent state — no configuration needed.
154
+
155
+ ### Testing with MockContext
156
+
157
+ Test components in any state without complex setup:
158
+
159
+ ```tsx
160
+ import { render, screen } from '@testing-library/react';
161
+
162
+ test('shows warning when balance is low', () => {
163
+ render(
164
+ <useAccountStore.MockContext state={{ balance: 5, currency: 'USD' }}>
165
+ <AccountStatus />
166
+ </useAccountStore.MockContext>,
167
+ );
168
+
169
+ expect(screen.getByText('Low balance warning')).toBeInTheDocument();
170
+ });
171
+
172
+ test('shows normal status when balance is healthy', () => {
173
+ render(
174
+ <useAccountStore.MockContext state={{ balance: 1000, currency: 'USD' }}>
175
+ <AccountStatus />
176
+ </useAccountStore.MockContext>,
177
+ );
178
+
179
+ expect(screen.queryByText('Low balance warning')).not.toBeInTheDocument();
180
+ });
181
+ ```
182
+
183
+ No mocking libraries. No global state cleanup. Just render with the state you need.
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
+
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
+
256
+ ## Why context-scoped-state Over Other Libraries?
257
+
258
+ | Feature | context-scoped-state | Redux | Zustand |
259
+ | ---------------------- | -------------------- | ---------------------------- | -------------- |
260
+ | **Scoped by default** | Yes | No | No |
261
+ | **Multiple instances** | Automatic | Manual wiring | Manual wiring |
262
+ | **Test isolation** | Built-in MockContext | Requires setup | Requires reset |
263
+ | **Boilerplate** | Low | High | Low |
264
+ | **Type safety** | Full | Requires setup | Good |
265
+ | **Learning curve** | Just classes | Actions, reducers, selectors | Simple |
266
+
267
+ ### The Core Difference
268
+
269
+ **Global state libraries** make you fight against React's component model. You end up with:
270
+
271
+ - Selector functions to prevent re-renders
272
+ - Complex test fixtures to reset global state
273
+ - Workarounds for component reusability
274
+
275
+ **context-scoped-state** works _with_ React:
276
+
277
+ - State lives in the component tree, just like React intended
278
+ - Each provider = new instance, automatically
279
+ - Testing is just rendering with different props
280
+
281
+ ### When to Use What
282
+
283
+ **Use context-scoped-state when:**
284
+
285
+ - Building reusable components with internal state
286
+ - You want test isolation without extra setup
287
+ - State naturally belongs to a subtree, not the whole app
288
+
289
+ **Need global state?** Just place the Context at your app root — same API, app-wide access.
290
+
291
+ ### Why Not Just Use useState or useReducer?
292
+
293
+ **vs useState:**
294
+
295
+ - `useState` binds state directly to the component — poor separation of concerns and hard to test since you can't easily set a component to a specific state
296
+ - Lifting state up with `useState` requires refactoring components and passing props; with `context-scoped-state`, just move the Context wrapper up the tree
297
+
298
+ **vs useReducer:**
299
+
300
+ - No action types, switch statements, or dispatch boilerplate
301
+ - Just call methods directly: `store.increment()` instead of `dispatch({ type: 'INCREMENT' })`
302
+ - Full TypeScript autocomplete for your actions
303
+
304
+ ---
305
+
306
+ ## API Design Choices
307
+
308
+ These design decisions are intentional trade-offs that optimize for debuggability, clarity, and simplicity.
309
+
310
+ ### Why Classes for Stores?
311
+
312
+ 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.
313
+
314
+ **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.
315
+
316
+ ```tsx
317
+ class CounterStore extends Store<{ count: number }> {
318
+ increment() {
319
+ // Set a breakpoint here to catch ALL count changes
320
+ this.setState((state) => ({ count: state.count + 1 }));
321
+ }
322
+ }
323
+ ```
324
+
325
+ ### Why Can't I Destructure Actions?
326
+
327
+ This won't work:
328
+
329
+ ```tsx
330
+ const { increment } = useCounterStore(); // ❌ Breaks 'this' binding
331
+ increment(); // Error: cannot read setState of undefined
332
+ ```
333
+
334
+ You must use:
335
+
336
+ ```tsx
337
+ const store = useCounterStore(); // ✅
338
+ store.increment();
339
+ ```
340
+
341
+ **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.
342
+
343
+ ### Why `useStore.Context` Instead of Separate Exports?
344
+
345
+ Instead of:
346
+
347
+ ```tsx
348
+ // Two exports to manage
349
+ export const useCounterStore = createStoreHook(CounterStore);
350
+ export const CounterStoreContext = useCounterStore.Context;
351
+ ```
352
+
353
+ We have:
354
+
355
+ ```tsx
356
+ // One export does it all
357
+ export const useCounterStore = createStoreHook(CounterStore);
358
+
359
+ // Usage
360
+ <useCounterStore.Context>
361
+ <App />
362
+ </useCounterStore.Context>;
363
+ ```
364
+
365
+ **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.
366
+
367
+ ### Context `value` vs MockContext `state`
368
+
369
+ Both `Context` and `MockContext` accept props, but they work differently:
370
+
371
+ ```tsx
372
+ // Context: value is passed TO getInitialState() for computation
373
+ <useCounterStore.Context value={{ count: 10 }}>
374
+
375
+ // MockContext: state REPLACES getInitialState() entirely
376
+ <useCounterStore.MockContext state={{ count: 10 }}>
377
+ ```
378
+
379
+ **Why the distinction?**
380
+
381
+ - `Context.value` — Provides input data for `getInitialState()` to use. Your `getInitialState()` method is still the single source of truth for how state is computed.
382
+ - `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.
383
+
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.
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
+
404
+ ### Why `getInitialState()` Method Instead of a Property?
405
+
406
+ Instead of:
407
+
408
+ ```tsx
409
+ class CounterStore extends Store<{ count: number }> {
410
+ initialState = { count: 0 }; // Static value
411
+ }
412
+ ```
413
+
414
+ We use:
415
+
416
+ ```tsx
417
+ class CounterStore extends Store<{ count: number }> {
418
+ protected getInitialState() {
419
+ // Can include logic!
420
+ return { count: 0 };
421
+ }
422
+ }
423
+ ```
424
+
425
+ **Flexibility:** A method lets you compute initial state dynamically:
426
+
427
+ ```tsx
428
+ protected getInitialState() {
429
+ return {
430
+ count: parseInt(localStorage.getItem('count') ?? '0'),
431
+ timestamp: Date.now(),
432
+ };
433
+ }
434
+ ```
435
+
436
+ ---
437
+
438
+ **context-scoped-state** — Because not all state needs to be global.
package/dist/index.js CHANGED
@@ -1,22 +1,22 @@
1
1
  import { jsx as C } from "react/jsx-runtime";
2
- import h, { useSyncExternalStore as H } from "react";
3
- var m = function(e, r) {
4
- return m = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(t, n) {
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
- }, m(e, r);
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
- m(e, r);
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 w(e) {
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 x(e, r) {
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 E(e, r, t) {
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 _ = M(function(e) {
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 g(e, r) {
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 S = (function() {
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 = w(s), c = u.next(); !c.done; c = u.next()) {
88
- var f = c.value;
89
- f.remove(this);
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 j = this.initialTeardown;
103
- if (l(j))
102
+ var b = this.initialTeardown;
103
+ if (l(b))
104
104
  try {
105
- j();
105
+ b();
106
106
  } catch (a) {
107
- i = a instanceof _ ? a.errors : [a];
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 b = w(P), p = b.next(); !p.done; p = b.next()) {
114
- var V = p.value;
113
+ for (var d = E(P), h = d.next(); !h.done; h = d.next()) {
114
+ var H = h.value;
115
115
  try {
116
- A(V);
116
+ A(H);
117
117
  } catch (a) {
118
- i = i ?? [], a instanceof _ ? i = E(E([], x(i)), x(a.errors)) : i.push(a);
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
- p && !p.done && (o = b.return) && o.call(b);
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 _(i);
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) && g(t, r);
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 && g(t, r), r instanceof e && r._removeParent(this);
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 = S.EMPTY;
163
+ })(), Y = _.EMPTY;
164
164
  function R(e) {
165
- return e instanceof S || e && "closed" in e && l(e.remove) && l(e.add) && l(e.unsubscribe);
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
- }, W = {
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, E([e, r], x(t)));
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 q(e) {
184
- W.setTimeout(function() {
183
+ function D(e) {
184
+ q.setTimeout(function() {
185
185
  throw e;
186
186
  });
187
187
  }
188
188
  function T() {
189
189
  }
190
- function y(e) {
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 = J, n;
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 O(t, n, o);
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
- })(S), D = (function() {
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
- d(n);
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
- d(n);
242
+ y(n);
243
243
  }
244
244
  else
245
- d(r);
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
- d(t);
252
+ y(t);
253
253
  }
254
254
  }, e;
255
- })(), O = (function(e) {
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 D(s), i;
263
+ } : s = t, i.destination = new G(s), i;
264
264
  }
265
265
  return r;
266
266
  })(F);
267
- function d(e) {
268
- q(e);
267
+ function y(e) {
268
+ D(e);
269
269
  }
270
- function G(e) {
270
+ function J(e) {
271
271
  throw e;
272
272
  }
273
- var J = {
273
+ var K = {
274
274
  closed: !0,
275
275
  next: T,
276
- error: G,
276
+ error: J,
277
277
  complete: T
278
- }, K = (function() {
278
+ }, L = (function() {
279
279
  return typeof Symbol == "function" && Symbol.observable || "@@observable";
280
280
  })();
281
- function L(e) {
281
+ function Q(e) {
282
282
  return e;
283
283
  }
284
- function Q(e) {
285
- return e.length === 0 ? L : e.length === 1 ? e[0] : function(t) {
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 = Z(r) ? r : new O(r, t, n);
300
- return y(function() {
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 O({
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[K] = function() {
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 Q(r)(this);
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 ?? $.Promise) !== null && r !== void 0 ? r : Promise;
353
+ return (r = e ?? W.Promise) !== null && r !== void 0 ? r : Promise;
354
354
  }
355
- function X(e) {
355
+ function Z(e) {
356
356
  return e && l(e.next) && l(e.error) && l(e.complete);
357
357
  }
358
- function Z(e) {
359
- return e && e instanceof F || X(e) && R(e);
358
+ function z(e) {
359
+ return e && e instanceof F || Z(e) && R(e);
360
360
  }
361
- var z = M(function(e) {
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 z();
376
+ throw new V();
377
377
  }, r.prototype.next = function(t) {
378
378
  var n = this;
379
- y(function() {
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 = w(n.currentObservers), u = s.next(); !u.done; u = s.next()) {
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 (f) {
389
- o = { error: f };
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
- y(function() {
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
- y(function() {
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 S(function() {
433
- n.currentObservers = null, g(u, t);
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 = h.createContext(
512
+ }, t = f.createContext(
513
513
  void 0
514
514
  );
515
- function n() {
516
- const o = h.useContext(t);
517
- if (!o)
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
- const i = h.useCallback(
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 n.Context = function({
544
- children: i,
545
- value: s
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 [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
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 u = () => {
554
- if (s === void 0)
561
+ const c = () => {
562
+ if (u === void 0)
555
563
  return new r();
556
- const f = class extends r {
564
+ const b = class extends r {
557
565
  getInitialState() {
558
- return s;
566
+ return u;
559
567
  }
560
568
  };
561
- return new f();
562
- }, c = h.useRef(u());
563
- return /* @__PURE__ */ C(t.Provider, { value: c.current, children: i });
564
- }, n;
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;;;;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"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-scoped-state",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "repository": {