@zwire/vue 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.
@@ -0,0 +1,12 @@
1
+ import { type DefineSetupFnComponent, type InjectionKey, type MaybeRefOrGetter } from 'vue';
2
+ import { type ReactiveScope } from '@zwire/core';
3
+ export interface ScopeProviderProps {
4
+ scope: ReactiveScope;
5
+ }
6
+ /** Injection key carrying the active `ReactiveScope`; usable with `app.provide(ScopeKey, scope)`. */
7
+ export declare const ScopeKey: InjectionKey<ReactiveScope>;
8
+ /** Provides `scope` to every descendant of the current component (call inside `setup()`). */
9
+ export declare function provideScope(scope: ReactiveScope): void;
10
+ export declare const ScopeProvider: DefineSetupFnComponent<ScopeProviderProps>;
11
+ export declare function scopeSource(override?: ReactiveScope): MaybeRefOrGetter<ReactiveScope>;
12
+ export declare function useStoreScope(override?: ReactiveScope): ReactiveScope;
@@ -0,0 +1,26 @@
1
+ import { defineComponent, hasInjectionContext, inject, provide, toValue, } from 'vue';
2
+ import { defaultScope } from '@zwire/core';
3
+ /** Injection key carrying the active `ReactiveScope`; usable with `app.provide(ScopeKey, scope)`. */
4
+ export const ScopeKey = /* @__PURE__ */ Symbol('zwire.scope');
5
+ const SCOPE_GET = /* @__PURE__ */ Symbol();
6
+ /** Provides `scope` to every descendant of the current component (call inside `setup()`). */
7
+ export function provideScope(scope) {
8
+ provide(ScopeKey, scope);
9
+ provide(SCOPE_GET, undefined);
10
+ }
11
+ export const ScopeProvider =
12
+ /* @__PURE__ */ defineComponent((props, { slots }) => {
13
+ provide(ScopeKey, props.scope);
14
+ provide(SCOPE_GET, () => props.scope);
15
+ return () => slots.default?.();
16
+ }, { name: 'ScopeProvider', props: ['scope'] });
17
+ export function scopeSource(override) {
18
+ return (override ??
19
+ (hasInjectionContext()
20
+ ? (inject(SCOPE_GET, undefined) ?? inject(ScopeKey, defaultScope))
21
+ : defaultScope));
22
+ }
23
+ export function useStoreScope(override) {
24
+ return toValue(scopeSource(override));
25
+ }
26
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,mBAAmB,EACnB,MAAM,EACN,OAAO,EACP,OAAO,GAIR,MAAM,KAAK,CAAC;AACb,OAAO,EAAE,YAAY,EAAsB,MAAM,aAAa,CAAC;AAM/D,qGAAqG;AACrG,MAAM,CAAC,MAAM,QAAQ,GAAgC,eAAe,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;AAE3F,MAAM,SAAS,GAAoD,eAAe,CAAC,MAAM,EAAE,CAAC;AAE5F,6FAA6F;AAC7F,MAAM,UAAU,YAAY,CAAC,KAAoB;IAC/C,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACzB,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,CAAC,MAAM,aAAa;AACxB,eAAe,CAAC,eAAe,CAC7B,CAAC,KAAyB,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE;IACvC,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IAC/B,OAAO,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACtC,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;AACjC,CAAC,EACD,EAAE,IAAI,EAAE,eAAe,EAAE,KAAK,EAAE,CAAC,OAAO,CAAC,EAAE,CAC5C,CAAC;AAEJ,MAAM,UAAU,WAAW,CAAC,QAAwB;IAClD,OAAO,CACL,QAAQ;QACR,CAAC,mBAAmB,EAAE;YACpB,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,IAAI,MAAM,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;YAClE,CAAC,CAAC,YAAY,CAAC,CAClB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,QAAwB;IACpD,OAAO,OAAO,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC;AACxC,CAAC","sourcesContent":["import {\n defineComponent,\n hasInjectionContext,\n inject,\n provide,\n toValue,\n type DefineSetupFnComponent,\n type InjectionKey,\n type MaybeRefOrGetter,\n} from 'vue';\nimport { defaultScope, type ReactiveScope } from '@zwire/core';\n\nexport interface ScopeProviderProps {\n scope: ReactiveScope;\n}\n\n/** Injection key carrying the active `ReactiveScope`; usable with `app.provide(ScopeKey, scope)`. */\nexport const ScopeKey: InjectionKey<ReactiveScope> = /* @__PURE__ */ Symbol('zwire.scope');\n\nconst SCOPE_GET: InjectionKey<(() => ReactiveScope) | undefined> = /* @__PURE__ */ Symbol();\n\n/** Provides `scope` to every descendant of the current component (call inside `setup()`). */\nexport function provideScope(scope: ReactiveScope): void {\n provide(ScopeKey, scope);\n provide(SCOPE_GET, undefined);\n}\n\nexport const ScopeProvider: DefineSetupFnComponent<ScopeProviderProps> =\n /* @__PURE__ */ defineComponent(\n (props: ScopeProviderProps, { slots }) => {\n provide(ScopeKey, props.scope);\n provide(SCOPE_GET, () => props.scope);\n return () => slots.default?.();\n },\n { name: 'ScopeProvider', props: ['scope'] },\n );\n\nexport function scopeSource(override?: ReactiveScope): MaybeRefOrGetter<ReactiveScope> {\n return (\n override ??\n (hasInjectionContext()\n ? (inject(SCOPE_GET, undefined) ?? inject(ScopeKey, defaultScope))\n : defaultScope)\n );\n}\n\nexport function useStoreScope(override?: ReactiveScope): ReactiveScope {\n return toValue(scopeSource(override));\n}\n"]}
@@ -0,0 +1,15 @@
1
+ import { type MaybeRefOrGetter, type Ref } from 'vue';
2
+ import type { ReactiveScope, ReadableSignal } from '@zwire/core';
3
+ /** Lazily open a layer for this component. Unmount reverts an open layer. */
4
+ export declare function useOptimistic<H extends {
5
+ readonly active: boolean;
6
+ revert(): void;
7
+ }>(create: (scope: ReactiveScope) => H, scope?: ReactiveScope): () => H;
8
+ /** Lazily open a draft for this component. Unmount discards an open draft. */
9
+ export declare function useDraft<H extends {
10
+ readonly closed: boolean;
11
+ discard(): void;
12
+ }>(create: (scope: ReactiveScope) => H, scope?: ReactiveScope): () => H;
13
+ export declare function useCollectionTopK<T>(collection: MaybeRefOrGetter<{
14
+ topK(k: number): ReadableSignal<readonly T[]>;
15
+ }>, k: MaybeRefOrGetter<number>, scope?: ReactiveScope): Readonly<Ref<readonly T[]>>;
package/dist/hooks.js ADDED
@@ -0,0 +1,41 @@
1
+ import { onScopeDispose, watch, toValue } from 'vue';
2
+ import { useSignal } from './index.js';
3
+ import { scopeSource } from './context.js';
4
+ function useTransactionHandle(create, scope) {
5
+ const source = scopeSource(scope);
6
+ let current;
7
+ let disposed = false;
8
+ const close = () => {
9
+ const previous = current;
10
+ current = undefined;
11
+ if (previous && 'revert' in previous)
12
+ previous.revert();
13
+ else
14
+ previous?.discard();
15
+ };
16
+ if (typeof source === 'function')
17
+ watch(source, close, { flush: 'sync' });
18
+ onScopeDispose(() => {
19
+ disposed = true;
20
+ close();
21
+ });
22
+ return () => {
23
+ if (disposed)
24
+ throw new Error('Transaction owner disposed');
25
+ if (!current || ('active' in current ? !current.active : current.closed))
26
+ current = create(toValue(source));
27
+ return current;
28
+ };
29
+ }
30
+ /** Lazily open a layer for this component. Unmount reverts an open layer. */
31
+ export function useOptimistic(create, scope) {
32
+ return useTransactionHandle(create, scope);
33
+ }
34
+ /** Lazily open a draft for this component. Unmount discards an open draft. */
35
+ export function useDraft(create, scope) {
36
+ return useTransactionHandle(create, scope);
37
+ }
38
+ export function useCollectionTopK(collection, k, scope) {
39
+ return useSignal(() => toValue(collection).topK(toValue(k)), scope);
40
+ }
41
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,OAAO,EAAmC,MAAM,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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,UAA+E,EAC/E,CAA2B,EAC3B,KAAqB;IAErB,OAAO,SAAS,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AACtE,CAAC","sourcesContent":["import { onScopeDispose, watch, toValue, type MaybeRefOrGetter, type Ref } from 'vue';\nimport type { ReactiveScope, ReadableSignal } from '@zwire/core';\nimport { useSignal } from './index.ts';\nimport { scopeSource } from './context.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 source = scopeSource(scope);\n let current: H | undefined;\n let disposed = false;\n const close = (): void => {\n const previous = current;\n current = undefined;\n if (previous && 'revert' in previous) previous.revert();\n else previous?.discard();\n };\n if (typeof source === 'function') watch(source, close, { flush: 'sync' });\n onScopeDispose(() => {\n disposed = true;\n close();\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(toValue(source));\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: MaybeRefOrGetter<{ topK(k: number): ReadableSignal<readonly T[]> }>,\n k: MaybeRefOrGetter<number>,\n scope?: ReactiveScope,\n): Readonly<Ref<readonly T[]>> {\n return useSignal(() => toValue(collection).topK(toValue(k)), scope);\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -1,14 +1,6 @@
1
- import { type ComputedRef, type DefineSetupFnComponent, type InjectionKey, type MaybeRefOrGetter, type Ref } from 'vue';
1
+ import { type ComputedRef, type MaybeRefOrGetter, type Ref } from 'vue';
2
2
  import { type EqualityFn, type ReactiveScope, type ReadableSignal, type WritableSignal } from '@zwire/core';
3
- export interface ScopeProviderProps {
4
- scope: ReactiveScope;
5
- }
6
- /** Injection key carrying the active `ReactiveScope`; usable with `app.provide(ScopeKey, scope)`. */
7
- export declare const ScopeKey: InjectionKey<ReactiveScope>;
8
- /** Provides `scope` to every descendant of the current component (call inside `setup()`). */
9
- export declare function provideScope(scope: ReactiveScope): void;
10
- export declare const ScopeProvider: DefineSetupFnComponent<ScopeProviderProps>;
11
- export declare function useStoreScope(override?: ReactiveScope): ReactiveScope;
3
+ export { ScopeKey, ScopeProvider, provideScope, useStoreScope, type ScopeProviderProps, } from './context.js';
12
4
  /**
13
5
  * Tracks a zwire signal as a Vue ref. The ref reads through to `signal.get()`, so snapshots are
14
6
  * never copied or deeply proxied. `signal` may be a ref or getter to switch sources reactively.
package/dist/index.js CHANGED
@@ -1,28 +1,7 @@
1
- import { computed, customRef, defineComponent, getCurrentScope, hasInjectionContext, inject, provide, toRaw, toValue, watchEffect, } from 'vue';
2
- import { defaultScope, } from '@zwire/core';
3
- /** Injection key carrying the active `ReactiveScope`; usable with `app.provide(ScopeKey, scope)`. */
4
- export const ScopeKey = /* @__PURE__ */ Symbol('zwire.scope');
5
- const SCOPE_GET = /* @__PURE__ */ Symbol();
6
- /** Provides `scope` to every descendant of the current component (call inside `setup()`). */
7
- export function provideScope(scope) {
8
- provide(ScopeKey, scope);
9
- provide(SCOPE_GET, undefined);
10
- }
11
- export const ScopeProvider =
12
- /* @__PURE__ */ defineComponent((props, { slots }) => {
13
- provide(ScopeKey, props.scope);
14
- provide(SCOPE_GET, () => props.scope);
15
- return () => slots.default?.();
16
- }, { name: 'ScopeProvider', props: ['scope'] });
17
- function scopeSource(override) {
18
- return (override ??
19
- (hasInjectionContext()
20
- ? (inject(SCOPE_GET, undefined) ?? inject(ScopeKey, defaultScope))
21
- : defaultScope));
22
- }
23
- export function useStoreScope(override) {
24
- return toValue(scopeSource(override));
25
- }
1
+ import { computed, customRef, getCurrentScope, toRaw, toValue, watchEffect, } from 'vue';
2
+ import {} from '@zwire/core';
3
+ import { scopeSource } from './context.js';
4
+ export { ScopeKey, ScopeProvider, provideScope, useStoreScope, } from './context.js';
26
5
  /** Resolves a possibly reactive source and strips any Vue proxy wrapped around the signal. */
27
6
  function resolve(signal) {
28
7
  return toRaw(toValue(signal));
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,SAAS,EACT,eAAe,EACf,eAAe,EACf,mBAAmB,EACnB,MAAM,EACN,OAAO,EACP,KAAK,EACL,OAAO,EACP,WAAW,GAMZ,MAAM,KAAK,CAAC;AACb,OAAO,EACL,YAAY,GAKb,MAAM,aAAa,CAAC;AAMrB,qGAAqG;AACrG,MAAM,CAAC,MAAM,QAAQ,GAAgC,eAAe,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;AAE3F,MAAM,SAAS,GAAoD,eAAe,CAAC,MAAM,EAAE,CAAC;AAE5F,6FAA6F;AAC7F,MAAM,UAAU,YAAY,CAAC,KAAoB;IAC/C,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACzB,OAAO,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;AAChC,CAAC;AAED,MAAM,CAAC,MAAM,aAAa;AACxB,eAAe,CAAC,eAAe,CAC7B,CAAC,KAA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C;IACd,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n computed,\n customRef,\n defineComponent,\n getCurrentScope,\n hasInjectionContext,\n inject,\n provide,\n toRaw,\n toValue,\n watchEffect,\n type ComputedRef,\n type DefineSetupFnComponent,\n type InjectionKey,\n type MaybeRefOrGetter,\n type Ref,\n} from 'vue';\nimport {\n defaultScope,\n type EqualityFn,\n type ReactiveScope,\n type ReadableSignal,\n type WritableSignal,\n} from '@zwire/core';\n\nexport interface ScopeProviderProps {\n scope: ReactiveScope;\n}\n\n/** Injection key carrying the active `ReactiveScope`; usable with `app.provide(ScopeKey, scope)`. */\nexport const ScopeKey: InjectionKey<ReactiveScope> = /* @__PURE__ */ Symbol('zwire.scope');\n\nconst SCOPE_GET: InjectionKey<(() => ReactiveScope) | undefined> = /* @__PURE__ */ Symbol();\n\n/** Provides `scope` to every descendant of the current component (call inside `setup()`). */\nexport function provideScope(scope: ReactiveScope): void {\n provide(ScopeKey, scope);\n provide(SCOPE_GET, undefined);\n}\n\nexport const ScopeProvider: DefineSetupFnComponent<ScopeProviderProps> =\n /* @__PURE__ */ defineComponent(\n (props: ScopeProviderProps, { slots }) => {\n provide(ScopeKey, props.scope);\n provide(SCOPE_GET, () => props.scope);\n return () => slots.default?.();\n },\n { name: 'ScopeProvider', props: ['scope'] },\n );\n\nfunction scopeSource(override?: ReactiveScope): MaybeRefOrGetter<ReactiveScope> {\n return (\n override ??\n (hasInjectionContext()\n ? (inject(SCOPE_GET, undefined) ?? inject(ScopeKey, defaultScope))\n : defaultScope)\n );\n}\n\nexport function useStoreScope(override?: ReactiveScope): ReactiveScope {\n return toValue(scopeSource(override));\n}\n\n/** Resolves a possibly reactive source and strips any Vue proxy wrapped around the signal. */\nfunction resolve<Sig extends object>(signal: MaybeRefOrGetter<Sig>): Sig {\n return toRaw(toValue(signal));\n}\n\nfunction bridge<T>(\n signal: MaybeRefOrGetter<ReadableSignal<T>>,\n scope: ReactiveScope | undefined,\n write?: (value: T) => void,\n): Ref<T> {\n const sc = scopeSource(scope);\n return customRef<T>((track, trigger) => {\n if (getCurrentScope()) {\n watchEffect((onCleanup) => onCleanup(toValue(sc).subscribe(resolve(signal), trigger)), {\n flush: 'sync',\n });\n }\n return {\n get(): T {\n track();\n return resolve(signal).get();\n },\n set(value: T): void {\n write?.(value);\n },\n };\n });\n}\n\n/**\n * Tracks a zwire signal as a Vue ref. The ref reads through to `signal.get()`, so snapshots are\n * never copied or deeply proxied. `signal` may be a ref or getter to switch sources reactively.\n */\nexport function useSignal<T>(\n signal: MaybeRefOrGetter<ReadableSignal<T>>,\n scope?: ReactiveScope,\n): Readonly<Ref<T>> {\n return bridge(signal, scope);\n}\n\nexport const useAtomValue: typeof useSignal = useSignal;\n\n/** Two-way ref over a writable zwire signal, suitable for `v-model`. */\nexport function useAtom<T>(\n signal: MaybeRefOrGetter<WritableSignal<T>>,\n scope?: ReactiveScope,\n): Ref<T> {\n return bridge(signal, scope, (value) => {\n resolve(signal).set(() => value);\n });\n}\n\n/**\n * Derives a slice of `source`. Dependents re-run only when `equals` reports a changed slice, and\n * an equal slice keeps its previous reference.\n */\nexport function useSelector<T, S>(\n source: MaybeRefOrGetter<ReadableSignal<T>>,\n selector: (value: T) => S,\n equals?: EqualityFn<S>,\n scope?: ReactiveScope,\n): ComputedRef<S> {\n const input = useSignal(source, scope);\n const eq = equals ?? Object.is;\n let has = false;\n let last: S;\n return computed<S>(() => {\n const next = selector(input.value);\n if (has && eq(last, next)) return last;\n has = true;\n last = next;\n return next;\n });\n}\n"]}
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,SAAS,EACT,eAAe,EACf,KAAK,EACL,OAAO,EACP,WAAW,GAIZ,MAAM,KAAK,CAAC;AACb,OAAO,EAKN,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EACL,QAAQ,EACR,aAAa,EACb,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,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CACzB,MAA2C,EAC3C,QAAyB,EACzB,MAAsB,EACtB,KAAqB;IAErB,MAAM,KAAK,GAAG,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACvC,MAAM,EAAE,GAAG,MAAM,IAAI,MAAM,CAAC,EAAE,CAAC;IAC/B,IAAI,GAAG,GAAG,KAAK,CAAC;IAChB,IAAI,IAAO,CAAC;IACZ,OAAO,QAAQ,CAAI,GAAG,EAAE;QACtB,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACnC,IAAI,GAAG,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC;QACvC,GAAG,GAAG,IAAI,CAAC;QACX,IAAI,GAAG,IAAI,CAAC;QACZ,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {\n computed,\n customRef,\n getCurrentScope,\n toRaw,\n toValue,\n watchEffect,\n type ComputedRef,\n type MaybeRefOrGetter,\n type Ref,\n} from 'vue';\nimport {\n type EqualityFn,\n type ReactiveScope,\n type ReadableSignal,\n type WritableSignal,\n} from '@zwire/core';\n\nimport { scopeSource } from './context.ts';\nexport {\n ScopeKey,\n ScopeProvider,\n provideScope,\n useStoreScope,\n type ScopeProviderProps,\n} from './context.ts';\n\n/** Resolves a possibly reactive source and strips any Vue proxy wrapped around the signal. */\nfunction resolve<Sig extends object>(signal: MaybeRefOrGetter<Sig>): Sig {\n return toRaw(toValue(signal));\n}\n\nfunction bridge<T>(\n signal: MaybeRefOrGetter<ReadableSignal<T>>,\n scope: ReactiveScope | undefined,\n write?: (value: T) => void,\n): Ref<T> {\n const sc = scopeSource(scope);\n return customRef<T>((track, trigger) => {\n if (getCurrentScope()) {\n watchEffect((onCleanup) => onCleanup(toValue(sc).subscribe(resolve(signal), trigger)), {\n flush: 'sync',\n });\n }\n return {\n get(): T {\n track();\n return resolve(signal).get();\n },\n set(value: T): void {\n write?.(value);\n },\n };\n });\n}\n\n/**\n * Tracks a zwire signal as a Vue ref. The ref reads through to `signal.get()`, so snapshots are\n * never copied or deeply proxied. `signal` may be a ref or getter to switch sources reactively.\n */\nexport function useSignal<T>(\n signal: MaybeRefOrGetter<ReadableSignal<T>>,\n scope?: ReactiveScope,\n): Readonly<Ref<T>> {\n return bridge(signal, scope);\n}\n\nexport const useAtomValue: typeof useSignal = useSignal;\n\n/** Two-way ref over a writable zwire signal, suitable for `v-model`. */\nexport function useAtom<T>(\n signal: MaybeRefOrGetter<WritableSignal<T>>,\n scope?: ReactiveScope,\n): Ref<T> {\n return bridge(signal, scope, (value) => {\n resolve(signal).set(() => value);\n });\n}\n\n/**\n * Derives a slice of `source`. Dependents re-run only when `equals` reports a changed slice, and\n * an equal slice keeps its previous reference.\n */\nexport function useSelector<T, S>(\n source: MaybeRefOrGetter<ReadableSignal<T>>,\n selector: (value: T) => S,\n equals?: EqualityFn<S>,\n scope?: ReactiveScope,\n): ComputedRef<S> {\n const input = useSignal(source, scope);\n const eq = equals ?? Object.is;\n let has = false;\n let last: S;\n return computed<S>(() => {\n const next = selector(input.value);\n if (has && eq(last, next)) return last;\n has = true;\n last = next;\n return next;\n });\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwire/vue",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Vue 3.5+ read-through customRef and v-model adapter for @zwire/core",
5
5
  "keywords": [
6
6
  "reactive",
@@ -34,13 +34,17 @@
34
34
  "types": "./dist/index.d.ts",
35
35
  "default": "./dist/index.js"
36
36
  },
37
+ "./hooks": {
38
+ "types": "./dist/hooks.d.ts",
39
+ "default": "./dist/hooks.js"
40
+ },
37
41
  "./package.json": "./package.json"
38
42
  },
39
43
  "publishConfig": {
40
44
  "access": "public"
41
45
  },
42
46
  "peerDependencies": {
43
- "@zwire/core": "^0.1.0",
47
+ "@zwire/core": "^0.2.0",
44
48
  "vue": ">=3.5.0"
45
49
  },
46
50
  "engines": {