/
CmsFactory.js
61 lines (48 loc) · 2.05 KB
/
CmsFactory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const { MemoryCache } = require('@thinkpixellab-public/px-long-operations');
const CmsSnapshot = require('./CmsSnapshot');
class CmsFactory {
constructor(source, publishedTTLSeconds) {
this.source = source;
publishedTTLSeconds = publishedTTLSeconds || 15 * 60; // default to 15 minutes
this._cache = new MemoryCache({ defaultTTL: publishedTTLSeconds * 1000 });
}
async refreshPublished() {
const existingPublished = this._cache.get('published', true /* allow expired */);
// fetch the latest version, ignoring cache
var publishedVersion = await this.source.getPublishedReleaseVersion(
true /* ignore cache */
);
// if version hasn't changed, we are fine to continue using existing CMS instance
if (
existingPublished &&
(existingPublished.manifest.version === publishedVersion || !publishedVersion)
) {
return existingPublished;
}
const startTime = Date.now();
let manifest;
if (!publishedVersion) {
// handle new projects where nothing has been published
manifest = { collections: {}, media: {} };
} else {
manifest = await this.source.getPublishedRelease(publishedVersion);
}
const snapshot = new CmsSnapshot(manifest, this.source);
this._cache.set('published', snapshot);
const elapsedSeconds = Math.round((Date.now() - startTime) / 1000);
console.log(`fetched published CMS content: ${snapshot.id} in ${elapsedSeconds} secs.`);
return snapshot;
}
async getPublished() {
return this._cache.ensureLazy('published', () => this.refreshPublished());
}
async getScheduled(scheduledId) {
var manifest = await this.source.getScheduledRelease(scheduledId);
return new CmsSnapshot(manifest, this.source);
}
async getSaved() {
var manifest = await this.source.getSavedRelease();
return new CmsSnapshot(manifest, this.source);
}
}
module.exports = CmsFactory;