2017-01-06 11:38:51 +00:00
|
|
|
import { action, observable } from 'mobx'
|
2018-11-06 09:18:26 +00:00
|
|
|
import { useStaticRendering } from 'mobx-react'
|
2017-01-06 11:38:51 +00:00
|
|
|
|
2018-11-06 09:18:26 +00:00
|
|
|
const isServer = typeof window === 'undefined'
|
|
|
|
useStaticRendering(isServer)
|
2017-01-06 11:38:51 +00:00
|
|
|
|
|
|
|
class Store {
|
|
|
|
@observable lastUpdate = 0
|
|
|
|
@observable light = false
|
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
constructor(isServer, initialData = {}) {
|
|
|
|
this.lastUpdate =
|
|
|
|
initialData.lastUpdate != null ? initialData.lastUpdate : Date.now()
|
2018-11-06 09:18:26 +00:00
|
|
|
this.light = !!initialData.light
|
2017-01-06 11:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@action start = () => {
|
|
|
|
this.timer = setInterval(() => {
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
this.light = true
|
2018-02-12 10:02:05 +00:00
|
|
|
}, 1000)
|
2017-01-06 11:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stop = () => clearInterval(this.timer)
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:18:26 +00:00
|
|
|
let store = null
|
2018-12-17 16:34:32 +00:00
|
|
|
export function initializeStore(initialData) {
|
2018-11-06 09:18:26 +00:00
|
|
|
// Always make a new store if server, otherwise state is shared between requests
|
2017-08-19 16:02:40 +00:00
|
|
|
if (isServer) {
|
2018-11-06 09:18:26 +00:00
|
|
|
return new Store(isServer, initialData)
|
|
|
|
}
|
|
|
|
if (store === null) {
|
|
|
|
store = new Store(isServer, initialData)
|
2017-01-06 11:38:51 +00:00
|
|
|
}
|
2018-11-06 09:18:26 +00:00
|
|
|
return store
|
2017-01-06 11:38:51 +00:00
|
|
|
}
|