@zwire/svelte 0.1.0 → 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/README.md +3 -1
- 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 +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- 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 +14 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
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
4
|
|
|
5
|
-
|
|
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
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -10,6 +10,8 @@ Plain TypeScript (no Svelte compiler step needed to consume), read-through to `s
|
|
|
10
10
|
npm install @zwire/core @zwire/svelte
|
|
11
11
|
```
|
|
12
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
|
+
|
|
13
15
|
## Usage
|
|
14
16
|
|
|
15
17
|
```svelte
|
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
CHANGED
|
@@ -11,7 +11,7 @@ export interface AtomRef<T> {
|
|
|
11
11
|
current: T;
|
|
12
12
|
}
|
|
13
13
|
/** Provides `scope` to every descendant of the current component (call during component init). */
|
|
14
|
-
export declare function provideScope(scope: ReactiveScope): void;
|
|
14
|
+
export declare function provideScope(scope: ReactiveScope | (() => ReactiveScope)): void;
|
|
15
15
|
export declare function useStoreScope(override?: ReactiveScope): ReactiveScope;
|
|
16
16
|
/**
|
|
17
17
|
* Tracks a zwire signal from Svelte 5 runes code. `current` reads through to `signal.get()`.
|
package/dist/index.js
CHANGED
|
@@ -4,13 +4,13 @@ import { defaultScope, } from '@zwire/core';
|
|
|
4
4
|
const SCOPE_KEY = /* @__PURE__ */ Symbol('zwire.scope');
|
|
5
5
|
/** Provides `scope` to every descendant of the current component (call during component init). */
|
|
6
6
|
export function provideScope(scope) {
|
|
7
|
-
setContext(SCOPE_KEY, scope);
|
|
7
|
+
setContext(SCOPE_KEY, typeof scope === 'function' ? scope : () => scope);
|
|
8
8
|
}
|
|
9
9
|
export function useStoreScope(override) {
|
|
10
10
|
if (override !== undefined)
|
|
11
11
|
return override;
|
|
12
12
|
try {
|
|
13
|
-
return getContext(SCOPE_KEY) ?? defaultScope;
|
|
13
|
+
return getContext(SCOPE_KEY)?.() ?? defaultScope;
|
|
14
14
|
}
|
|
15
15
|
catch {
|
|
16
16
|
// Outside component initialisation there is no context to read.
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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,
|
|
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,CAAC;AAED;;;;GAIG;AACH,SAAS,OAAO,CACd,MAAwB,EACxB,KAAoB,EACpB,YAAoD;IAEpD,IAAI,MAAuB,CAAC;IAC5B,IAAI,SAAmC,CA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AC,IAAI,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC;YACpC,IAAI,GAAG,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;gBAAE,OAAO,IAAI,CAAC;YACvC,GAAG,GAAG,IAAI,CAAC;YACX,IAAI,GAAG,IAAI,CAAC;YACZ,OAAO,IAAI,CAAC;QACd,CAAC;KACF,CAAC;AACJ,CAAC;AAED,uFAAuF;AACvF,MAAM,UAAU,UAAU,CAAI,MAAyB,EAAE,KAAqB;IAC5E,MAAM,WAAW,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IACzC,OAAO;QACL,SAAS,CAAC,GAAG;YACX,MAAM,IAAI,GAAG,WAAW,CAAC,SAAS,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;YAChD,IAAI,CAAC;gBACH,GAAG,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC;YACpB,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,IAAI,EAAE,CAAC;gBACP,MAAM,GAAG,CAAC;YACZ,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;KACF,CAAC;AACJ,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,UAAU,CAAI,MAAyB,EAAE,KAAqB;IAC5E,OAAO;QACL,SAAS,EAAE,UAAU,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,SAAS;QAC9C,GAAG,CAAC,KAAK;YACP,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;QAC1B,CAAC;QACD,MAAM,CAAC,OAAO;YACZ,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACtB,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { getContext, setContext } from 'svelte';\nimport { createSubscriber } 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,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwire/svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Svelte 5 runes createSubscriber ref and store-contract adapter for @zwire/core",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"reactive",
|
|
@@ -31,16 +31,28 @@
|
|
|
31
31
|
"sideEffects": false,
|
|
32
32
|
"exports": {
|
|
33
33
|
".": {
|
|
34
|
+
"svelte": {
|
|
35
|
+
"types": "./dist/svelte.d.ts",
|
|
36
|
+
"default": "./dist/svelte.js"
|
|
37
|
+
},
|
|
34
38
|
"types": "./dist/index.d.ts",
|
|
35
39
|
"default": "./dist/index.js"
|
|
36
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
|
+
},
|
|
37
49
|
"./package.json": "./package.json"
|
|
38
50
|
},
|
|
39
51
|
"publishConfig": {
|
|
40
52
|
"access": "public"
|
|
41
53
|
},
|
|
42
54
|
"peerDependencies": {
|
|
43
|
-
"@zwire/core": "^0.
|
|
55
|
+
"@zwire/core": "^0.2.0",
|
|
44
56
|
"svelte": ">=5.7.0"
|
|
45
57
|
},
|
|
46
58
|
"engines": {
|