2016-12-22 05:10:54 +00:00
|
|
|
import { createStore, applyMiddleware } from 'redux'
|
|
|
|
import thunkMiddleware from 'redux-thunk'
|
|
|
|
|
|
|
|
export const reducer = (state = { lastUpdate: 0, light: false }, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'TICK': return { lastUpdate: action.ts, light: !!action.light }
|
|
|
|
default: return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const startClock = () => dispatch => {
|
2017-01-08 15:04:35 +00:00
|
|
|
return setInterval(() => dispatch({ type: 'TICK', light: true, ts: Date.now() }), 800)
|
2016-12-22 05:10:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-28 02:33:42 +00:00
|
|
|
let store = null
|
|
|
|
|
2016-12-22 05:10:54 +00:00
|
|
|
export const initStore = (reducer, initialState, isServer) => {
|
|
|
|
if (isServer && typeof window === 'undefined') {
|
|
|
|
return createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
|
|
|
|
} else {
|
2017-01-28 02:33:42 +00:00
|
|
|
if (!store) {
|
|
|
|
store = createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
|
2016-12-22 05:10:54 +00:00
|
|
|
}
|
2017-01-28 02:33:42 +00:00
|
|
|
return store
|
2016-12-22 05:10:54 +00:00
|
|
|
}
|
|
|
|
}
|