import { makeProxy } from '@/proxy'; import { firstRunner } from '@/connectors/runner'; import type { Renew, StoreType, StateRefStore, StoreRenderList } from '@/types'; /** * Make the value a stateRef. */ export function makeReference({ renew, rootValue, storeRenderList, cacheMap, autoSync, editable, }: { renew: Renew>; rootValue: StoreType; storeRenderList: StoreRenderList; cacheMap: WeakMap>, StateRefStore>; autoSync: boolean; editable: boolean; }) { const ref: { value: null | StateRefStore> } = { value: null, }; const run = (isFirst?: boolean) => renew(ref.value!.root, isFirst ?? false); ref.value = makeProxy, StateRefStore>>( rootValue, storeRenderList, run, autoSync, editable, rootValue ); /** * It is initialized only once per subscription and collects the 'abort' signal. */ firstRunner(run, storeRenderList, cacheMap, renew); cacheMap.set(renew, ref.value!.root); return ref.value!.root; }