import { diffGunCRDT , mergeGraph } from '@chaingun/crdt' ; import { GunGraphAdapter , GunGraphData , GunNode } from '@chaingun/types' ; import { clone , curry } from 'ramda' ; const DEFAULT_OPTS = { diffFn : diffGunCRDT , mergeFn : mergeGraph } ; interface MemoryAdapterOpts { readonly diffFn ? : typeof diffGunCRDT ; readonly mergeFn ? : typeof mergeGraph ; readonly direct ? : boolean ; } const getSync = curry ( ( opts , graph , soul ) : GunNode | null => ( opts . direct ? graph [ soul ] : clone ( graph [ soul ] ) ) || null ) ; const get = curry ( ( opts , graph , soul ) : Promise < GunNode | null > => Promise . resolve ( getSync ( opts , graph , soul ) ) ) ; const putSync = curry ( ( { diffFn = DEFAULT_OPTS . diffFn , mergeFn = DEFAULT_OPTS . mergeFn } , graph , graphData ) => { const diff = diffFn ( graphData , graph ) ; if ( diff ) { mergeFn ( graph , diff , 'mutable' ) ; } return diff || null ; } ) ; const put = curry ( ( opts , graph , graphData ) : Promise < GunGraphData | null > => Promise . resolve ( putSync ( opts , graph , graphData ) ) ) ; export function createMemoryAdapter ( opts = DEFAULT_OPTS ) { const graph = { } ; return { get : get ( opts , graph ) , getSync : getSync ( opts , graph ) , put : put ( opts , graph ) , putSync : putSync ( opts , graph ) } ; }