2018-05-10 17:40:21 +00:00
|
|
|
import { createStore, applyMiddleware } from 'redux'
|
|
|
|
import { composeWithDevTools } from 'redux-devtools-extension'
|
|
|
|
import thunkMiddleware from 'redux-thunk'
|
|
|
|
import { fromJS } from 'immutable'
|
|
|
|
|
|
|
|
const exampleInitialState = fromJS({
|
|
|
|
lastUpdate: 0,
|
|
|
|
light: false,
|
|
|
|
count: 0
|
|
|
|
})
|
|
|
|
|
|
|
|
export const actionTypes = {
|
|
|
|
ADD: 'ADD',
|
|
|
|
TICK: 'TICK'
|
|
|
|
}
|
|
|
|
|
|
|
|
// REDUCERS
|
|
|
|
export const reducer = (state = exampleInitialState, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case actionTypes.TICK:
|
|
|
|
return state.merge({
|
|
|
|
lastUpdate: action.ts,
|
|
|
|
light: !!action.light
|
|
|
|
})
|
|
|
|
|
|
|
|
case actionTypes.ADD:
|
|
|
|
return state.merge({
|
|
|
|
count: state.get('count') + 1
|
|
|
|
})
|
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
default:
|
|
|
|
return state
|
2018-05-10 17:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ACTIONS
|
2018-12-17 16:34:32 +00:00
|
|
|
export const serverRenderClock = isServer => dispatch => {
|
2018-05-10 17:40:21 +00:00
|
|
|
return dispatch({ type: actionTypes.TICK, light: !isServer, ts: Date.now() })
|
|
|
|
}
|
|
|
|
|
|
|
|
export const startClock = () => dispatch => {
|
2018-12-17 16:34:32 +00:00
|
|
|
return setInterval(
|
|
|
|
() => dispatch({ type: actionTypes.TICK, light: true, ts: Date.now() }),
|
|
|
|
1000
|
|
|
|
)
|
2018-05-10 17:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const addCount = () => dispatch => {
|
|
|
|
return dispatch({ type: actionTypes.ADD })
|
|
|
|
}
|
|
|
|
|
|
|
|
export const makeStore = (initialState = exampleInitialState) => {
|
2018-12-17 16:34:32 +00:00
|
|
|
return createStore(
|
|
|
|
reducer,
|
|
|
|
initialState,
|
|
|
|
composeWithDevTools(applyMiddleware(thunkMiddleware))
|
|
|
|
)
|
2018-05-10 17:40:21 +00:00
|
|
|
}
|