valdres-svelte 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 +71 -0
- package/dist/types/context.d.ts +3 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/readable.d.ts +3 -0
- package/dist/types/scope.d.ts +2 -0
- package/dist/types/watch.svelte.d.ts +12 -0
- package/package.json +32 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// src/watch.svelte.ts
|
|
2
|
+
import { isAtom } from "valdres";
|
|
3
|
+
|
|
4
|
+
// src/context.ts
|
|
5
|
+
import { getContext, setContext } from "svelte";
|
|
6
|
+
var VALDRES_CONTEXT_KEY = "valdres-store";
|
|
7
|
+
var setValdresContext = (store) => {
|
|
8
|
+
setContext(VALDRES_CONTEXT_KEY, store);
|
|
9
|
+
};
|
|
10
|
+
var getValdresContext = () => {
|
|
11
|
+
const store = getContext(VALDRES_CONTEXT_KEY);
|
|
12
|
+
if (!store)
|
|
13
|
+
throw new Error("No valdres store found in context. Did you call setValdresContext(store) in a parent component?");
|
|
14
|
+
return store;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// src/watch.svelte.ts
|
|
18
|
+
function watch(state, store) {
|
|
19
|
+
const resolvedStore = store || getValdresContext();
|
|
20
|
+
let value = $state(resolvedStore.get(state));
|
|
21
|
+
$effect.pre(() => {
|
|
22
|
+
value = resolvedStore.get(state);
|
|
23
|
+
const unsub = resolvedStore.sub(state, () => {
|
|
24
|
+
value = resolvedStore.get(state);
|
|
25
|
+
});
|
|
26
|
+
return unsub;
|
|
27
|
+
});
|
|
28
|
+
if (isAtom(state)) {
|
|
29
|
+
return {
|
|
30
|
+
get value() {
|
|
31
|
+
return value;
|
|
32
|
+
},
|
|
33
|
+
set: (v) => resolvedStore.set(state, v),
|
|
34
|
+
reset: () => resolvedStore.reset(state)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
get value() {
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
// src/readable.ts
|
|
44
|
+
var readable = (state, store) => {
|
|
45
|
+
return {
|
|
46
|
+
subscribe(run) {
|
|
47
|
+
run(store.get(state));
|
|
48
|
+
return store.sub(state, () => {
|
|
49
|
+
run(store.get(state));
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
// src/scope.ts
|
|
55
|
+
import { onDestroy } from "svelte";
|
|
56
|
+
var generateId = () => (Math.random() + 1).toString(36).substring(7);
|
|
57
|
+
var scope = (scopeId = generateId()) => {
|
|
58
|
+
const parentStore = getValdresContext();
|
|
59
|
+
const scopeCreated = !parentStore.data.scopes?.has(scopeId);
|
|
60
|
+
const scopedStore = parentStore.scope(scopeId);
|
|
61
|
+
setValdresContext(scopedStore);
|
|
62
|
+
onDestroy(() => scopedStore.detach(scopeCreated));
|
|
63
|
+
return scopedStore;
|
|
64
|
+
};
|
|
65
|
+
export {
|
|
66
|
+
watch,
|
|
67
|
+
setValdresContext,
|
|
68
|
+
scope,
|
|
69
|
+
readable,
|
|
70
|
+
getValdresContext
|
|
71
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Atom, type SetAtomValue, type State, type Store } from "valdres";
|
|
2
|
+
interface WatchedValue<V> {
|
|
3
|
+
readonly value: V;
|
|
4
|
+
}
|
|
5
|
+
interface WatchedAtom<V> {
|
|
6
|
+
readonly value: V;
|
|
7
|
+
set: (value: SetAtomValue<V>) => void;
|
|
8
|
+
reset: () => void;
|
|
9
|
+
}
|
|
10
|
+
export declare function watch<V>(atom: Atom<V>, store?: Store): WatchedAtom<V>;
|
|
11
|
+
export declare function watch<V>(state: State<V>, store?: Store): WatchedValue<V>;
|
|
12
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "valdres-svelte",
|
|
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
|
+
"svelte": "./src/index.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"svelte": ">=5",
|
|
26
|
+
"valdres": "workspace:^"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public",
|
|
30
|
+
"registry": "https://registry.npmjs.org/"
|
|
31
|
+
}
|
|
32
|
+
}
|