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
Tim Neutkens 9c4eefcdbf
Add prettier for examples directory (#5909)
* Add prettier for examples directory

* Fix files

* Fix linting

* Add prettier script in case it has to be ran again
2018-12-17 17:34:32 +01:00

38 lines
910 B
JavaScript

import { action, observable } from 'mobx'
import { useStaticRendering } from 'mobx-react'
const isServer = typeof window === 'undefined'
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
}