valdres-vue 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,177 @@
1
+ // src/useAtom.ts
2
+ import { customRef, onScopeDispose } from "vue";
3
+ import { isPromiseLike } from "valdres";
4
+
5
+ // src/useStore.ts
6
+ import { inject } from "vue";
7
+
8
+ // src/lib/storeKey.ts
9
+ var ValdresKey = Symbol("valdres");
10
+
11
+ // src/useStore.ts
12
+ var useStore = (id) => {
13
+ const ctx = inject(ValdresKey);
14
+ if (!ctx)
15
+ throw new Error("No valdres store provided. Install createValdres() as a plugin or wrap in a ValdresScope.");
16
+ if (id) {
17
+ const store = ctx.stores[id];
18
+ if (!store)
19
+ throw new Error(`No store with id "${id}" found in ancestor chain`);
20
+ return store;
21
+ }
22
+ return ctx.current;
23
+ };
24
+
25
+ // src/useAtom.ts
26
+ var useAtom = (atom, store) => {
27
+ const currentStore = store || useStore();
28
+ const initial = currentStore.get(atom);
29
+ if (isPromiseLike(initial)) {
30
+ throw initial;
31
+ }
32
+ let triggerRef;
33
+ const ref = customRef((track, trigger) => {
34
+ triggerRef = trigger;
35
+ return {
36
+ get() {
37
+ track();
38
+ return currentStore.get(atom);
39
+ },
40
+ set(value) {
41
+ currentStore.set(atom, value);
42
+ }
43
+ };
44
+ });
45
+ const unsub = currentStore.sub(atom, () => {
46
+ triggerRef();
47
+ }, false);
48
+ onScopeDispose(() => {
49
+ unsub();
50
+ });
51
+ return ref;
52
+ };
53
+ // src/useResetAtom.ts
54
+ var useResetAtom = (atom, store) => {
55
+ const selectedStore = store || useStore();
56
+ return () => selectedStore.reset(atom);
57
+ };
58
+ // src/useSetAtom.ts
59
+ var useSetAtom = (atom, store) => {
60
+ const selectedStore = store || useStore();
61
+ return (value) => selectedStore.set(atom, value);
62
+ };
63
+ // src/useTransaction.ts
64
+ var useTransaction = (store) => {
65
+ const selectedStore = store || useStore();
66
+ return (callback) => selectedStore.txn(callback);
67
+ };
68
+ // src/useValue.ts
69
+ import { shallowRef, onScopeDispose as onScopeDispose2 } from "vue";
70
+ import { isPromiseLike as isPromiseLike2 } from "valdres";
71
+ var useValue = (state, store) => {
72
+ const currentStore = store || useStore();
73
+ const initial = currentStore.get(state);
74
+ if (isPromiseLike2(initial)) {
75
+ throw initial;
76
+ }
77
+ const value = shallowRef(initial);
78
+ const unsub = currentStore.sub(state, () => {
79
+ const newValue = currentStore.get(state);
80
+ if (!isPromiseLike2(newValue)) {
81
+ value.value = newValue;
82
+ }
83
+ }, false);
84
+ onScopeDispose2(() => {
85
+ unsub();
86
+ });
87
+ return value;
88
+ };
89
+ // src/ValdresPlugin.ts
90
+ import { store as createStore } from "valdres";
91
+
92
+ // src/lib/hydrate.ts
93
+ var hydrate = (set, state) => {
94
+ for (const [atom, value] of state) {
95
+ set(atom, value);
96
+ }
97
+ };
98
+
99
+ // src/ValdresPlugin.ts
100
+ var createValdres = (options = {}) => ({
101
+ install(app) {
102
+ let store = options.store;
103
+ if (store) {
104
+ if (!store.data.batchUpdates) {
105
+ console.warn("valdres-vue: The store passed to createValdres() was not created " + "with { batchUpdates: true }. Sequential store.set() calls " + "will trigger intermediate selector evaluations. Consider " + "using store({ batchUpdates: true }) for optimal performance.");
106
+ }
107
+ } else {
108
+ store = createStore({ batchUpdates: true });
109
+ }
110
+ if (options.initialize) {
111
+ store.txn((txn) => {
112
+ const pairs = options.initialize(txn);
113
+ if (pairs) {
114
+ hydrate(txn.set, pairs);
115
+ }
116
+ });
117
+ }
118
+ app.provide(ValdresKey, {
119
+ current: store,
120
+ stores: { [store.data.id]: store }
121
+ });
122
+ }
123
+ });
124
+ // src/ValdresScope.ts
125
+ import { defineComponent, inject as inject2, provide, onScopeDispose as onScopeDispose3 } from "vue";
126
+ var generateId = () => (Math.random() + 1).toString(36).substring(7);
127
+ var ValdresScope = defineComponent({
128
+ name: "ValdresScope",
129
+ props: {
130
+ scopeId: {
131
+ type: String,
132
+ default: () => generateId()
133
+ },
134
+ initialize: {
135
+ type: Function
136
+ }
137
+ },
138
+ setup(props, { slots }) {
139
+ const parentCtx = inject2(ValdresKey);
140
+ if (!parentCtx) {
141
+ throw new Error("No valdres store provided. ValdresScope must be nested under createValdres() or another ValdresScope.");
142
+ }
143
+ const scopeCreated = !parentCtx.current.data.scopes?.has(props.scopeId);
144
+ const scopedStore = parentCtx.current.scope(props.scopeId);
145
+ if (props.initialize) {
146
+ scopedStore.txn((txn) => {
147
+ const pairs = props.initialize(txn);
148
+ if (pairs) {
149
+ hydrate(txn.set, pairs);
150
+ }
151
+ });
152
+ }
153
+ provide(ValdresKey, {
154
+ current: scopedStore,
155
+ stores: {
156
+ ...parentCtx.stores,
157
+ [parentCtx.current.data.id]: parentCtx.current,
158
+ [scopedStore.data.id]: scopedStore
159
+ }
160
+ });
161
+ onScopeDispose3(() => {
162
+ scopedStore?.detach?.(scopeCreated);
163
+ });
164
+ return () => slots.default?.();
165
+ }
166
+ });
167
+ export {
168
+ useValue,
169
+ useTransaction,
170
+ useStore,
171
+ useSetAtom,
172
+ useResetAtom,
173
+ useAtom,
174
+ createValdres,
175
+ ValdresScope,
176
+ ValdresKey
177
+ };
@@ -0,0 +1,8 @@
1
+ import type { Plugin } from "vue";
2
+ import { type Store } from "valdres";
3
+ import type { InitializeCallback } from "./types/InitializeCallback";
4
+ export interface ValdresPluginOptions {
5
+ store?: Store;
6
+ initialize?: InitializeCallback;
7
+ }
8
+ export declare const createValdres: (options?: ValdresPluginOptions) => Plugin;
@@ -0,0 +1,23 @@
1
+ import { type PropType } from "vue";
2
+ import type { InitializeCallback } from "./types/InitializeCallback";
3
+ export declare const ValdresScope: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
4
+ scopeId: {
5
+ type: StringConstructor;
6
+ default: () => string;
7
+ };
8
+ initialize: {
9
+ type: PropType<InitializeCallback>;
10
+ };
11
+ }>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
12
+ [key: string]: any;
13
+ }>[] | undefined, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
14
+ scopeId: {
15
+ type: StringConstructor;
16
+ default: () => string;
17
+ };
18
+ initialize: {
19
+ type: PropType<InitializeCallback>;
20
+ };
21
+ }>> & Readonly<{}>, {
22
+ scopeId: string;
23
+ }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
@@ -0,0 +1,11 @@
1
+ export { useAtom } from "./useAtom";
2
+ export { useResetAtom } from "./useResetAtom";
3
+ export { useSetAtom } from "./useSetAtom";
4
+ export { useStore } from "./useStore";
5
+ export { useTransaction } from "./useTransaction";
6
+ export { useValue } from "./useValue";
7
+ export { createValdres } from "./ValdresPlugin";
8
+ export type { ValdresPluginOptions } from "./ValdresPlugin";
9
+ export { ValdresScope } from "./ValdresScope";
10
+ export { ValdresKey } from "./lib/storeKey";
11
+ export type { ValdresContext } from "./lib/storeKey";
@@ -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,7 @@
1
+ import type { InjectionKey } from "vue";
2
+ import type { Store } from "valdres";
3
+ export interface ValdresContext {
4
+ current: Store;
5
+ stores: Record<string, Store>;
6
+ }
7
+ export declare const ValdresKey: InjectionKey<ValdresContext>;
@@ -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,3 @@
1
+ import { type Ref } from "vue";
2
+ import { type Atom, type Store } from "valdres";
3
+ export declare const useAtom: <V>(atom: Atom<V>, store?: Store) => Ref<V>;
@@ -0,0 +1,2 @@
1
+ import type { Atom, Store } from "valdres";
2
+ export declare const useResetAtom: <V>(atom: Atom<V>, store?: Store) => (() => void);
@@ -0,0 +1,2 @@
1
+ import type { Atom, SetAtomValue, Store } from "valdres";
2
+ export declare const useSetAtom: <V>(atom: Atom<V>, store?: Store) => (value: SetAtomValue<V>) => void;
@@ -0,0 +1,2 @@
1
+ import type { Store } from "valdres";
2
+ export declare const useStore: (id?: string) => Store;
@@ -0,0 +1,2 @@
1
+ import type { Store, Transaction } from "valdres";
2
+ export declare const useTransaction: (store?: Store) => (callback: (state: Transaction) => any) => void;
@@ -0,0 +1,3 @@
1
+ import { type Ref } from "vue";
2
+ import { type Atom, type Selector, type Store } from "valdres";
3
+ export declare const useValue: <Value extends any = any>(state: Atom<Value> | Selector<Value>, store?: Store) => Readonly<Ref<Value>>;
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "valdres-vue",
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
+ "vue": ">=3.3"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public",
29
+ "registry": "https://registry.npmjs.org/"
30
+ }
31
+ }