import { DocMeta } from '../metadata/DocMeta' ; import { IDocInfo } from 'polar-shared/src/metadata/IDocInfo' ; import { DocUUID } from './CloudAwareDatastore' ; import { isPresent } from 'polar-shared/src/Preconditions' ; import { DocMetaMutation } from './Datastore' ; import { UUIDs } from '../metadata/UUIDs' ; import { IDocMeta } from "polar-shared/src/metadata/IDocMeta" ; /**
 * The DocComparisonIndex allows us to detect which documents are local already
 * so that when we receive document from the cloud datastore we can decide
 * that we do not need to replicate it locally.
 */ export class DocMetaComparisonIndex { private readonly backing : { [ fingerprint ] : DocUUID ; } = { } ; public contains ( fingerprint ) { return isPresent ( this . backing [ fingerprint ] ) ; } public get ( fingerprint ) { return this . backing [ fingerprint ] ; } public remove ( fingerprint ) { delete this . backing [ fingerprint ] ; } public updateUsingDocMeta ( docMeta ) { this . backing [ docMeta . docInfo . fingerprint ] = { fingerprint : docMeta . docInfo . fingerprint , uuid : docMeta . docInfo . uuid } ; } public updateUsingDocInfo ( docInfo ) { this . backing [ docInfo . fingerprint ] = { fingerprint : docInfo . fingerprint , uuid : docInfo . uuid } ; } param docMetaMutation param docInfo public handleDocMetaMutation ( docMetaMutation , docInfo ) { const mutationType = docMetaMutation . mutationType ; let doUpdated = false ; const docComparison = this . get ( docInfo . fingerprint ) ; if ( ! docComparison ) { doUpdated = true ; } if ( docComparison ) { if ( UUIDs . compare ( docComparison . uuid , docInfo . uuid ) < 0 ) { doUpdated = true ; } else { } } if ( doUpdated ) { this . updateUsingDocInfo ( docInfo ) ; return true ; } if ( mutationType === 'deleted' && this . get ( docInfo . fingerprint ) ) { this . remove ( docInfo . fingerprint ) ; return true ; } return false ; } }