@pyreon/store 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present Vit Bokisch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # @pyreon/store
2
+
3
+ Global reactive state management built on `@pyreon/reactivity` signals. Composition API returning structured `StoreApi<T>`.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ bun add @pyreon/store
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```ts
14
+ import { defineStore, signal, computed } from "@pyreon/store"
15
+
16
+ const useCounter = defineStore("counter", () => {
17
+ const count = signal(0)
18
+ const doubled = computed(() => count() * 2)
19
+ const increment = () => count.update((n) => n + 1)
20
+ return { count, doubled, increment }
21
+ })
22
+
23
+ // Destructure what you need:
24
+ const { store, patch, subscribe, reset, dispose } = useCounter()
25
+ store.count() // 0
26
+ store.increment()
27
+ store.doubled() // 2
28
+ patch({ count: 10 }) // batch-update
29
+ ```
30
+
31
+ Stores are singletons. The setup function runs once per store ID; subsequent calls return the cached instance.
32
+
33
+ ## API
34
+
35
+ ### `defineStore(id, setup)`
36
+
37
+ Define a store with a unique string ID and a setup function. Returns a hook that produces a `StoreApi<T>`:
38
+
39
+ | Property | Description |
40
+ | --- | --- |
41
+ | `store` | The user-defined state, computed values, and actions |
42
+ | `id` | Store identifier |
43
+ | `state` | Read-only snapshot of all signal values |
44
+ | `patch(obj \| fn)` | Batch-update signals (object or function form) |
45
+ | `subscribe(cb, opts?)` | Listen to state mutations. `{ immediate: true }` fires instantly |
46
+ | `onAction(cb)` | Intercept action calls (sync + async). Returns unsubscribe |
47
+ | `reset()` | Reset all signals to initial values |
48
+ | `dispose()` | Teardown: unsubscribe all, remove from registry |
49
+
50
+ ### `addStorePlugin(plugin)`
51
+
52
+ Register a global plugin. Plugins receive the `StoreApi` when a store is created.
53
+
54
+ ### `resetStore(id)`
55
+
56
+ Destroy a store by ID. The next call to `useStore()` will re-run the setup function.
57
+
58
+ ### `resetAllStores()`
59
+
60
+ Destroy all stores. Useful for testing and SSR isolation.
61
+
62
+ ### `setStoreRegistryProvider(fn)`
63
+
64
+ Override the store registry provider for concurrent SSR. Each request can get its own isolated registry via `AsyncLocalStorage`.
65
+
66
+ ```ts
67
+ import { AsyncLocalStorage } from "node:async_hooks"
68
+ import { setStoreRegistryProvider } from "@pyreon/store"
69
+
70
+ const als = new AsyncLocalStorage<Map<string, unknown>>()
71
+ setStoreRegistryProvider(() => als.getStore() ?? new Map())
72
+ ```
73
+
74
+ ## Re-exports
75
+
76
+ The following are re-exported from `@pyreon/reactivity` for convenience:
77
+
78
+ - `signal`, `computed`, `effect`, `batch`
79
+ - `Signal` (type)