1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-mobx/store.js
2019-02-14 19:05:08 +01:00

39 lines
899 B
JavaScript

import { action, observable } from 'mobx'
import { useStaticRendering } from 'mobx-react'
const isServer = !process.browser
useStaticRendering(isServer)
class Store {
@observable lastUpdate = 0
@observable light = false
constructor(isServer, initialData = {}) {
this.lastUpdate =
initialData.lastUpdate != null ? initialData.lastUpdate : Date.now()
this.light = !!initialData.light
}
@action start = () => {
this.timer = setInterval(() => {
this.lastUpdate = Date.now()
this.light = true
}, 1000)
}
stop = () => clearInterval(this.timer)
}
let store = null
export function initializeStore (initialData) {
// Always make a new store if server, otherwise state is shared between requests
if (isServer) {
return new Store(isServer, initialData)
}
if (store === null) {
store = new Store(isServer, initialData)
}
return store
}