1- import PouchDB from 'pouchdb-browser'
21import fromPairs from 'lodash/fromPairs'
32import forEach from 'lodash/forEach'
43import get from 'lodash/get'
@@ -10,9 +9,9 @@ import { QueryDefinition } from 'cozy-client'
109
1110import Loop from './loop'
1211import logger from './logger'
12+ import { platformWeb } from './platformWeb'
1313import { fetchRemoteLastSequence } from './remote'
1414import { startReplication } from './startReplication'
15- import * as localStorage from './localStorage'
1615import { getDatabaseName } from './utils'
1716
1817const DEFAULT_DELAY = 30 * 1000
@@ -35,20 +34,33 @@ const getQueryAlias = query => {
3534class PouchManager {
3635 constructor ( doctypes , options ) {
3736 this . options = options
38- const pouchPlugins = get ( options , 'pouch.plugins' , [ ] )
39- const pouchOptions = get ( options , 'pouch.options' , { } )
37+ this . doctypes = doctypes
4038
41- forEach ( pouchPlugins , plugin => PouchDB . plugin ( plugin ) )
39+ /**
40+ * @type {import("./types").PouchLocalStorage }
41+ */
42+ this . storage = options . platform ?. storage || platformWeb . storage
43+ this . PouchDB = options . platform ?. pouchEngine || platformWeb . pouchEngine
44+ }
45+
46+ async init ( ) {
47+ const pouchPlugins = get ( this . options , 'pouch.plugins' , [ ] )
48+ const pouchOptions = get ( this . options , 'pouch.options' , { } )
49+
50+ forEach ( pouchPlugins , plugin => this . PouchDB . plugin ( plugin ) )
4251 this . pouches = fromPairs (
43- doctypes . map ( doctype => [
52+ this . doctypes . map ( doctype => [
4453 doctype ,
45- new PouchDB ( getDatabaseName ( options . prefix , doctype ) , pouchOptions )
54+ new this . PouchDB (
55+ getDatabaseName ( this . options . prefix , doctype ) ,
56+ pouchOptions
57+ )
4658 ] )
4759 )
48- this . syncedDoctypes = localStorage . getPersistedSyncedDoctypes ( )
49- this . warmedUpQueries = localStorage . getPersistedWarmedUpQueries ( )
50- this . getReplicationURL = options . getReplicationURL
51- this . doctypesReplicationOptions = options . doctypesReplicationOptions || { }
60+ this . syncedDoctypes = await this . storage . getPersistedSyncedDoctypes ( )
61+ this . warmedUpQueries = await this . storage . getPersistedWarmedUpQueries ( )
62+ this . getReplicationURL = this . options . getReplicationURL
63+ this . doctypesReplicationOptions = this . options . doctypesReplicationOptions || { }
5264 this . listenerLaunched = false
5365
5466 // We must ensure databases exist on the remote before
@@ -85,13 +97,13 @@ class PouchManager {
8597 }
8698 }
8799
88- destroy ( ) {
100+ async destroy ( ) {
89101 this . stopReplicationLoop ( )
90102 this . removeListeners ( )
91- this . clearSyncedDoctypes ( )
92- this . clearWarmedUpQueries ( )
93- localStorage . destroyAllDoctypeLastSequence ( )
94- localStorage . destroyAllLastReplicatedDocID ( )
103+ await this . clearSyncedDoctypes ( )
104+ await this . clearWarmedUpQueries ( )
105+ await this . storage . destroyAllDoctypeLastSequence ( )
106+ await this . storage . destroyAllLastReplicatedDocID ( )
95107
96108 return Promise . all (
97109 Object . values ( this . pouches ) . map ( pouch => pouch . destroy ( ) )
@@ -182,9 +194,9 @@ class PouchManager {
182194 // Before the first replication, get the last remote sequence,
183195 // which will be used as a checkpoint for the next replication
184196 const lastSeq = await fetchRemoteLastSequence ( getReplicationURL ( ) )
185- localStorage . persistDoctypeLastSequence ( doctype , lastSeq )
197+ await this . storage . persistDoctypeLastSequence ( doctype , lastSeq )
186198 } else {
187- seq = localStorage . getDoctypeLastSequence ( doctype )
199+ seq = await this . storage . getDoctypeLastSequence ( doctype )
188200 }
189201
190202 const replicationOptions = get (
@@ -203,15 +215,16 @@ class PouchManager {
203215 const res = await startReplication (
204216 pouch ,
205217 replicationOptions ,
206- getReplicationURL
218+ getReplicationURL ,
219+ this . storage
207220 )
208221 if ( seq ) {
209222 // We only need the sequence for the second replication, as PouchDB
210223 // will use a local checkpoint for the next runs.
211- localStorage . destroyDoctypeLastSequence ( doctype )
224+ await this . storage . destroyDoctypeLastSequence ( doctype )
212225 }
213226
214- this . updateSyncInfo ( doctype )
227+ await this . updateSyncInfo ( doctype )
215228 this . checkToWarmupDoctype ( doctype , replicationOptions )
216229 if ( this . options . onDoctypeSyncEnd ) {
217230 this . options . onDoctypeSyncEnd ( doctype )
@@ -273,9 +286,9 @@ class PouchManager {
273286 return this . pouches [ doctype ]
274287 }
275288
276- updateSyncInfo ( doctype ) {
289+ async updateSyncInfo ( doctype ) {
277290 this . syncedDoctypes [ doctype ] = { date : new Date ( ) . toISOString ( ) }
278- localStorage . persistSyncedDoctypes ( this . syncedDoctypes )
291+ await this . storage . persistSyncedDoctypes ( this . syncedDoctypes )
279292 }
280293
281294 getSyncInfo ( doctype ) {
@@ -287,9 +300,9 @@ class PouchManager {
287300 return info ? ! ! info . date : false
288301 }
289302
290- clearSyncedDoctypes ( ) {
303+ async clearSyncedDoctypes ( ) {
291304 this . syncedDoctypes = { }
292- localStorage . destroySyncedDoctypes ( )
305+ await this . storage . destroySyncedDoctypes ( )
293306 }
294307
295308 async warmupQueries ( doctype , queries ) {
@@ -304,7 +317,7 @@ class PouchManager {
304317 }
305318 } )
306319 )
307- localStorage . persistWarmedUpQueries ( this . warmedUpQueries )
320+ await this . storage . persistWarmedUpQueries ( this . warmedUpQueries )
308321 logger . log ( 'PouchManager: warmupQueries for ' + doctype + ' are done' )
309322 } catch ( err ) {
310323 logger . error (
@@ -324,18 +337,18 @@ class PouchManager {
324337 }
325338 }
326339
327- areQueriesWarmedUp ( doctype , queries ) {
328- const persistWarmedUpQueries = localStorage . getPersistedWarmedUpQueries ( )
340+ async areQueriesWarmedUp ( doctype , queries ) {
341+ const persistWarmedUpQueries = await this . storage . getPersistedWarmedUpQueries ( )
329342 return queries . every (
330343 query =>
331344 persistWarmedUpQueries [ doctype ] &&
332345 persistWarmedUpQueries [ doctype ] . includes ( getQueryAlias ( query ) )
333346 )
334347 }
335348
336- clearWarmedUpQueries ( ) {
349+ async clearWarmedUpQueries ( ) {
337350 this . warmedUpQueries = { }
338- localStorage . destroyWarmedUpQueries ( )
351+ await this . storage . destroyWarmedUpQueries ( )
339352 }
340353}
341354
0 commit comments