@zwire/svelte 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +29 -2
- package/dist/ScopeProvider.svelte +10 -0
- package/dist/hooks.d.ts +16 -0
- package/dist/hooks.js +37 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +144 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +8 -0
- package/dist/provider.js +4 -0
- package/dist/provider.js.map +1 -0
- package/dist/svelte.d.ts +2 -0
- package/dist/svelte.js +3 -0
- package/dist/svelte.js.map +1 -0
- package/package.json +59 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kjmin-dev
|
|
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
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@zwire/svelte`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Svelte 5.7+ runes `createSubscriber` ref and store-contract adapter for [`@zwire/core`](https://www.npmjs.com/package/@zwire/core).
|
|
4
|
+
|
|
5
|
+
The hooks are plain TypeScript and read through to `signal.get()`. The native `ScopeProvider` ships as Svelte source so the application compiler can render it on the client and server.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @zwire/core @zwire/svelte
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Import `ScopeProvider` from `@zwire/svelte/ScopeProvider.svelte` to provide a scope to a subtree. Svelte-aware bundlers also support the named `ScopeProvider` export.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```svelte
|
|
18
|
+
<script>
|
|
19
|
+
import { provideScope, toWritable, useAtom, useSelector, useSignal } from '@zwire/svelte';
|
|
20
|
+
|
|
21
|
+
const value = useSignal(count); // { readonly current }
|
|
22
|
+
const draft = useAtom(title); // { current }, works with bind:value
|
|
23
|
+
const name = useSelector(user, (u) => u.name);
|
|
24
|
+
const store = toWritable(count); // Svelte store contract ($store)
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<input bind:value={draft.current} /> {value.current} {name.current} {$store}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
See the [API reference](https://github.com/kjmin-dev/zwire/blob/main/docs/API.md#74-zwiresvelte) for full documentation.
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SignalRef } from './index.js';
|
|
2
|
+
import type { ReactiveScope, ReadableSignal } from '@zwire/core';
|
|
3
|
+
import { type MaybeGetter } from './index.js';
|
|
4
|
+
/** Lazily open a layer for this component. Unmount reverts an open layer. */
|
|
5
|
+
export declare function useOptimistic<H extends {
|
|
6
|
+
readonly active: boolean;
|
|
7
|
+
revert(): void;
|
|
8
|
+
}>(create: (scope: ReactiveScope) => H, scope?: ReactiveScope): () => H;
|
|
9
|
+
/** Lazily open a draft for this component. Unmount discards an open draft. */
|
|
10
|
+
export declare function useDraft<H extends {
|
|
11
|
+
readonly closed: boolean;
|
|
12
|
+
discard(): void;
|
|
13
|
+
}>(create: (scope: ReactiveScope) => H, scope?: ReactiveScope): () => H;
|
|
14
|
+
export declare function useCollectionTopK<T>(collection: MaybeGetter<{
|
|
15
|
+
topK(k: number): ReadableSignal<readonly T[]>;
|
|
16
|
+
}>, k: MaybeGetter<number>, scope?: ReactiveScope): SignalRef<readonly T[]>;
|
package/dist/hooks.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { onDestroy } from 'svelte';
|
|
2
|
+
import { useSignal, useStoreScope } from './index.js';
|
|
3
|
+
function useTransactionHandle(create, scope) {
|
|
4
|
+
const activeScope = useStoreScope(scope);
|
|
5
|
+
let current;
|
|
6
|
+
let disposed = false;
|
|
7
|
+
onDestroy(() => {
|
|
8
|
+
disposed = true;
|
|
9
|
+
if (current && 'revert' in current)
|
|
10
|
+
current.revert();
|
|
11
|
+
else
|
|
12
|
+
current?.discard();
|
|
13
|
+
current = undefined;
|
|
14
|
+
});
|
|
15
|
+
return () => {
|
|
16
|
+
if (disposed)
|
|
17
|
+
throw new Error('Transaction owner disposed');
|
|
18
|
+
if (!current || ('active' in current ? !current.active : current.closed))
|
|
19
|
+
current = create(activeScope);
|
|
20
|
+
return current;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** Lazily open a layer for this component. Unmount reverts an open layer. */
|
|
24
|
+
export function useOptimistic(create, scope) {
|
|
25
|
+
return useTransactionHandle(create, scope);
|
|
26
|
+
}
|
|
27
|
+
/** Lazily open a draft for this component. Unmount discards an open draft. */
|
|
28
|
+
export function useDraft(create, scope) {
|
|
29
|
+
return useTransactionHandle(create, scope);
|
|
30
|
+
}
|
|
31
|
+
export function useCollectionTopK(collection, k, scope) {
|
|
32
|
+
return useSignal(() => {
|
|
33
|
+
const active = typeof collection === 'function' ? collection() : collection;
|
|
34
|
+
return active.topK(typeof k === 'function' ? k() : k);
|
|
35
|
+
}, scope);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAGnC,OAAO,EAAE,SAAS,EAAE,aAAa,EAAoB,MAAM,YAAY,CAAC;AAMxE,SAAS,oBAAoB,CAC3B,MAAmC,EACnC,KAAqB;IAErB,MAAM,WAAW,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IACzC,IAAI,OAAsB,CAAC;IAC3B,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,SAAS,CAAC,GAAG,EAAE;QACb,QAAQ,GAAG,IAAI,CAAC;QAChB,IAAI,OAAO,IAAI,QAAQ,IAAI,OAAO;YAAE,OAAO,CAAC,MAAM,EAAE,CAAC;;YAChD,OAAO,EAAE,OAAO,EAAE,CAAC;QACxB,OAAO,GAAG,SAAS,CAAC;IACtB,CAAC,CAAC,CAAC;IACH,OAAO,GAAG,EAAE;QACV,IAAI,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAC5D,IAAI,CAAC,OAAO,IAAI,CAAC,QAAQ,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC;YACtE,OAAO,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;QAChC,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;AACJ,CAAC;AAED,6EAA6E;AAC7E,MAAM,UAAU,aAAa,CAC3B,MAAmC,EACnC,KAAqB;IAErB,OAAO,oBAAoB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AAC7C,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,QAAQ,CACtB,MAAmC,EACnC,KAAqB;IAErB,OAAO,oBAAoB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AAC7C,CAAC;AAED,MAAM,UAAU,iBAAiB,CAC/B,UAA0E,EAC1E,CAAsB,EACtB,KAAqB;IAErB,OAAO,SAAS,CAAC,GAAG,EAAE;QACpB,MAAM,MAAM,GAAG,OAAO,UAAU,KAAK,UAAU,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC;QAC5E,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACxD,CAAC,EAAE,KAAK,CAAC,CAAC;AACZ,CAAC","sourcesContent":["import { onDestroy } from 'svelte';\nimport type { SignalRef } from './index.ts';\nimport type { ReactiveScope, ReadableSignal } from '@zwire/core';\nimport { useSignal, useStoreScope, type MaybeGetter } from './index.ts';\n\ntype TransactionHandle =\n | { readonly active: boolean; revert(): void }\n | { readonly closed: boolean; discard(): void };\n\nfunction useTransactionHandle<H extends TransactionHandle>(\n create: (scope: ReactiveScope) => H,\n scope?: ReactiveScope,\n): () => H {\n const activeScope = useStoreScope(scope);\n let current: H | undefined;\n let disposed = false;\n onDestroy(() => {\n disposed = true;\n if (current && 'revert' in current) current.revert();\n else current?.discard();\n current = undefined;\n });\n return () => {\n if (disposed) throw new Error('Transaction owner disposed');\n if (!current || ('active' in current ? !current.active : current.closed))\n current = create(activeScope);\n return current;\n };\n}\n\n/** Lazily open a layer for this component. Unmount reverts an open layer. */\nexport function useOptimistic<H extends { readonly active: boolean; revert(): void }>(\n create: (scope: ReactiveScope) => H,\n scope?: ReactiveScope,\n): () => H {\n return useTransactionHandle(create, scope);\n}\n\n/** Lazily open a draft for this component. Unmount discards an open draft. */\nexport function useDraft<H extends { readonly closed: boolean; discard(): void }>(\n create: (scope: ReactiveScope) => H,\n scope?: ReactiveScope,\n): () => H {\n return useTransactionHandle(create, scope);\n}\n\nexport function useCollectionTopK<T>(\n collection: MaybeGetter<{ topK(k: number): ReadableSignal<readonly T[]> }>,\n k: MaybeGetter<number>,\n scope?: ReactiveScope,\n): SignalRef<readonly T[]> {\n return useSignal(() => {\n const active = typeof collection === 'function' ? collection() : collection;\n return active.topK(typeof k === 'function' ? k() : k);\n }, scope);\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Readable, Writable } from 'svelte/store';
|
|
2
|
+
import { type EqualityFn, type ReactiveScope, type ReadableSignal, type WritableSignal } from '@zwire/core';
|
|
3
|
+
/** A static value, or a getter read on every access to switch sources reactively. */
|
|
4
|
+
export type MaybeGetter<T> = T | (() => T);
|
|
5
|
+
/** Reactive read-only view of a zwire signal; read `current` inside markup, `$derived` or `$effect`. */
|
|
6
|
+
export interface SignalRef<T> {
|
|
7
|
+
readonly current: T;
|
|
8
|
+
}
|
|
9
|
+
/** Reactive two-way view of a writable zwire signal, suitable for `bind:value={ref.current}`. */
|
|
10
|
+
export interface AtomRef<T> {
|
|
11
|
+
current: T;
|
|
12
|
+
}
|
|
13
|
+
/** Provides `scope` to every descendant of the current component (call during component init). */
|
|
14
|
+
export declare function provideScope(scope: ReactiveScope | (() => ReactiveScope)): void;
|
|
15
|
+
export declare function useStoreScope(override?: ReactiveScope): ReactiveScope;
|
|
16
|
+
/**
|
|
17
|
+
* Tracks a zwire signal from Svelte 5 runes code. `current` reads through to `signal.get()`.
|
|
18
|
+
* Pass a getter (`() => atom`) to switch sources reactively.
|
|
19
|
+
*/
|
|
20
|
+
export declare function useSignal<T>(signal: MaybeGetter<ReadableSignal<T>>, scope?: ReactiveScope): SignalRef<T>;
|
|
21
|
+
export declare const useAtomValue: typeof useSignal;
|
|
22
|
+
/** Two-way view over a writable zwire signal; assigning `current` writes the signal. */
|
|
23
|
+
export declare function useAtom<T>(signal: MaybeGetter<WritableSignal<T>>, scope?: ReactiveScope): AtomRef<T>;
|
|
24
|
+
/**
|
|
25
|
+
* Derives a slice of `source`. Dependent effects re-run only when `equals` reports a changed
|
|
26
|
+
* slice, and an equal slice keeps its previous reference.
|
|
27
|
+
*/
|
|
28
|
+
export declare function useSelector<T, S>(source: MaybeGetter<ReadableSignal<T>>, selector: (value: T) => S, equals?: EqualityFn<S>, scope?: ReactiveScope): SignalRef<S>;
|
|
29
|
+
/** Adapts a zwire signal to the Svelte store contract (`$store` auto-subscription). */
|
|
30
|
+
export declare function toReadable<T>(signal: ReadableSignal<T>, scope?: ReactiveScope): Readable<T>;
|
|
31
|
+
/** Adapts a writable zwire signal to a Svelte writable store (`bind:value={$store}`). */
|
|
32
|
+
export declare function toWritable<T>(signal: WritableSignal<T>, scope?: ReactiveScope): Writable<T>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
import { createSubscriber } from 'svelte/reactivity';
|
|
3
|
+
import { defaultScope, } from '@zwire/core';
|
|
4
|
+
const SCOPE_KEY = /* @__PURE__ */ Symbol('zwire.scope');
|
|
5
|
+
/** Provides `scope` to every descendant of the current component (call during component init). */
|
|
6
|
+
export function provideScope(scope) {
|
|
7
|
+
setContext(SCOPE_KEY, typeof scope === 'function' ? scope : () => scope);
|
|
8
|
+
}
|
|
9
|
+
export function useStoreScope(override) {
|
|
10
|
+
if (override !== undefined)
|
|
11
|
+
return override;
|
|
12
|
+
try {
|
|
13
|
+
return getContext(SCOPE_KEY)?.() ?? defaultScope;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
// Outside component initialisation there is no context to read.
|
|
17
|
+
return defaultScope;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns a reader that resolves the active signal and registers the calling effect with a
|
|
22
|
+
* `createSubscriber` bound to it. The zwire subscription starts with the first tracking reader,
|
|
23
|
+
* stops after the last one is destroyed, and never starts during server rendering.
|
|
24
|
+
*/
|
|
25
|
+
function tracker(source, scope, createFilter) {
|
|
26
|
+
let active;
|
|
27
|
+
let subscribe;
|
|
28
|
+
return () => {
|
|
29
|
+
const signal = typeof source === 'function' ? source() : source;
|
|
30
|
+
if (subscribe === undefined || signal !== active) {
|
|
31
|
+
active = signal;
|
|
32
|
+
subscribe = createSubscriber((update) => {
|
|
33
|
+
const shouldNotify = createFilter?.(signal);
|
|
34
|
+
return scope.subscribe(signal, shouldNotify === undefined
|
|
35
|
+
? update
|
|
36
|
+
: (next) => {
|
|
37
|
+
if (shouldNotify(next))
|
|
38
|
+
update();
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
subscribe();
|
|
43
|
+
return signal;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Tracks a zwire signal from Svelte 5 runes code. `current` reads through to `signal.get()`.
|
|
48
|
+
* Pass a getter (`() => atom`) to switch sources reactively.
|
|
49
|
+
*/
|
|
50
|
+
export function useSignal(signal, scope) {
|
|
51
|
+
const read = tracker(signal, useStoreScope(scope));
|
|
52
|
+
return {
|
|
53
|
+
get current() {
|
|
54
|
+
return read().get();
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export const useAtomValue = useSignal;
|
|
59
|
+
/** Two-way view over a writable zwire signal; assigning `current` writes the signal. */
|
|
60
|
+
export function useAtom(signal, scope) {
|
|
61
|
+
const read = tracker(signal, useStoreScope(scope));
|
|
62
|
+
return {
|
|
63
|
+
get current() {
|
|
64
|
+
return read().get();
|
|
65
|
+
},
|
|
66
|
+
set current(value) {
|
|
67
|
+
(typeof signal === 'function' ? signal() : signal).set(() => value);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Derives a slice of `source`. Dependent effects re-run only when `equals` reports a changed
|
|
73
|
+
* slice, and an equal slice keeps its previous reference.
|
|
74
|
+
*/
|
|
75
|
+
export function useSelector(source, selector, equals, scope) {
|
|
76
|
+
const eq = equals ?? Object.is;
|
|
77
|
+
let has = false;
|
|
78
|
+
let last;
|
|
79
|
+
const read = tracker(source, useStoreScope(scope), (signal) => {
|
|
80
|
+
let hasActive = false;
|
|
81
|
+
let activeVal;
|
|
82
|
+
try {
|
|
83
|
+
activeVal = selector(signal.get());
|
|
84
|
+
hasActive = true;
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// Initial selector error will be surfaced on read.
|
|
88
|
+
}
|
|
89
|
+
return (next) => {
|
|
90
|
+
try {
|
|
91
|
+
const nextSel = selector(next);
|
|
92
|
+
if (hasActive && eq(activeVal, nextSel))
|
|
93
|
+
return false;
|
|
94
|
+
activeVal = nextSel;
|
|
95
|
+
hasActive = true;
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
hasActive = false;
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
return {
|
|
105
|
+
get current() {
|
|
106
|
+
const next = selector(read().get());
|
|
107
|
+
if (has && eq(last, next))
|
|
108
|
+
return last;
|
|
109
|
+
has = true;
|
|
110
|
+
last = next;
|
|
111
|
+
return next;
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
/** Adapts a zwire signal to the Svelte store contract (`$store` auto-subscription). */
|
|
116
|
+
export function toReadable(signal, scope) {
|
|
117
|
+
const activeScope = useStoreScope(scope);
|
|
118
|
+
return {
|
|
119
|
+
subscribe(run) {
|
|
120
|
+
const stop = activeScope.subscribe(signal, run);
|
|
121
|
+
try {
|
|
122
|
+
run(signal.get());
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
stop();
|
|
126
|
+
throw err;
|
|
127
|
+
}
|
|
128
|
+
return stop;
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** Adapts a writable zwire signal to a Svelte writable store (`bind:value={$store}`). */
|
|
133
|
+
export function toWritable(signal, scope) {
|
|
134
|
+
return {
|
|
135
|
+
subscribe: toReadable(signal, scope).subscribe,
|
|
136
|
+
set(value) {
|
|
137
|
+
signal.set(() => value);
|
|
138
|
+
},
|
|
139
|
+
update(updater) {
|
|
140
|
+
signal.set(updater);
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAErD,OAAO,EACL,YAAY,GAKb,MAAM,aAAa,CAAC;AAerB,MAAM,SAAS,GAAkB,eAAe,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;AAEvE,kGAAkG;AAClG,MAAM,UAAU,YAAY,CAAC,KAA4C;IACvE,UAAU,CAAC,SAAS,EAAE,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;AAC3E,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,QAAwB;IACpD,IAAI,QAAQ,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAC;IAC5C,IAAI,CAAC;QACH,OAAO,UAAU,CAAoC,SAAS,CAAC,EAAE,EAAE,IAAI,YAAY,CAAC;IACtF,CAAC;IAAC,MAAM,CAAC;QACP,gEAAgE;QAChE,OAAO,YAAY,CAAC;IACtB,CAAC;AACH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from 'svelte/reactivity';\nimport type { Readable, Writable } from 'svelte/store';\nimport {\n defaultScope,\n type EqualityFn,\n type ReactiveScope,\n type ReadableSignal,\n type WritableSignal,\n} from '@zwire/core';\n\n/** A static value, or a getter read on every access to switch sources reactively. */\nexport type MaybeGetter<T> = T | (() => T);\n\n/** Reactive read-only view of a zwire signal; read `current` inside markup, `$derived` or `$effect`. */\nexport interface SignalRef<T> {\n readonly current: T;\n}\n\n/** Reactive two-way view of a writable zwire signal, suitable for `bind:value={ref.current}`. */\nexport interface AtomRef<T> {\n current: T;\n}\n\nconst SCOPE_KEY: unique symbol = /* @__PURE__ */ Symbol('zwire.scope');\n\n/** Provides `scope` to every descendant of the current component (call during component init). */\nexport function provideScope(scope: ReactiveScope | (() => ReactiveScope)): void {\n setContext(SCOPE_KEY, typeof scope === 'function' ? scope : () => scope);\n}\n\nexport function useStoreScope(override?: ReactiveScope): ReactiveScope {\n if (override !== undefined) return override;\n try {\n return getContext<(() => ReactiveScope) | undefined>(SCOPE_KEY)?.() ?? defaultScope;\n } catch {\n // Outside component initialisation there is no context to read.\n return defaultScope;\n }\n}\n\n/**\n * Returns a reader that resolves the active signal and registers the calling effect with a\n * `createSubscriber` bound to it. The zwire subscription starts with the first tracking reader,\n * stops after the last one is destroyed, and never starts during server rendering.\n */\nfunction tracker<T, Sig extends ReadableSignal<T>>(\n source: MaybeGetter<Sig>,\n scope: ReactiveScope,\n createFilter?: (signal: Sig) => (next: T) => boolean,\n): () => Sig {\n let active: Sig | undefined;\n let subscribe: (() => void) | undefined;\n return () => {\n const signal = typeof source === 'function' ? source() : source;\n if (subscribe === undefined || signal !== active) {\n active = signal;\n subscribe = createSubscriber((update) => {\n const shouldNotify = createFilter?.(signal);\n return scope.subscribe(\n signal,\n shouldNotify === undefined\n ? update\n : (next) => {\n if (shouldNotify(next)) update();\n },\n );\n });\n }\n subscribe();\n return signal;\n };\n}\n\n/**\n * Tracks a zwire signal from Svelte 5 runes code. `current` reads through to `signal.get()`.\n * Pass a getter (`() => atom`) to switch sources reactively.\n */\nexport function useSignal<T>(\n signal: MaybeGetter<ReadableSignal<T>>,\n scope?: ReactiveScope,\n): SignalRef<T> {\n const read = tracker<T, ReadableSignal<T>>(signal, useStoreScope(scope));\n return {\n get current(): T {\n return read().get();\n },\n };\n}\n\nexport const useAtomValue: typeof useSignal = useSignal;\n\n/** Two-way view over a writable zwire signal; assigning `current` writes the signal. */\nexport function useAtom<T>(\n signal: MaybeGetter<WritableSignal<T>>,\n scope?: ReactiveScope,\n): AtomRef<T> {\n const read = tracker<T, WritableSignal<T>>(signal, useStoreScope(scope));\n return {\n get current(): T {\n return read().get();\n },\n set current(value: T) {\n (typeof signal === 'function' ? signal() : signal).set(() => value);\n },\n };\n}\n\n/**\n * Derives a slice of `source`. Dependent effects re-run only when `equals` reports a changed\n * slice, and an equal slice keeps its previous reference.\n */\nexport function useSelector<T, S>(\n source: MaybeGetter<ReadableSignal<T>>,\n selector: (value: T) => S,\n equals?: EqualityFn<S>,\n scope?: ReactiveScope,\n): SignalRef<S> {\n const eq = equals ?? Object.is;\n let has = false;\n let last: S;\n const read = tracker<T, ReadableSignal<T>>(source, useStoreScope(scope), (signal) => {\n let hasActive = false;\n let activeVal: S;\n try {\n activeVal = selector(signal.get());\n hasActive = true;\n } catch {\n // Initial selector error will be surfaced on read.\n }\n return (next) => {\n try {\n const nextSel = selector(next);\n if (hasActive && eq(activeVal, nextSel)) return false;\n activeVal = nextSel;\n hasActive = true;\n return true;\n } catch {\n hasActive = false;\n return true;\n }\n };\n });\n return {\n get current(): S {\n const next = selector(read().get());\n if (has && eq(last, next)) return last;\n has = true;\n last = next;\n return next;\n },\n };\n}\n\n/** Adapts a zwire signal to the Svelte store contract (`$store` auto-subscription). */\nexport function toReadable<T>(signal: ReadableSignal<T>, scope?: ReactiveScope): Readable<T> {\n const activeScope = useStoreScope(scope);\n return {\n subscribe(run) {\n const stop = activeScope.subscribe(signal, run);\n try {\n run(signal.get());\n } catch (err) {\n stop();\n throw err;\n }\n return stop;\n },\n };\n}\n\n/** Adapts a writable zwire signal to a Svelte writable store (`bind:value={$store}`). */\nexport function toWritable<T>(signal: WritableSignal<T>, scope?: ReactiveScope): Writable<T> {\n return {\n subscribe: toReadable(signal, scope).subscribe,\n set(value) {\n signal.set(() => value);\n },\n update(updater) {\n signal.set(updater);\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ReactiveScope } from '@zwire/core';
|
|
2
|
+
import type { Component, Snippet } from 'svelte';
|
|
3
|
+
export interface ScopeProviderProps {
|
|
4
|
+
scope: ReactiveScope;
|
|
5
|
+
children?: Snippet;
|
|
6
|
+
}
|
|
7
|
+
declare const ScopeProvider: Component<ScopeProviderProps>;
|
|
8
|
+
export default ScopeProvider;
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sources":["../src/provider.ts"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,wBAAwB,CAAC;AAO9C,MAAM,aAAa,GAAkC,QAAQ,CAAC;AAC9D,eAAe,aAAa,CAAC","sourcesContent":["import type { ReactiveScope } from '@zwire/core';\nimport type { Component, Snippet } from 'svelte';\nimport Provider from './ScopeProvider.svelte';\n\nexport interface ScopeProviderProps {\n scope: ReactiveScope;\n children?: Snippet;\n}\n\nconst ScopeProvider: Component<ScopeProviderProps> = Provider;\nexport default ScopeProvider;\n"]}
|
package/dist/svelte.d.ts
ADDED
package/dist/svelte.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"svelte.js","sources":["../src/svelte.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,OAAO,IAAI,aAAa,EAA2B,MAAM,eAAe,CAAC","sourcesContent":["export * from './index.ts';\nexport { default as ScopeProvider, type ScopeProviderProps } from './provider.ts';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwire/svelte",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Svelte 5 runes createSubscriber ref and store-contract adapter for @zwire/core",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"reactive",
|
|
7
|
+
"signals",
|
|
8
|
+
"state-management",
|
|
9
|
+
"store",
|
|
10
|
+
"svelte",
|
|
11
|
+
"typescript",
|
|
12
|
+
"zwire"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/kjmin-dev/zwire#readme",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/kjmin-dev/zwire/issues"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Kyoungjun Min",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/kjmin-dev/zwire.git",
|
|
23
|
+
"directory": "ts/packages/adapters/svelte"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"svelte": {
|
|
35
|
+
"types": "./dist/svelte.d.ts",
|
|
36
|
+
"default": "./dist/svelte.js"
|
|
37
|
+
},
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./ScopeProvider.svelte": {
|
|
42
|
+
"types": "./dist/provider.d.ts",
|
|
43
|
+
"default": "./dist/ScopeProvider.svelte"
|
|
44
|
+
},
|
|
45
|
+
"./hooks": {
|
|
46
|
+
"types": "./dist/hooks.d.ts",
|
|
47
|
+
"default": "./dist/hooks.js"
|
|
48
|
+
},
|
|
49
|
+
"./package.json": "./package.json"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@zwire/core": "^0.2.0",
|
|
56
|
+
"svelte": ">=5.7.0"
|
|
57
|
+
},
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=20.0.0"
|
|
60
|
+
}
|
|
61
|
+
}
|