mirror of
https://github.com/acid-info/logos-press-engine.git
synced 2025-02-23 22:58:08 +00:00
refactor: unbody service initial data loader
This commit is contained in:
parent
89494566b3
commit
8a27d9e977
@ -1,5 +1,4 @@
|
|||||||
import { ApolloClient, InMemoryCache } from '@apollo/client'
|
import { ApolloClient, InMemoryCache } from '@apollo/client'
|
||||||
import { minutesToMilliseconds } from 'date-fns'
|
|
||||||
import {
|
import {
|
||||||
CountDocumentsDocument,
|
CountDocumentsDocument,
|
||||||
CountDocumentsQueryVariables,
|
CountDocumentsQueryVariables,
|
||||||
@ -55,14 +54,13 @@ export class UnbodyService {
|
|||||||
helpers = UnbodyHelpers
|
helpers = UnbodyHelpers
|
||||||
|
|
||||||
lastUpdate: number = 0
|
lastUpdate: number = 0
|
||||||
initialDataLastUpdate: number = 0
|
loadingInitialData: boolean = false
|
||||||
initialDataPromise: CreatePromiseResult<(typeof this)['initialData']> =
|
|
||||||
createPromise()
|
|
||||||
|
|
||||||
initialData: {
|
initialData: {
|
||||||
posts: LPE.Post.Document[]
|
posts: LPE.Post.Document[]
|
||||||
staticPages: LPE.StaticPage.Document[]
|
staticPages: LPE.StaticPage.Document[]
|
||||||
} = { posts: [], staticPages: [] }
|
} = { posts: [], staticPages: [] }
|
||||||
|
initialDataPromise: CreatePromiseResult<any> = null as any
|
||||||
|
|
||||||
constructor(private apiKey: string, private projectId: string) {
|
constructor(private apiKey: string, private projectId: string) {
|
||||||
const cache = new InMemoryCache({
|
const cache = new InMemoryCache({
|
||||||
@ -114,20 +112,17 @@ export class UnbodyService {
|
|||||||
|
|
||||||
private checkForUpdates = async () => {
|
private checkForUpdates = async () => {
|
||||||
const data = await getWebhookData()
|
const data = await getWebhookData()
|
||||||
if (!data) return
|
|
||||||
|
|
||||||
if (data.lastUpdate > this.lastUpdate) {
|
if (!this.loadingInitialData && data && data.lastUpdate > this.lastUpdate) {
|
||||||
this.lastUpdate = data.lastUpdate
|
this.lastUpdate = data.lastUpdate
|
||||||
await this.clearCache()
|
|
||||||
this.loadInitialData(true)
|
this.loadInitialData(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(this.checkForUpdates, 1000)
|
setTimeout(this.checkForUpdates, 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
private _loadInitialData = async () => {
|
private _loadInitialData = async (callback: (data: any) => void) => {
|
||||||
this.initialDataLastUpdate = +new Date()
|
this.loadingInitialData = true
|
||||||
|
|
||||||
const articles: LPE.Article.Data[] = await this.fetchAllArticles()
|
const articles: LPE.Article.Data[] = await this.fetchAllArticles()
|
||||||
const episodes: LPE.Podcast.Document[] = await this.fetchAllEpisodes()
|
const episodes: LPE.Podcast.Document[] = await this.fetchAllEpisodes()
|
||||||
const staticPages = await this.fetchAllStaticPages()
|
const staticPages = await this.fetchAllStaticPages()
|
||||||
@ -140,22 +135,19 @@ export class UnbodyService {
|
|||||||
posts,
|
posts,
|
||||||
staticPages,
|
staticPages,
|
||||||
}
|
}
|
||||||
|
callback(this.initialData)
|
||||||
this.initialDataPromise.resolve({ posts, staticPages })
|
this.loadingInitialData = false
|
||||||
}
|
}
|
||||||
|
|
||||||
loadInitialData = async (forced: boolean = false) => {
|
loadInitialData = async (forced: boolean = false) => {
|
||||||
if (
|
if (forced) {
|
||||||
forced ||
|
|
||||||
+new Date() - this.initialDataLastUpdate > minutesToMilliseconds(10)
|
|
||||||
) {
|
|
||||||
console.log('load initial data')
|
|
||||||
this.initialDataPromise &&
|
this.initialDataPromise &&
|
||||||
this.initialDataPromise.resolve(this.initialData)
|
this.initialDataPromise.resolve(this.initialData)
|
||||||
|
|
||||||
this.initialDataPromise = createPromise()
|
this.initialDataPromise = createPromise()
|
||||||
|
|
||||||
await this.clearCache()
|
await this.clearCache()
|
||||||
this._loadInitialData()
|
this._loadInitialData(this.initialDataPromise.callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.initialDataPromise.promise
|
return this.initialDataPromise.promise
|
||||||
|
Loading…
x
Reference in New Issue
Block a user