Skip to content
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

Remove dispatcher (WIP) #166

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
37 changes: 10 additions & 27 deletions src/Redux.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,29 @@
import createDispatcher from './createDispatcher';
import composeStores from './utils/composeStores';
import thunkMiddleware from './middleware/thunk';

export default class Redux {
constructor(dispatcherOrStores, initialState) {
let finalDispatcher = dispatcherOrStores;
if (typeof dispatcherOrStores === 'object') {
// A shortcut notation to use the default dispatcher
finalDispatcher = createDispatcher(
composeStores(dispatcherOrStores),
(getState) => [thunkMiddleware(getState)]
);
}

constructor(reducer, initialState) {
this.state = initialState;
this.listeners = [];
this.replaceDispatcher(finalDispatcher);
this.replaceReducer(reducer);
}

getDispatcher() {
return this.dispatcher;
getReducer() {
return this.reducer;
}

replaceDispatcher(nextDispatcher) {
this.dispatcher = nextDispatcher;
this.dispatchFn = nextDispatcher(this.state, ::this.setState);
replaceReducer(nextReducer) {
this.reducer = nextReducer;
this.dispatch({ type: '@@INIT' });
}

dispatch(action) {
return this.dispatchFn(action);
const { reducer } = this;
this.state = reducer(this.state, action);
this.listeners.forEach(listener => listener());
}

getState() {
return this.state;
}

setState(nextState) {
this.state = nextState;
this.listeners.forEach(listener => listener());
return nextState;
}

subscribe(listener) {
const { listeners } = this;
listeners.push(listener);
Expand Down
4 changes: 2 additions & 2 deletions src/components/createProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default function createProvider(React) {
const { redux: nextRedux } = nextProps;

if (redux !== nextRedux) {
const nextDispatcher = nextRedux.getDispatcher();
redux.replaceDispatcher(nextDispatcher);
const nextReducer = nextRedux.getReducer();
redux.replaceReducer(nextReducer);
}
}

Expand Down
26 changes: 0 additions & 26 deletions src/createDispatcher.js

This file was deleted.

13 changes: 9 additions & 4 deletions src/createRedux.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import Redux from './Redux';
import composeReducers from './utils/composeReducers';

export default function createRedux(...args) {
const redux = new Redux(...args);
export default function createRedux(reducer, initialState) {
const finalReducer = typeof reducer === 'function' ?
reducer :
composeReducers(reducer);

const redux = new Redux(finalReducer, initialState);

return {
subscribe: ::redux.subscribe,
dispatch: ::redux.dispatch,
getState: ::redux.getState,
getDispatcher: ::redux.getDispatcher,
replaceDispatcher: ::redux.replaceDispatcher
getReducer: ::redux.getReducer,
replaceReducer: ::redux.replaceReducer
};
}
6 changes: 2 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Core
import createRedux from './createRedux';
import createDispatcher from './createDispatcher';

// Utilities
import composeMiddleware from './utils/composeMiddleware';
import composeStores from './utils/composeStores';
import composeReducers from './utils/composeReducers';
import bindActionCreators from './utils/bindActionCreators';

export {
createRedux,
createDispatcher,
composeMiddleware,
composeStores,
composeReducers,
bindActionCreators
};
14 changes: 5 additions & 9 deletions src/middleware/thunk.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export default function thunkMiddleware(getState) {
return (next) => {
const recurse = (action) =>
typeof action === 'function' ?
action(recurse, getState) :
next(action);

return recurse;
};
export default function thunkMiddleware({ getState, dispatch }) {
return (next) => (action) =>
typeof action === 'function' ?
action(dispatch, getState) :
next(action);
}
12 changes: 12 additions & 0 deletions src/utils/composeReducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import mapValues from '../utils/mapValues';
import pick from '../utils/pick';

export default function composeReducers(reducers) {
const finalReducers = pick(reducers, (val) => typeof val === 'function');

return function Composition(atom = {}, action) {
return mapValues(finalReducers, (store, key) =>
store(atom[key], action)
);
};
}
11 changes: 0 additions & 11 deletions src/utils/composeStores.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/components/connect.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import expect from 'expect';
import jsdomReact from './jsdomReact';
import React, { PropTypes, Component } from 'react/addons';
import { createRedux } from '../../src';
import { createRedux, composeReducers } from '../../src';
import { connect, Connector } from '../../src/react';

const { TestUtils } = React.addons;
Expand Down
16 changes: 8 additions & 8 deletions test/composeStores.spec.js → test/composeReducers.spec.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import expect from 'expect';
import { composeStores } from '../src';
import { composeReducers } from '../src';

describe('Utils', () => {
describe('composeStores', () => {
it('should return a store that maps state keys to reducer functions', () => {
const store = composeStores({
describe('composeReducers', () => {
it('should return a reducer that maps state keys to reducer functions', () => {
const reducer = composeReducers({
counter: (state = 0, action) =>
action.type === 'increment' ? state + 1 : state,
stack: (state = [], action) =>
action.type === 'push' ? [...state, action.value] : state
});

const s1 = store({}, { type: 'increment' });
const s1 = reducer({}, { type: 'increment' });
expect(s1).toEqual({ counter: 1, stack: [] });
const s2 = store(s1, { type: 'push', value: 'a' });
const s2 = reducer(s1, { type: 'push', value: 'a' });
expect(s2).toEqual({ counter: 1, stack: ['a'] });
});

it('should ignore all props which are not a function', () => {
const store = composeStores({
const reducer = composeReducers({
fake: true,
broken: 'string',
another: {nested: 'object'},
stack: (state = []) => state
});

expect(Object.keys(store({}, {type: 'push'}))).toEqual(['stack']);
expect(Object.keys(reducer({}, {type: 'push'}))).toEqual(['stack']);
});
});
});
2 changes: 1 addition & 1 deletion test/createDispatcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { constants, defaultText, todoActions, todoStore } = helpers;
const { addTodo, addTodoAsync } = todoActions;
const { ADD_TODO } = constants;

describe('createDispatcher', () => {
describe.skip('createDispatcher', () => {

it('should handle sync and async dispatches', done => {
const spy = expect.createSpy(
Expand Down
8 changes: 4 additions & 4 deletions test/createRedux.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('createRedux', () => {
expect(methods).toContain('subscribe');
expect(methods).toContain('dispatch');
expect(methods).toContain('getState');
expect(methods).toContain('getDispatcher');
expect(methods).toContain('replaceDispatcher');
expect(methods).toContain('getReducer');
expect(methods).toContain('replaceReducer');
});

it('should subscribe to changes', done => {
Expand Down Expand Up @@ -54,11 +54,11 @@ describe('createRedux', () => {
expect(changeListenerSpy.calls.length).toBe(1);
});

it('should use existing state when replacing the dispatcher', () => {
it('should use existing state when replacing the reducer', () => {
redux.dispatch(addTodo('Hello'));

let nextRedux = createRedux({ todoStore });
redux.replaceDispatcher(nextRedux.getDispatcher());
redux.replaceReducer(nextRedux.getReducer());

let state;
let action = (_, getState) => {
Expand Down