import { O } from 'ts-toolbelt' ; import { MagnetarWriteAction , MagnetarFetchAction , MagnetarStreamAction , MagnetarDeleteAction , MagnetarDeletePropAction , MagnetarInsertAction , FetchPromises , } from './types/actions' ; import { actionNameTypeMap } from './types/actionsInternal' ; import { handleActionPerStore , HandleActionSharedParams , } from './moduleActions/handleActionPerStore' ; import { handleStreamPerStore } from './moduleActions/handleStreamPerStore' ; import { ModuleConfig , GlobalConfig } from './types/config' ; import { CollectionFn , DocFn , WriteLock } from './Magnetar' ; import { executeSetupModulePerStore , getDataProxyHandler } from './helpers/moduleHelpers' ; export type DocInstance < DocDataType extends Record < string , any > = Record < string , any > > = { /**
     * The cached data that was written or read so far
     */ data : DocDataType | undefined ; example collection : CollectionFn ; example id : string ; example path : string ; /**
     * Returns the open stream promise of this doc.
     *
     * Returns `null` when there is no open stream.
     *
     * This promise will resolve when `doc().closeStream()` is called, or when the stream closed because of an error.
     */ streaming : ( ) => Promise < void > | null ; /**
     * Close the stream of this doc.
     *
     * Does nothing if there is no open stream.
     */ closeStream : ( ) => void ; see MagnetarFetchAction fetch : MagnetarFetchAction < DocDataType , 'doc' > ; see MagnetarStreamAction stream : MagnetarStreamAction ; see MagnetarInsertAction insert : MagnetarInsertAction < DocDataType > ; see MagnetarWriteAction merge : MagnetarWriteAction < DocDataType > ; see MagnetarWriteAction assign : MagnetarWriteAction < DocDataType > ; see MagnetarWriteAction replace : MagnetarWriteAction < DocDataType > ; see MagnetarDeletePropAction deleteProp : MagnetarDeletePropAction < DocDataType > ; see MagnetarDeleteAction delete : MagnetarDeleteAction ; } ; export function createDocWithContext < DocDataType extends Record < string , any > > ( [ collectionPath , docId ] , moduleConfig , globalConfig , docFn , collectionFn , streamAndFetchPromises ) { const { writeLockMap , fetchPromises , cacheStream , streaming , closeStream } = streamAndFetchPromises ; const id = docId ; const path = [ collectionPath , docId ] . join ( '/' ) ; const collection = ( collectionId , _moduleConfig = { } ) => { return collectionFn ( `${ path }/${ collectionId }` , _moduleConfig ) ; } ; const sharedParams = { collectionPath , _docId : docId , moduleConfig , globalConfig , fetchPromises , writeLockMap , docFn , } ; const actions = { insert : ( handleActionPerStore ( sharedParams , 'insert' , actionNameTypeMap . insert ) as MagnetarInsertAction < DocDataType > ) , merge : ( handleActionPerStore ( sharedParams , 'merge' , actionNameTypeMap . merge ) as MagnetarWriteAction < DocDataType > ) , assign : ( handleActionPerStore ( sharedParams , 'assign' , actionNameTypeMap . assign ) as MagnetarWriteAction < DocDataType > ) , replace : ( handleActionPerStore ( sharedParams , 'replace' , actionNameTypeMap . replace ) as MagnetarWriteAction < DocDataType > ) , deleteProp : ( handleActionPerStore ( sharedParams , 'deleteProp' , actionNameTypeMap . deleteProp ) as MagnetarDeletePropAction < DocDataType > ) , delete : ( handleActionPerStore ( sharedParams , 'delete' , actionNameTypeMap . delete ) as MagnetarDeleteAction ) , fetch : ( handleActionPerStore ( sharedParams , 'fetch' , actionNameTypeMap . fetch ) as MagnetarFetchAction < DocDataType , 'doc' > ) , stream : handleStreamPerStore ( [ collectionPath , docId ] , moduleConfig , globalConfig , actionNameTypeMap . stream , streaming , cacheStream , writeLockMap ) , } ; executeSetupModulePerStore ( globalConfig . stores , [ collectionPath , docId ] , moduleConfig ) ; const moduleInstance = { collection , id : id as string , path , streaming , closeStream , ... actions , } ; /**
     * The data returned by the store specified as 'localStoreName'
     */ const dataProxyHandler = getDataProxyHandler < 'doc' , DocDataType > ( [ collectionPath , docId ] , moduleConfig , globalConfig ) ; return new Proxy ( moduleInstance , dataProxyHandler ) ; }