Skip to content

Instantly share code, notes, and snippets.

View factoryhr's full-sized avatar

Factory.dev factoryhr

View GitHub Profile
var actionTypes = {
increment: 'increment',
decrement: 'decrement',
};
function incrementAction(value) {
return {
type: actionTypes.increment,
};
}
function decrementAction(value) {
return {
type: actionTypes.increment,
};
function incrementHandler(state, action) {
var nextState = Object.assign({}, state);
nextState.counter += 1;
return nextState;
}
function decrementHandler(state, action) {
var nextState = Object.assign({}, state);
nextState.counter -= 1;
return nextState;
function counterReducer(state, action) {
switch (action.type) {
case actionTypes.increment:
return incrementHandler(state, action);
case actionTypes.decrement:
return decrementHandler(state, action);
default:
return state;
}
}
function createStore(reducer, preloadedState) {
// Our store implementation will go here...
}
function createStore(reducer, preloadedState) {
// Store the current state
var currentState = preloadedState;
// Retrieve the current state
function getState() {
return currentState;
}
// Expose the API
function createStore(reducer, preloadedState) {
var currentState = preloadedState;
// Keep track of all listeners
var listeners = [];
function getState() {
return currentState;
}
// Subscribe new listeners to the store
function createStore(reducer, preloadedState) {
var currentState = preloadedState;
var listeners = [];
function getState() {
return currentState;
}
function subscribe(listener) {
listeners.push(listener);
function createStore(reducer, preloadedState) {
if (typeof reducer !== 'function') {
throw new Error('Expected `reducer` to be a function.');
}
if (typeof preloadedState === 'function') {
console.warn(
'Received function as `preloadedState`. ' +
'State is undefined.'
);