import { AbstractDatastore , Datastore , DatastoreID } from './Datastore' ; import { WritableBinaryMetaDatastore } from './Datastore' ; import { DocMetaSnapshotEventListener } from './Datastore' ; import { DatastoreCapabilities } from './Datastore' ; import { ErrorListener } from './Datastore' ; import { DatastoreInitOpts } from './Datastore' ; import { DatastoreOverview } from './Datastore' ; import { SnapshotResult } from './Datastore' ; import { InitResult } from './Datastore' ; import { DeleteResult } from './Datastore' ; import { BinaryFileData } from './Datastore' ; import { WriteFileOpts } from './Datastore' ; import { WriteOpts } from './Datastore' ; import { Logger } from 'polar-shared/src/logger/Logger' ; import { DocMetaRef } from './DocMetaRef' ; import { DocMetaFileRef } from './DocMetaRef' ; import { IDocInfo } from 'polar-shared/src/metadata/IDocInfo' ; import { DatastoreMutation } from './DatastoreMutation' ; import { Backend } from 'polar-shared/src/datastore/Backend' ; import { DocFileMeta } from 'polar-shared/src/datastore/DocFileMeta' ; import { FirebaseDatastore } from './FirebaseDatastore' ; import { FirebaseDocMetaID } from './FirebaseDatastore' ; import { DocMetas } from '../metadata/DocMetas' ; import { DocMeta } from '../metadata/DocMeta' ; import { BackendFileRefs } from './BackendFileRefs' ; import { IDocMeta } from "polar-shared/src/metadata/IDocMeta" ; import { BackendFileRef } from "polar-shared/src/datastore/BackendFileRef" ; import { FileRef } from "polar-shared/src/datastore/FileRef" ; import { GetFileOpts , NetworkLayers } from "polar-shared/src/datastore/IDatastore" ; const log = Logger . create ( ) ; export class SharingDatastore extends AbstractDatastore implements Datastore , WritableBinaryMetaDatastore { public readonly id : DatastoreID ; private readonly delegate = new FirebaseDatastore ( ) ; private docMetaData : string | null = null ; private docMeta : IDocMeta | undefined ; private docMetaRefs : DocMetaRef [ ] = [ ] ; private backendFileRefs : ReadonlyArray < BackendFileRef > = [ ] ; public constructor ( private readonly docMetaID , private readonly fingerprint ) { super ( ) ; this . id = 'shared' ; } public async init ( errorListener ? , opts ? ) { try { opts = { ... opts , noInitialSnapshot : true } ; await this . delegate . init ( errorListener , opts ) ; this . docMetaData = await this . delegate . getDocMetaDirectly ( this . docMetaID ) ; if ( this . docMetaData ) { this . docMeta = DocMetas . deserialize ( this . docMetaData ! , this . fingerprint ) ; this . docMetaRefs = [ { fingerprint : this . fingerprint , docMetaProvider : ( ) => Promise . resolve ( this . docMeta ! ) } ] ; this . backendFileRefs = BackendFileRefs . toBackendFileRefs ( this . docMeta ) ; } return { } ; } catch ( e ) { log . error ( "Unable to init datastore: " , e ) ; throw e ; } } public addDocMetaSnapshotEventListener ( docMetaSnapshotEventListener ) { } public capabilities ( ) { return { networkLayers : NetworkLayers . WEB , permission : { mode : 'ro' } } ; } public async contains ( fingerprint ) { return this . fingerprint === fingerprint ; } public async deactivate ( ) { } public async getDocMeta ( fingerprint ) { return this . docMetaData ; } public async getDocMetaRefs ( ) { return this . docMetaRefs ; } public async overview ( ) { return await this . delegate . overview ( ) ; } public async snapshot ( docMetaSnapshotEventListener , errorListener ? ) { throw new Error ( "Not supported" ) ; } public async stop ( ) { } public async write ( fingerprint , data , docInfo , opts = { } ) { throw new Error ( "Not supported" ) ; } public async containsFile ( backend , ref ) { const backendFileRef = { backend , ... ref } ; return this . backendFileRefs . filter ( current => BackendFileRefs . equals ( current , backendFileRef ) ) . length > 0 ; } public async delete ( docMetaFileRef , datastoreMutation ? ) { throw new Error ( "Not supported" ) ; } public async deleteFile ( backend , ref ) { throw new Error ( "Not supported" ) ; } public getFile ( backend , ref , opts ? ) { throw new Error ( "Not implemented yet" ) ; } public async writeFile ( backend , ref , data , opts ? ) { throw new Error ( "Not supported" ) ; } }