musora-content-services 1.2.4 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +5 -0
- package/.prettierrc +8 -0
- package/CHANGELOG.md +11 -0
- package/babel.config.cjs +1 -1
- package/jest.config.js +9 -10
- package/jsdoc.json +17 -12
- package/package.json +2 -1
- package/src/contentMetaData.js +1190 -1131
- package/src/contentTypeConfig.js +492 -387
- package/src/filterBuilder.js +163 -145
- package/src/index.d.ts +227 -237
- package/src/index.js +226 -236
- package/src/services/config.js +12 -12
- package/src/services/contentLikes.js +33 -32
- package/src/services/contentProgress.js +233 -200
- package/src/services/dataContext.js +99 -93
- package/src/services/lastUpdated.js +7 -7
- package/src/services/railcontent.js +368 -364
- package/src/services/sanity.js +975 -955
- package/src/services/userPermissions.js +12 -14
- package/test/contentLikes.test.js +89 -86
- package/test/contentProgress.test.js +229 -236
- package/test/initializeTests.js +54 -51
- package/test/lastUpdated.test.js +20 -18
- package/test/live/contentProgressLive.test.js +135 -137
- package/test/live/railcontentLive.test.js +12 -14
- package/test/localStorageMock.js +16 -16
- package/test/log.js +5 -5
- package/test/sanityQueryService.test.js +857 -821
- package/test/userPermissions.test.js +15 -15
- package/tools/generate-index.cjs +108 -111
- package/.yarnrc.yml +0 -1
|
@@ -1,110 +1,116 @@
|
|
|
1
|
-
import {globalConfig} from
|
|
1
|
+
import { globalConfig } from './config.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Exported functions that are excluded from index generation.
|
|
5
5
|
*
|
|
6
6
|
* @type {string[]}
|
|
7
7
|
*/
|
|
8
|
-
const excludeFromGeneratedIndex = []
|
|
8
|
+
const excludeFromGeneratedIndex = []
|
|
9
9
|
|
|
10
10
|
//These constants need to match MWP UserDataVersionKeyEnum enum
|
|
11
|
-
export const ContentLikesVersionKey = 0
|
|
12
|
-
export const ContentProgressVersionKey = 1
|
|
11
|
+
export const ContentLikesVersionKey = 0
|
|
12
|
+
export const ContentProgressVersionKey = 1
|
|
13
13
|
|
|
14
|
-
let cache = null
|
|
14
|
+
let cache = null
|
|
15
15
|
|
|
16
16
|
export class DataContext {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
context = null
|
|
18
|
+
dataPromise = null
|
|
19
|
+
|
|
20
|
+
constructor(dataVersionKey, fetchDataFunction) {
|
|
21
|
+
this.dataVersionKey = dataVersionKey
|
|
22
|
+
this.fetchDataFunction = fetchDataFunction
|
|
23
|
+
this.localStorageKey = `dataContext_${this.dataVersionKey.toString()}`
|
|
24
|
+
this.localStorageLastUpdatedKey = `dataContext_${this.dataVersionKey.toString()}_lastUpdated`
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async getData() {
|
|
28
|
+
if (!this.dataPromise) {
|
|
29
|
+
this.dataPromise = this.getDataPromise()
|
|
25
30
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
return this.dataPromise
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async getDataPromise() {
|
|
35
|
+
await this.ensureLocalContextLoaded()
|
|
36
|
+
const shouldVerify = await this.shouldVerifyServerVerions()
|
|
37
|
+
|
|
38
|
+
if (!this.context || shouldVerify) {
|
|
39
|
+
let version = this.version()
|
|
40
|
+
let data = await this.fetchData(version)
|
|
41
|
+
if (data?.version !== 'No Change') {
|
|
42
|
+
this.context = data
|
|
43
|
+
cache.setItem(this.localStorageKey, JSON.stringify(data))
|
|
44
|
+
}
|
|
45
|
+
cache.setItem(this.localStorageLastUpdatedKey, new Date().getTime()?.toString())
|
|
32
46
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
this.dataPromise = null
|
|
48
|
+
return this.context.data
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async fetchData(version) {
|
|
52
|
+
return await this.fetchDataFunction(version)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async ensureLocalContextLoaded() {
|
|
56
|
+
if (this.context) return
|
|
57
|
+
this.verifyConfig()
|
|
58
|
+
let localData = globalConfig.isMA
|
|
59
|
+
? await cache.getItem(this.localStorageKey)
|
|
60
|
+
: cache.getItem(this.localStorageKey)
|
|
61
|
+
if (localData) {
|
|
62
|
+
this.context = JSON.parse(localData)
|
|
49
63
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
verifyConfig() {
|
|
67
|
+
if (!cache) {
|
|
68
|
+
cache = globalConfig.localStorage
|
|
69
|
+
if (!cache)
|
|
70
|
+
throw new Error(
|
|
71
|
+
'dataContext: LocalStorage cache not configured in musora content services initializeService.'
|
|
72
|
+
)
|
|
53
73
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async shouldVerifyServerVerions() {
|
|
77
|
+
let lastUpdated = globalConfig.isMA
|
|
78
|
+
? await cache.getItem(this.localStorageLastUpdatedKey)
|
|
79
|
+
: cache.getItem(this.localStorageLastUpdatedKey)
|
|
80
|
+
if (!lastUpdated) return false
|
|
81
|
+
const verifyServerTime = 10000 //10 s
|
|
82
|
+
return new Date().getTime() - lastUpdated > verifyServerTime
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
clearCache() {
|
|
86
|
+
this.clearContext()
|
|
87
|
+
cache.removeItem(this.localStorageKey)
|
|
88
|
+
cache.removeItem(this.localStorageLastUpdatedKey)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
clearContext() {
|
|
92
|
+
this.context = null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async update(localUpdateFunction, serverUpdateFunction) {
|
|
96
|
+
await this.ensureLocalContextLoaded()
|
|
97
|
+
if (this.context) {
|
|
98
|
+
await localUpdateFunction(this.context)
|
|
99
|
+
if (this.context) this.context.version++
|
|
100
|
+
let data = JSON.stringify(this.context)
|
|
101
|
+
cache.setItem(this.localStorageKey, data)
|
|
102
|
+
cache.setItem(this.localStorageLastUpdatedKey, new Date().getTime().toString())
|
|
62
103
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return (new Date().getTime() - lastUpdated) > verifyServerTime;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
clearCache() {
|
|
79
|
-
this.clearContext();
|
|
80
|
-
cache.removeItem(this.localStorageKey);
|
|
81
|
-
cache.removeItem(this.localStorageLastUpdatedKey);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
clearContext() {
|
|
85
|
-
this.context = null;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async update(localUpdateFunction, serverUpdateFunction) {
|
|
89
|
-
await this.ensureLocalContextLoaded();
|
|
90
|
-
if (this.context) {
|
|
91
|
-
await localUpdateFunction(this.context);
|
|
92
|
-
if (this.context) this.context.version++;
|
|
93
|
-
let data = JSON.stringify(this.context);
|
|
94
|
-
cache.setItem(this.localStorageKey, data);
|
|
95
|
-
cache.setItem(this.localStorageLastUpdatedKey, new Date().getTime().toString());
|
|
96
|
-
}
|
|
97
|
-
const updatePromise = serverUpdateFunction();
|
|
98
|
-
updatePromise.then((response) => {
|
|
99
|
-
if (response?.version !== this.version()) {
|
|
100
|
-
this.clearCache();
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
return updatePromise;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
version() {
|
|
107
|
-
return this.context?.version ?? -1;
|
|
108
|
-
}
|
|
109
|
-
|
|
104
|
+
const updatePromise = serverUpdateFunction()
|
|
105
|
+
updatePromise.then((response) => {
|
|
106
|
+
if (response?.version !== this.version()) {
|
|
107
|
+
this.clearCache()
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
return updatePromise
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
version() {
|
|
114
|
+
return this.context?.version ?? -1
|
|
115
|
+
}
|
|
110
116
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {globalConfig} from
|
|
1
|
+
import { globalConfig } from './config.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Exported functions that are excluded from index generation.
|
|
5
5
|
*
|
|
6
6
|
* @type {string[]}
|
|
7
7
|
*/
|
|
8
|
-
const excludeFromGeneratedIndex = ['wasLastUpdateOlderThanXSeconds', 'setLastUpdatedTime']
|
|
8
|
+
const excludeFromGeneratedIndex = ['wasLastUpdateOlderThanXSeconds', 'setLastUpdatedTime']
|
|
9
9
|
export function wasLastUpdateOlderThanXSeconds(seconds, key) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
let lastUpdated = globalConfig.localStorage.getItem(key)
|
|
11
|
+
if (!lastUpdated) return false
|
|
12
|
+
const verifyServerTime = seconds * 1000
|
|
13
|
+
return new Date().getTime() - lastUpdated > verifyServerTime
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export function setLastUpdatedTime(key) {
|
|
17
|
-
|
|
17
|
+
globalConfig.localStorage.setItem(key, new Date().getTime()?.toString())
|
|
18
18
|
}
|