Skip to content

Commit 33e56a4

Browse files
committed
allow debbug cached collection by name
1 parent fbb47b6 commit 33e56a4

File tree

1 file changed

+62
-57
lines changed

1 file changed

+62
-57
lines changed

app/ui-cached-collection/client/models/CachedCollection.js

+62-57
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import _ from 'underscore';
99

1010
import { callbacks } from '../../../callbacks';
1111
import Notifications from '../../../notifications/client/lib/Notifications';
12+
import { getConfig } from '../../../ui-utils/client/config';
1213

1314
class CachedCollectionManagerClass {
1415
constructor() {
@@ -99,7 +100,7 @@ class CachedCollectionManagerClass {
99100

100101
export const CachedCollectionManager = new CachedCollectionManagerClass();
101102

102-
const debug = false;
103+
const debug = (name) => [getConfig(`debugCachedCollection-${ name }`), getConfig('debugCachedCollection'), getConfig('debug')].includes('true');
103104

104105
const nullLog = function() {};
105106

@@ -134,13 +135,12 @@ export class CachedCollection {
134135
this.useSync = useSync;
135136
this.useCache = useCache;
136137
this.listenChangesForLoggedUsersOnly = listenChangesForLoggedUsersOnly;
137-
this.debug = debug;
138138
this.version = version;
139139
this.userRelated = userRelated;
140140
this.updatedAt = new Date(0);
141141
this.maxCacheTime = maxCacheTime;
142142
this.onSyncData = onSyncData;
143-
this.log = debug ? log : nullLog;
143+
this.log = debug(name) ? log : nullLog;
144144
CachedCollectionManager.register(this);
145145

146146
if (userRelated === true) {
@@ -178,43 +178,49 @@ export class CachedCollection {
178178
}
179179

180180
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);
190186
}
191187

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+
}
198195

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+
}
205203

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+
}
210216
}
211-
}
212-
});
213-
214-
callback(true);
215-
} else {
217+
});
218+
resolve(true);
219+
return callback(true);
220+
}
221+
resolve(false);
216222
callback(false);
217-
}
223+
});
218224
});
219225
}
220226

@@ -375,42 +381,41 @@ export class CachedCollection {
375381

376382
trySync() {
377383
// Wait for an empty queue to load data again and sync
378-
const interval = Meteor.setInterval(() => {
384+
const interval = setInterval(() => {
379385
if (this.sync()) {
380-
Meteor.clearInterval(interval);
386+
clearInterval(interval);
381387
}
382388
}, 200);
383389
}
384390

385-
init() {
391+
async init() {
392+
this.log('init');
386393
if (this.initiated === true) {
387394
return;
388395
}
389396

390397
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+
}
393406

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(() => {
398409
this.trySync();
399-
}
400-
401-
if (this.useSync === true) {
402-
CachedCollectionManager.onReconnect(() => {
403-
this.trySync();
404-
});
405-
}
410+
});
411+
}
406412

407-
if (this.listenChangesForLoggedUsersOnly) {
408-
CachedCollectionManager.onLogin(() => {
409-
this.setupListener();
410-
});
411-
} else {
413+
if (this.listenChangesForLoggedUsersOnly) {
414+
CachedCollectionManager.onLogin(() => {
412415
this.setupListener();
413-
}
414-
});
416+
});
417+
} else {
418+
this.setupListener();
419+
}
415420
}
416421
}

0 commit comments

Comments
 (0)