Skip to content

Instantly share code, notes, and snippets.

@jdjkelly
jdjkelly / index.js
Created April 25, 2017 14:44
redux/index.js
export {
createStore,
combineReducers,
bindActionCreators,
applyMiddleware,
compose
}
@jdjkelly
jdjkelly / createStore.js
Last active April 30, 2017 16:16
redux/createStore.js
export default function createStore(reducer, preloadedState, enhancer) {
...
return {
dispatch,
subscribe,
getState,
replaceReducer,
[$$observable]: observable
}
}
@jdjkelly
jdjkelly / createStore.js
Created April 30, 2017 16:27
redux/createStore.js
let currentReducer = reducer
let currentState = preloadedState
let currentListeners = []
let nextListeners = currentListeners
let isDispatching = false
@jdjkelly
jdjkelly / Reducer.flow.js
Created April 30, 2017 16:38
flow-typed/redux.js
declare type Reducer<S, A> = (state: S, action: A) => S;
@jdjkelly
jdjkelly / small.js
Last active April 30, 2017 18:47
simplest-possible-redux-app
import { createStore } from 'redux'; // CommonJS: const createStore = require('redux')['createStore']
const app = createStore((state, action) => { return state });
@jdjkelly
jdjkelly / preload.js
Created April 30, 2017 18:47
simplest-possible-redux-app-with-preload
import { createStore } from 'redux';
const app = createStore((state, action) => { return state }, { booted_at: new Date() });
@jdjkelly
jdjkelly / ducks.js
Last active April 30, 2017 19:54
ducks.js
export default function createStore(reducer, preloadedState) {
let state = preloadedState
let listeners = []
let dispatching = false
function getState() {
return state
}
function dispatch(action) {
@jdjkelly
jdjkelly / createStore.js
Last active April 30, 2017 23:34
getState()
...
let currentState = preloadedState
...
function getState() {
return currentState
}
@jdjkelly
jdjkelly / createStore.js
Last active May 1, 2017 00:24
dispatch
function dispatch(action) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
}
if (typeof action.type === 'undefined') {
throw new Error(
@jdjkelly
jdjkelly / createStore.js
Created May 1, 2017 00:24
Dispatch Flow Type
declare type Dispatch<A: { type: $Subtype<string> }> = (action: A) => A;