import { AffectedItemChange } from './AffectedItemChange' ; import { LocalForageObservableChangeWithPrivateProps } from './LocalForageObservableChangeWithPrivateProps' ; import { LocalForageObservableWrapper } from './LocalForageObservableWrapper' ; import { LocalForageWithObservablePrivateProps } from './LocalForageWithObservablePrivateProps' ; import { equals as isEqual } from './utils' ; export function processObserverList ( list , changeArgs ) { for ( const observableWrapper of list ) { const itemOptions = observableWrapper . options ; if ( ! itemOptions || ( observableWrapper . shouldNotifyAboutAffectedKey ( changeArgs ) && observableWrapper . shouldNotifyAboutMethodCall ( changeArgs . methodName ) && ( ! changeArgs . crossTabNotification || itemOptions . crossTabNotification ) ) ) { observableWrapper . publish ( changeArgs ) ; } } } const getChangeArgsForKey = ( localforageInstance , methodName , key , newValue ) => { return localforageInstance . getItem ( key ) . then ( oldValue => { return { key , methodName , oldValue , newValue , } as LocalForageObservableChange ; } ) ; } ; const getChangeArgsForKeys = ( localforageInstance , methodName , keys , newValue ) => { return Promise . all ( keys . map ( key => getChangeArgsForKey ( localforageInstance , methodName , key , newValue ) ) ) ; } ; export const setOldValues = ( localforageInstance , detectChanges , changeArgs ) : Promise < void > => { if ( ! detectChanges ) { return Promise . resolve ( ) ; } if ( changeArgs . methodName === 'clear' ) { const { observedKeys , allKeysObservers , } = localforageInstance . _observables . changeDetection . reduce ( ( acc , o ) => { if ( ! o . shouldNotifyAboutMethodCall ( 'clear' ) ) { return acc ; } const observesKey = o . options && o . options . key ; if ( ! observesKey ) { acc . allKeysObservers . push ( o ) ; } else if ( acc . observedKeys . indexOf ( observesKey ) < 0 ) { acc . keyObservers . push ( o ) ; acc . observedKeys . push ( observesKey ) ; } return acc ; } , { observedKeys : [ ] as string [ ] , keyObservers : [ ] as LocalForageObservableWrapper [ ] , allKeysObservers : [ ] as LocalForageObservableWrapper [ ] , } ) ; const keysPromise = allKeysObservers . length ? localforageInstance . keys ( ) : Promise . resolve ( observedKeys ) ; return keysPromise . then ( keys => { const affectedItems = keys . map ( key => { return { oldValue : changeArgs . oldValue , newValue : changeArgs . newValue , key , } ; } ) ; let affectedItemsByKey ; return Promise . all ( affectedItems . map ( affectedItem => { return localforageInstance . getItem ( affectedItem . key ) . then ( value => { affectedItem . oldValue = value ; const valueChange = ! isEqual ( affectedItem . oldValue , affectedItem . newValue ) ; if ( valueChange ) { affectedItemsByKey = affectedItemsByKey || { } ; affectedItemsByKey [ affectedItem . key ] = affectedItem ; } } ) ; } ) ) . then ( result => { changeArgs . _affectedItemsByKey = affectedItemsByKey ; } ) ; } ) . then ( ( ) => undefined ) ; } return localforageInstance . getItem ( changeArgs . key ) . then ( function ( value ) { changeArgs . oldValue = value ; } ) ; } ;