musora-content-services 1.3.7 → 1.3.8

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.
Files changed (36) hide show
  1. package/.github/workflows/node.js.yml +0 -0
  2. package/CHANGELOG.md +2 -0
  3. package/README.md +0 -0
  4. package/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
  5. package/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
  6. package/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
  7. package/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
  8. package/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
  9. package/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
  10. package/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
  11. package/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
  12. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
  13. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +0 -0
  14. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
  15. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
  16. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
  17. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
  18. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +0 -0
  19. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
  20. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
  21. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
  22. package/docs/scripts/collapse.js +0 -0
  23. package/docs/scripts/commonNav.js +0 -0
  24. package/docs/scripts/linenumber.js +0 -0
  25. package/docs/scripts/nav.js +0 -0
  26. package/docs/scripts/polyfill.js +0 -0
  27. package/docs/scripts/prettify/Apache-License-2.0.txt +0 -0
  28. package/docs/scripts/prettify/lang-css.js +0 -0
  29. package/docs/scripts/prettify/prettify.js +0 -0
  30. package/docs/scripts/search.js +0 -0
  31. package/docs/styles/jsdoc.css +0 -0
  32. package/docs/styles/prettify.css +0 -0
  33. package/package.json +1 -1
  34. package/src/filterBuilder.js +11 -11
  35. package/src/services/dataContext.js +26 -5
  36. package/test/dataContext.test.js +38 -0
File without changes
package/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.3.8](https://github.com/railroadmedia/musora-content-services/compare/v1.3.7...v1.3.8) (2025-02-21)
6
+
5
7
  ### [1.3.7](https://github.com/railroadmedia/musora-content-services/compare/v1.3.6...v1.3.7) (2025-02-20)
6
8
 
7
9
  ### [1.3.6](https://github.com/railroadmedia/musora-content-services/compare/v1.3.5...v1.3.6) (2025-02-18)
package/README.md CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "musora-content-services",
3
- "version": "1.3.7",
3
+ "version": "1.3.8",
4
4
  "description": "A package for Musoras content services ",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -56,6 +56,15 @@ export class FilterBuilder {
56
56
  return filter
57
57
  }
58
58
 
59
+ _getRoundedTime(){
60
+ // We need to set the published on filter date to be a round time so that it doesn't bypass the query cache
61
+ // with every request by changing the filter date every second. I've set it to one minute past the current hour
62
+ // because publishing usually publishes content on the hour exactly which means it should still skip the cache
63
+ // when the new content is available.
64
+ const now = new Date();
65
+ return new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), 1);
66
+ }
67
+
59
68
  _applyContentStatuses() {
60
69
  // This must be run before _applyPublishDateRestrictions()
61
70
  if (this.bypassStatuses) return this
@@ -89,7 +98,7 @@ export class FilterBuilder {
89
98
  ) {
90
99
  // we must pull in future content here, otherwise we'll restrict on content this is published in the past and remove any scheduled content
91
100
  this.pullFutureContent = true
92
- const now = new Date().toISOString()
101
+ const now = this._getRoundedTime().toISOString();
93
102
  let statuses = [...this.availableContentStatuses]
94
103
  statuses.splice(statuses.indexOf(this.STATUS_SCHEDULED), 1)
95
104
  this._andWhere(
@@ -121,16 +130,7 @@ export class FilterBuilder {
121
130
 
122
131
  _applyPublishingDateRestrictions() {
123
132
  if (this.bypassPublishedDateRestriction) return this
124
- let now = new Date()
125
-
126
- // We need to set the published on filter date to be a round time so that it doesn't bypass the query cache
127
- // with every request by changing the filter date every second. I've set it to one minute past the current hour
128
- // because publishing usually publishes content on the hour exactly which means it should still skip the cache
129
- // when the new content is available.
130
- // Round to the start of the current hour
131
- const roundedDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours())
132
-
133
- now = roundedDate.toISOString()
133
+ const now = this._getRoundedTime().toISOString();
134
134
 
135
135
  if (this.getFutureContentOnly) {
136
136
  this._andWhere(`${this.prefix}published_on >= '${now}'`)
@@ -17,6 +17,23 @@ export class DataContext {
17
17
  context = null
18
18
  dataPromise = null
19
19
 
20
+ /**
21
+ * Verify current cached data is on the correct version
22
+ *
23
+ * @param {int} dataVersionKey - Data version key from the back end
24
+ * @param {int} currentVersion - Current version of the data on the back end
25
+ * */
26
+ static async verifyLocalData(dataVersionKey, currentVersion) {
27
+ const tempContext = new DataContext(dataVersionKey, null)
28
+ await tempContext.ensureLocalContextLoaded()
29
+
30
+ if (currentVersion !== tempContext.version()) {
31
+ tempContext.clearCache()
32
+ } else {
33
+ tempContext.setLastUpdatedTime()
34
+ }
35
+ }
36
+
20
37
  constructor(dataVersionKey, fetchDataFunction) {
21
38
  this.dataVersionKey = dataVersionKey
22
39
  this.fetchDataFunction = fetchDataFunction
@@ -33,7 +50,7 @@ export class DataContext {
33
50
 
34
51
  async getDataPromise() {
35
52
  await this.ensureLocalContextLoaded()
36
- const shouldVerify = await this.shouldVerifyServerVerions()
53
+ const shouldVerify = await this.shouldVerifyServerVersions()
37
54
 
38
55
  if (!this.context || shouldVerify) {
39
56
  let version = this.version()
@@ -42,7 +59,7 @@ export class DataContext {
42
59
  this.context = data
43
60
  cache.setItem(this.localStorageKey, JSON.stringify(data))
44
61
  }
45
- cache.setItem(this.localStorageLastUpdatedKey, new Date().getTime()?.toString())
62
+ this.setLastUpdatedTime()
46
63
  }
47
64
  this.dataPromise = null
48
65
  return this.context.data
@@ -73,11 +90,11 @@ export class DataContext {
73
90
  }
74
91
  }
75
92
 
76
- async shouldVerifyServerVerions() {
93
+ async shouldVerifyServerVersions() {
77
94
  let lastUpdated = globalConfig.isMA
78
95
  ? await cache.getItem(this.localStorageLastUpdatedKey)
79
96
  : cache.getItem(this.localStorageLastUpdatedKey)
80
- if (!lastUpdated) return false
97
+ if (!lastUpdated) return true
81
98
  const verifyServerTime = 10000 //10 s
82
99
  return new Date().getTime() - lastUpdated > verifyServerTime
83
100
  }
@@ -92,6 +109,10 @@ export class DataContext {
92
109
  this.context = null
93
110
  }
94
111
 
112
+ setLastUpdatedTime() {
113
+ cache.setItem(this.localStorageLastUpdatedKey, new Date().getTime().toString())
114
+ }
115
+
95
116
  async update(localUpdateFunction, serverUpdateFunction) {
96
117
  await this.ensureLocalContextLoaded()
97
118
  if (this.context) {
@@ -99,7 +120,7 @@ export class DataContext {
99
120
  if (this.context) this.context.version++
100
121
  let data = JSON.stringify(this.context)
101
122
  cache.setItem(this.localStorageKey, data)
102
- cache.setItem(this.localStorageLastUpdatedKey, new Date().getTime().toString())
123
+ this.setLastUpdatedTime()
103
124
  }
104
125
  const updatePromise = serverUpdateFunction()
105
126
  updatePromise.then((response) => {
@@ -0,0 +1,38 @@
1
+ import { initializeTestService } from './initializeTests'
2
+ import { DataContext } from '../src/services/dataContext.js'
3
+
4
+ describe('dataContext', function () {
5
+ let mock = null
6
+ let testVersion = 1
7
+ const dataVersionKey = 1
8
+ const dataContext = new DataContext(dataVersionKey, null)
9
+
10
+ beforeEach(() => {
11
+ initializeTestService()
12
+ mock = jest.spyOn(dataContext, 'fetchData')
13
+ mock.mockImplementation(() =>
14
+ JSON.parse(`{"version":${testVersion},"data":[308516,308515,308514,308518]}`)
15
+ )
16
+ })
17
+
18
+ test('verifyLocalData', async () => {
19
+ //force load data into context and verify version 1 loaded
20
+ await dataContext.getData()
21
+ expect(dataContext.version()).toBe(1)
22
+
23
+ //increment source version and verify data context still uses old version
24
+ testVersion++
25
+ await dataContext.getData()
26
+ expect(dataContext.version()).toBe(1)
27
+
28
+ //verifyLocalData with old source version and verify context still uses old version
29
+ await DataContext.verifyLocalData(1, 1)
30
+ await dataContext.getData()
31
+ expect(dataContext.version()).toBe(1)
32
+
33
+ //verifyLocalData with new source version and verify context loads new version
34
+ await DataContext.verifyLocalData(1, 2)
35
+ await dataContext.getData()
36
+ expect(dataContext.version()).toBe(2)
37
+ })
38
+ })