Skip to content

Experiment with uber dispatcher #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions examples/containers/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React from 'react';
import CounterApp from './CounterApp';
import TodoApp from './TodoApp';
import { createRedux, Provider } from 'redux';
import { createRedux, composeStores, Provider } from 'redux';
import createDispatcher from '../dispatcher/createDispatcher';
import * as stores from '../stores/index';

const redux = createRedux(stores);
const store = composeStores(stores);
// You can modify these options while the code is running:
const dispatcher = createDispatcher(store, { log: true, replay: false });
const redux = createRedux(dispatcher);

export default class App {
render() {
Expand Down
73 changes: 73 additions & 0 deletions examples/dispatcher/createDispatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
function createDefaultReducer(store) {
return store;
}

const REPLAY_STATE = Symbol('Replay State');
function createReplayingReducer(store) {
return (state = {}, action) => {
let {
[REPLAY_STATE]: { actions = [], initialState = state } = {},
...appState
} = state;

actions = [...actions, action];
appState = actions.reduce(store, initialState);

return {
[REPLAY_STATE]: { actions, initialState },
...appState
}
};
}

function createDefaultScheduler(dispatch, getState) {
function schedule(action) {
if (typeof action === 'function') {
return action(schedule, getState());
} else {
return dispatch(action);
}
}

return schedule;
}

export default function createDispatcher(store, {
log = false,
replay = false
}: options = {}) {
return function dispatcher(initialState, setState) {
const reduce = replay ?
createReplayingReducer(store) :
createDefaultReducer(store);

if (replay) {
console.debug('---- Replay Mode ----');
} else {
console.debug('---- Normal Mode ----');
}
console.debug('Initial state:', initialState);

let state = reduce(initialState, {});
setState(state);

function dispatch(action) {
if (log) {
console.groupCollapsed(replay ? '[replay]' : '[normal]', action);
console.log('State before:', state);
}

state = reduce(state, action);
setState(state);

if (log) {
console.log('State after:', state);
console.groupEnd(replay ? '[replay]' : '[normal]', action);
}

return action;
}

return createDefaultScheduler(dispatch, () => state);
};
}