import { defineStore as definePiniaStore, storeToRefs as piniaStoreToRefs, type StoreGeneric, } from 'pinia'; import { toRefs as composableStoreToRefs, type ToRefs } from 'vue'; import { applyStorePatch } from '../scripts/utils'; import type { CreateDatalistStoreParams, Identifiable, Patch, PatchableStore, PatchableStoreFactory, StoreInstance, } from '../types/createDatalistStore.types'; import { DatalistStoreProvider, type DatalistStoreProviderType, } from '../types/StoreProvider'; const defaultStoreType = DatalistStoreProvider.Pinia; /** * makeThisToRefs converts a store object into a set of reactive references (toRefs), * using Pinia's storeToRefs if it's a Pinia store, or Vue's toRefs for composable stores. * */ export const makeThisToRefs = ( store: StoreBody, storeType: DatalistStoreProviderType, ): ToRefs => { const thisStoreType = storeType || defaultStoreType; if (thisStoreType === DatalistStoreProvider.Pinia) { return piniaStoreToRefs(store as StoreGeneric) as ToRefs; } return composableStoreToRefs(store) as ToRefs; }; export const createDatalistStore = < StoreBody extends StoreInstance, G extends Identifiable, >({ config, namespace, storeBody, }: CreateDatalistStoreParams< StoreBody, G >): PatchableStoreFactory => { const thisStoreType = config.storeType || defaultStoreType; if (thisStoreType === DatalistStoreProvider.Composable) { const storeFactory = storeBody({ ...config, storeType: thisStoreType, }) as PatchableStore; storeFactory.$patch = (val: Patch) => applyStorePatch(storeFactory, val); return () => storeFactory; } if (thisStoreType === DatalistStoreProvider.Pinia) { return definePiniaStore(namespace, () => storeBody({ ...config, storeType: thisStoreType, }), ); } throw new Error(`Unsupported store type: ${thisStoreType}`); };