import { PluginFetchAction , FetchResponse , DoOnFetch , PluginFetchActionPayload , DocMetadata , } from '@magnetarjs/core' ; import { SimpleStoreModuleConfig , SimpleStoreOptions } from '../CreatePlugin' ; import { insertActionFactory } from './insert' ; export function fetchActionFactory ( data , simpleStoreOptions ) { return function ( { payload , collectionPath , docId , actionConfig , pluginModuleConfig , } ) : FetchResponse | DoOnFetch { const optimisticFetch = ! payload || ! Object . hasOwnProperty . call ( payload || { } , 'force' ) || payload ?. force === false ; if ( optimisticFetch ) { const collectionData = data [ collectionPath ] ; if ( ! docId && collectionData . size > 0 ) { const localDocs = [ ... collectionData . entries ( ) ] . map ( ( [ _docId , data ] ) => ( { data , exists : 'unknown' , id : _docId , } ) ) ; const fetchResponse = { docs : localDocs } ; return fetchResponse ; } if ( docId ) { const localDoc = collectionData . get ( docId ) ; if ( localDoc ) { const fetchResponse = { docs : [ { data : localDoc , exists : 'unknown' , id : docId , } , ] , } ; return fetchResponse ; } if ( ! localDoc ) { } } } const doOnFetchAction = ( _payload , meta ) : void => { if ( _payload === undefined ) return ; insertActionFactory ( data , simpleStoreOptions ) ( { payload : _payload , collectionPath , docId , actionConfig , pluginModuleConfig , } ) ; } ; return doOnFetchAction ; } ; }