@@ -9,6 +9,7 @@ import _ from 'underscore';
9
9
10
10
import { callbacks } from '../../../callbacks' ;
11
11
import Notifications from '../../../notifications/client/lib/Notifications' ;
12
+ import { getConfig } from '../../../ui-utils/client/config' ;
12
13
13
14
class CachedCollectionManagerClass {
14
15
constructor ( ) {
@@ -99,7 +100,7 @@ class CachedCollectionManagerClass {
99
100
100
101
export const CachedCollectionManager = new CachedCollectionManagerClass ( ) ;
101
102
102
- const debug = false ;
103
+ const debug = ( name ) => [ getConfig ( `debugCachedCollection- ${ name } ` ) , getConfig ( 'debugCachedCollection' ) , getConfig ( 'debug' ) ] . includes ( 'true' ) ;
103
104
104
105
const nullLog = function ( ) { } ;
105
106
@@ -134,13 +135,12 @@ export class CachedCollection {
134
135
this . useSync = useSync ;
135
136
this . useCache = useCache ;
136
137
this . listenChangesForLoggedUsersOnly = listenChangesForLoggedUsersOnly ;
137
- this . debug = debug ;
138
138
this . version = version ;
139
139
this . userRelated = userRelated ;
140
140
this . updatedAt = new Date ( 0 ) ;
141
141
this . maxCacheTime = maxCacheTime ;
142
142
this . onSyncData = onSyncData ;
143
- this . log = debug ? log : nullLog ;
143
+ this . log = debug ( name ) ? log : nullLog ;
144
144
CachedCollectionManager . register ( this ) ;
145
145
146
146
if ( userRelated === true ) {
@@ -178,43 +178,49 @@ export class CachedCollection {
178
178
}
179
179
180
180
loadFromCache ( callback = ( ) => { } ) {
181
- if ( this . useCache === false ) {
182
- return callback ( false ) ;
183
- }
184
-
185
- localforage . getItem ( this . name , ( error , data ) => {
186
- if ( data && ( data . version < this . version || data . token !== this . getToken ( ) || this . getToken ( ) === undefined ) ) {
187
- this . clearCache ( ) ;
188
- callback ( false ) ;
189
- return ;
181
+ return new Promise ( ( resolve ) => {
182
+ this . log ( 'loadFromCache' ) ;
183
+ if ( this . useCache === false ) {
184
+ resolve ( false ) ;
185
+ return callback ( false ) ;
190
186
}
191
187
192
- const now = new Date ( ) ;
193
- if ( data && now - data . updatedAt >= 1000 * this . maxCacheTime ) {
194
- this . clearCache ( ) ;
195
- callback ( false ) ;
196
- return ;
197
- }
188
+ localforage . getItem ( this . name , ( error , data ) => {
189
+ if ( data && ( data . version < this . version || data . token !== this . getToken ( ) || this . getToken ( ) === undefined ) ) {
190
+ this . clearCache ( ) ;
191
+ resolve ( false ) ;
192
+ callback ( false ) ;
193
+ return ;
194
+ }
198
195
199
- if ( data && data . records && data . records . length > 0 ) {
200
- this . log ( `${ data . records . length } records loaded from cache` ) ;
201
- data . records . forEach ( ( record ) => {
202
- callbacks . run ( `cachedCollection-loadFromCache-${ this . name } ` , record ) ;
203
- record . __cache__ = true ;
204
- this . collection . upsert ( { _id : record . _id } , _ . omit ( record , '_id' ) ) ;
196
+ const now = new Date ( ) ;
197
+ if ( data && now - data . updatedAt >= 1000 * this . maxCacheTime ) {
198
+ this . clearCache ( ) ;
199
+ resolve ( false ) ;
200
+ callback ( false ) ;
201
+ return ;
202
+ }
205
203
206
- if ( record . _updatedAt ) {
207
- const _updatedAt = new Date ( record . _updatedAt ) ;
208
- if ( _updatedAt > this . updatedAt ) {
209
- this . updatedAt = _updatedAt ;
204
+ if ( data && data . records && data . records . length > 0 ) {
205
+ this . log ( `${ data . records . length } records loaded from cache` ) ;
206
+ data . records . forEach ( ( record ) => {
207
+ callbacks . run ( `cachedCollection-loadFromCache-${ this . name } ` , record ) ;
208
+ record . __cache__ = true ;
209
+ this . collection . upsert ( { _id : record . _id } , _ . omit ( record , '_id' ) ) ;
210
+
211
+ if ( record . _updatedAt ) {
212
+ const _updatedAt = new Date ( record . _updatedAt ) ;
213
+ if ( _updatedAt > this . updatedAt ) {
214
+ this . updatedAt = _updatedAt ;
215
+ }
210
216
}
211
- }
212
- } ) ;
213
-
214
- callback ( true ) ;
215
- } else {
217
+ } ) ;
218
+ resolve ( true ) ;
219
+ return callback ( true ) ;
220
+ }
221
+ resolve ( false ) ;
216
222
callback ( false ) ;
217
- }
223
+ } ) ;
218
224
} ) ;
219
225
}
220
226
@@ -375,42 +381,41 @@ export class CachedCollection {
375
381
376
382
trySync ( ) {
377
383
// Wait for an empty queue to load data again and sync
378
- const interval = Meteor . setInterval ( ( ) => {
384
+ const interval = setInterval ( ( ) => {
379
385
if ( this . sync ( ) ) {
380
- Meteor . clearInterval ( interval ) ;
386
+ clearInterval ( interval ) ;
381
387
}
382
388
} , 200 ) ;
383
389
}
384
390
385
- init ( ) {
391
+ async init ( ) {
392
+ this . log ( 'init' ) ;
386
393
if ( this . initiated === true ) {
387
394
return ;
388
395
}
389
396
390
397
this . initiated = true ;
391
- this . loadFromCache ( ( cacheLoaded ) => {
392
- this . ready . set ( cacheLoaded ) ;
398
+ const cacheLoaded = await this . loadFromCache ( ) ;
399
+ this . ready . set ( cacheLoaded ) ;
400
+ if ( cacheLoaded === false ) {
401
+ // If there is no cache load data immediately
402
+ this . loadFromServerAndPopulate ( ) ;
403
+ } else if ( this . useSync === true ) {
404
+ this . trySync ( ) ;
405
+ }
393
406
394
- if ( cacheLoaded === false ) {
395
- // If there is no cache load data immediately
396
- this . loadFromServerAndPopulate ( ) ;
397
- } else if ( this . useSync === true ) {
407
+ if ( this . useSync === true ) {
408
+ CachedCollectionManager . onReconnect ( ( ) => {
398
409
this . trySync ( ) ;
399
- }
400
-
401
- if ( this . useSync === true ) {
402
- CachedCollectionManager . onReconnect ( ( ) => {
403
- this . trySync ( ) ;
404
- } ) ;
405
- }
410
+ } ) ;
411
+ }
406
412
407
- if ( this . listenChangesForLoggedUsersOnly ) {
408
- CachedCollectionManager . onLogin ( ( ) => {
409
- this . setupListener ( ) ;
410
- } ) ;
411
- } else {
413
+ if ( this . listenChangesForLoggedUsersOnly ) {
414
+ CachedCollectionManager . onLogin ( ( ) => {
412
415
this . setupListener ( ) ;
413
- }
414
- } ) ;
416
+ } ) ;
417
+ } else {
418
+ this . setupListener ( ) ;
419
+ }
415
420
}
416
421
}
0 commit comments