import { shallowRef, triggerRef, onScopeDispose, Ref } from 'vue'; import type { Observable } from 'rxjs'; import type { RxReactivityFactory, ReactivityLambda } from '../../types'; /** * Type-level function (ReactivityLambda) for Vue refs. * Use this as the Reactivity type parameter for properly typed refs. * * @example * const db = await createRxDatabase({ * reactivity: VueRxReactivityFactory * }); * const ref = doc.age$$; // Ref */ export interface VueRefReactivityLambda extends ReactivityLambda { readonly _result: Ref; } export type VueRef = Ref; export const VueRxReactivityFactory: RxReactivityFactory = { fromObservable( obs: Observable, initialValue: InitData ): VueRef { const ref = shallowRef(initialValue); const sub = obs.subscribe((value: Data) => { ref.value = value; triggerRef(ref); }); onScopeDispose(() => { sub.unsubscribe(); }); return ref; } };