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
|
|
|
})
|
|
|
|
.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()
|
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 }
|
|
|
|
})
|
|
|
|
|
2017-12-04 23:19:53 +00:00
|
|
|
export function initStore (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
|
|
|
|
}
|