valdres-solid 0.1.0-beta.0

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/dist/index.js ADDED
@@ -0,0 +1,174 @@
1
+ // src/createAtom.ts
2
+ import { from } from "solid-js";
3
+ import { isPromiseLike } from "valdres";
4
+
5
+ // src/useStore.ts
6
+ import { useContext } from "solid-js";
7
+
8
+ // src/lib/storeContext.ts
9
+ import { createContext } from "solid-js";
10
+ var StoreContext = createContext();
11
+
12
+ // src/useStore.ts
13
+ var useStore = (id) => {
14
+ const ctx = useContext(StoreContext);
15
+ if (!ctx) {
16
+ throw new Error("No valdres store found. Wrap your component tree with <ValdresProvider>.");
17
+ }
18
+ if (id) {
19
+ const store = ctx.stores[id];
20
+ if (!store) {
21
+ throw new Error(`No store with id "${id}" found in ancestor chain`);
22
+ }
23
+ return store;
24
+ }
25
+ return ctx.current;
26
+ };
27
+
28
+ // src/createAtom.ts
29
+ var createAtom = (atom, store) => {
30
+ const currentStore = store || useStore();
31
+ const initial = currentStore.get(atom);
32
+ if (isPromiseLike(initial)) {
33
+ throw initial;
34
+ }
35
+ const value = from((set) => {
36
+ set(() => initial);
37
+ return currentStore.sub(atom, () => {
38
+ const next = currentStore.get(atom);
39
+ if (!isPromiseLike(next)) {
40
+ set(() => next);
41
+ }
42
+ }, false);
43
+ });
44
+ const setter = (val) => currentStore.set(atom, val);
45
+ return [value, setter];
46
+ };
47
+ // src/createResetAtom.ts
48
+ var createResetAtom = (atom, store) => {
49
+ const currentStore = store || useStore();
50
+ return () => currentStore.reset(atom);
51
+ };
52
+ // src/createSetAtom.ts
53
+ var createSetAtom = (atom, store) => {
54
+ const currentStore = store || useStore();
55
+ return (value) => currentStore.set(atom, value);
56
+ };
57
+ // src/createTransaction.ts
58
+ var createTransaction = (store) => {
59
+ const currentStore = store || useStore();
60
+ return (callback) => currentStore.txn(callback);
61
+ };
62
+ // src/createValue.ts
63
+ import { from as from2 } from "solid-js";
64
+ import { isPromiseLike as isPromiseLike2 } from "valdres";
65
+ var createValue = (state, store) => {
66
+ const currentStore = store || useStore();
67
+ const initial = currentStore.get(state);
68
+ if (isPromiseLike2(initial)) {
69
+ throw initial;
70
+ }
71
+ return from2((set) => {
72
+ set(() => initial);
73
+ return currentStore.sub(state, () => {
74
+ const next = currentStore.get(state);
75
+ if (!isPromiseLike2(next)) {
76
+ set(() => next);
77
+ }
78
+ }, false);
79
+ });
80
+ };
81
+ // src/ValdresProvider.ts
82
+ import { useContext as useContext2 } from "solid-js";
83
+ import { store as createStore } from "valdres";
84
+
85
+ // src/lib/hydrate.ts
86
+ var hydrate = (set, state) => {
87
+ for (const [atom, value] of state) {
88
+ set(atom, value);
89
+ }
90
+ };
91
+
92
+ // src/ValdresProvider.ts
93
+ var ValdresProvider = (props) => {
94
+ const parentCtx = useContext2(StoreContext);
95
+ let store = props.store;
96
+ if (store) {
97
+ if (!store.data.batchUpdates) {
98
+ console.warn("valdres-solid: The store passed to ValdresProvider was not created " + "with { batchUpdates: true }. Sequential store.set() calls " + "will trigger intermediate selector evaluations. Consider " + "using store({ batchUpdates: true }) for optimal performance.");
99
+ }
100
+ } else {
101
+ store = createStore({ batchUpdates: true });
102
+ }
103
+ if (parentCtx) {
104
+ if (store.data.id in parentCtx.stores) {
105
+ throw new Error(`store with id ${store.data.id} is already defined further up the tree`);
106
+ }
107
+ }
108
+ if (props.initialize) {
109
+ store.txn((txn) => {
110
+ const pairs = props.initialize(txn);
111
+ if (pairs) {
112
+ hydrate(txn.set, pairs);
113
+ }
114
+ });
115
+ }
116
+ return StoreContext.Provider({
117
+ value: {
118
+ current: store,
119
+ stores: {
120
+ ...parentCtx?.stores ?? {},
121
+ [store.data.id]: store
122
+ }
123
+ },
124
+ get children() {
125
+ return props.children;
126
+ }
127
+ });
128
+ };
129
+ // src/ValdresScope.ts
130
+ import { useContext as useContext3, onCleanup, createUniqueId } from "solid-js";
131
+ var ValdresScope = (props) => {
132
+ const parentCtx = useContext3(StoreContext);
133
+ if (!parentCtx) {
134
+ throw new Error("No valdres store found. ValdresScope must be nested under a <ValdresProvider> or another <ValdresScope>.");
135
+ }
136
+ const scopeId = props.scopeId || createUniqueId();
137
+ const scopeCreated = !parentCtx.current.data.scopes?.has(scopeId);
138
+ const scopedStore = parentCtx.current.scope(scopeId);
139
+ if (props.initialize) {
140
+ scopedStore.txn((txn) => {
141
+ const pairs = props.initialize(txn);
142
+ if (pairs) {
143
+ hydrate(txn.set, pairs);
144
+ }
145
+ });
146
+ }
147
+ onCleanup(() => {
148
+ scopedStore?.detach?.(scopeCreated);
149
+ });
150
+ return StoreContext.Provider({
151
+ value: {
152
+ current: scopedStore,
153
+ stores: {
154
+ ...parentCtx.stores,
155
+ [parentCtx.current.data.id]: parentCtx.current,
156
+ [scopedStore.data.id]: scopedStore
157
+ }
158
+ },
159
+ get children() {
160
+ return props.children;
161
+ }
162
+ });
163
+ };
164
+ export {
165
+ useStore,
166
+ createValue,
167
+ createTransaction,
168
+ createSetAtom,
169
+ createResetAtom,
170
+ createAtom,
171
+ ValdresScope,
172
+ ValdresProvider,
173
+ StoreContext
174
+ };
@@ -0,0 +1,9 @@
1
+ import { type JSX } from "solid-js";
2
+ import { type Store } from "valdres";
3
+ import type { InitializeCallback } from "./types/InitializeCallback";
4
+ export interface ValdresProviderProps {
5
+ store?: Store;
6
+ initialize?: InitializeCallback;
7
+ children: JSX.Element;
8
+ }
9
+ export declare const ValdresProvider: (props: ValdresProviderProps) => JSX.Element;
@@ -0,0 +1,8 @@
1
+ import { type JSX } from "solid-js";
2
+ import type { InitializeCallback } from "./types/InitializeCallback";
3
+ export interface ValdresScopeProps {
4
+ scopeId?: string;
5
+ initialize?: InitializeCallback;
6
+ children: JSX.Element;
7
+ }
8
+ export declare const ValdresScope: (props: ValdresScopeProps) => JSX.Element;
@@ -0,0 +1,5 @@
1
+ import { type Accessor } from "solid-js";
2
+ import { type Atom, type SetAtomValue, type Store } from "valdres";
3
+ type SetterFn<V> = (value: SetAtomValue<V>) => void;
4
+ export declare const createAtom: <V>(atom: Atom<V>, store?: Store) => [Accessor<V>, SetterFn<V>];
5
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { Atom, Store } from "valdres";
2
+ export declare const createResetAtom: <V>(atom: Atom<V>, store?: Store) => (() => void);
@@ -0,0 +1,2 @@
1
+ import type { Atom, SetAtomValue, Store } from "valdres";
2
+ export declare const createSetAtom: <V>(atom: Atom<V>, store?: Store) => (value: SetAtomValue<V>) => void;
@@ -0,0 +1,2 @@
1
+ import type { Store, Transaction } from "valdres";
2
+ export declare const createTransaction: (store?: Store) => (callback: (txn: Transaction) => any) => void;
@@ -0,0 +1,3 @@
1
+ import { type Accessor } from "solid-js";
2
+ import { type State, type Store } from "valdres";
3
+ export declare const createValue: <Value extends any = any, Args extends [any, ...any[]] = [any, ...any[]]>(state: State<Value, Args>, store?: Store) => Accessor<Value>;
@@ -0,0 +1,12 @@
1
+ export { createAtom } from "./createAtom";
2
+ export { createResetAtom } from "./createResetAtom";
3
+ export { createSetAtom } from "./createSetAtom";
4
+ export { useStore } from "./useStore";
5
+ export { createTransaction } from "./createTransaction";
6
+ export { createValue } from "./createValue";
7
+ export { ValdresProvider } from "./ValdresProvider";
8
+ export type { ValdresProviderProps } from "./ValdresProvider";
9
+ export { ValdresScope } from "./ValdresScope";
10
+ export type { ValdresScopeProps } from "./ValdresScope";
11
+ export { StoreContext } from "./lib/storeContext";
12
+ export type { ValdresContext } from "./lib/storeContext";
@@ -0,0 +1,2 @@
1
+ import type { Atom, SyncSetAtom } from "valdres";
2
+ export declare const hydrate: (set: SyncSetAtom, state: [Atom, any][]) => void;
@@ -0,0 +1,6 @@
1
+ import type { Store } from "valdres";
2
+ export interface ValdresContext {
3
+ current: Store;
4
+ stores: Record<string, Store>;
5
+ }
6
+ export declare const StoreContext: import("solid-js").Context<ValdresContext | undefined>;
@@ -0,0 +1,4 @@
1
+ import type { Atom, Transaction } from "valdres";
2
+ type AtomPair = [Atom<any>, any];
3
+ export type InitializeCallback = (txn: Transaction) => void | AtomPair[];
4
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { Store } from "valdres";
2
+ export declare const useStore: (id?: string) => Store;
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "valdres-solid",
3
+ "version": "0.1.0-beta.0",
4
+ "license": "MIT",
5
+ "author": {
6
+ "name": "Eigil Sagafos"
7
+ },
8
+ "homepage": "https://valdres.dev",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/eigilsagafos/valdres.git"
12
+ },
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "import": "./dist/index.js",
17
+ "types": "./dist/types/index.d.ts"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "peerDependencies": {
24
+ "valdres": "workspace:^",
25
+ "solid-js": ">=1.8"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public",
29
+ "registry": "https://registry.npmjs.org/"
30
+ }
31
+ }