2016-12-22 05:10:54 +00:00
|
|
|
import { createStore, applyMiddleware } from 'redux'
|
|
|
|
import thunkMiddleware from 'redux-thunk'
|
|
|
|
|
2017-04-30 20:44:24 +00:00
|
|
|
const exampleInitialState = {
|
|
|
|
lastUpdate: 0,
|
|
|
|
light: false,
|
|
|
|
count: 0
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actionTypes = {
|
|
|
|
ADD: 'ADD',
|
|
|
|
TICK: 'TICK'
|
|
|
|
}
|
|
|
|
|
|
|
|
// REDUCERS
|
|
|
|
export const reducer = (state = exampleInitialState, action) => {
|
2016-12-22 05:10:54 +00:00
|
|
|
switch (action.type) {
|
2017-04-30 20:44:24 +00:00
|
|
|
case actionTypes.TICK:
|
|
|
|
return Object.assign({}, state, { lastUpdate: action.ts, light: !!action.light })
|
|
|
|
case actionTypes.ADD:
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
count: state.count + 1
|
|
|
|
})
|
2016-12-22 05:10:54 +00:00
|
|
|
default: return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 20:44:24 +00:00
|
|
|
// ACTIONS
|
|
|
|
export const serverRenderClock = (isServer) => dispatch => {
|
|
|
|
return dispatch({ type: actionTypes.TICK, light: !isServer, ts: Date.now() })
|
|
|
|
}
|
|
|
|
|
2016-12-22 05:10:54 +00:00
|
|
|
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-04-30 20:44:24 +00:00
|
|
|
export const addCount = () => dispatch => {
|
|
|
|
return dispatch({ type: actionTypes.ADD })
|
|
|
|
}
|
|
|
|
|
|
|
|
export const initStore = (initialState = exampleInitialState) => {
|
2017-02-18 04:10:27 +00:00
|
|
|
return createStore(reducer, initialState, applyMiddleware(thunkMiddleware))
|
2016-12-22 05:10:54 +00:00
|
|
|
}
|