2017-11-23 12:41:59 +00:00
|
|
|
import { types, applySnapshot } from 'mobx-state-tree'
|
|
|
|
|
|
|
|
let store = null
|
|
|
|
|
|
|
|
const Store = types
|
|
|
|
.model({
|
|
|
|
lastUpdate: types.Date,
|
2017-12-04 23:19:53 +00:00
|
|
|
light: false
|
2017-11-23 12:41:59 +00:00
|
|
|
})
|
2018-12-17 16:34:32 +00:00
|
|
|
.actions(self => {
|
2017-12-04 23:19:53 +00:00
|
|
|
let timer
|
|
|
|
function start () {
|
2017-11-23 12:41:59 +00:00
|
|
|
timer = setInterval(() => {
|
|
|
|
// mobx-state-tree doesn't allow anonymous callbacks changing data
|
|
|
|
// pass off to another action instead
|
2017-12-04 23:19:53 +00:00
|
|
|
self.update()
|
2018-02-12 10:02:05 +00:00
|
|
|
}, 1000)
|
2017-11-23 12:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 23:19:53 +00:00
|
|
|
function update () {
|
2017-11-23 12:41:59 +00:00
|
|
|
self.lastUpdate = Date.now()
|
|
|
|
self.light = true
|
|
|
|
}
|
|
|
|
|
2017-12-04 23:19:53 +00:00
|
|
|
function stop () {
|
|
|
|
clearInterval(timer)
|
2017-11-23 12:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { start, stop, update }
|
|
|
|
})
|
|
|
|
|
2018-10-14 08:04:58 +00:00
|
|
|
export function initializeStore (isServer, snapshot = null) {
|
2017-11-23 12:41:59 +00:00
|
|
|
if (isServer) {
|
|
|
|
store = Store.create({ lastUpdate: Date.now() })
|
|
|
|
}
|
|
|
|
if (store === null) {
|
|
|
|
store = Store.create({ lastUpdate: Date.now() })
|
|
|
|
}
|
|
|
|
if (snapshot) {
|
|
|
|
applySnapshot(store, snapshot)
|
|
|
|
}
|
|
|
|
return store
|
|
|
|
}
|