Skip to content

Commit

Permalink
Fix redux support for observables (#275)
Browse files Browse the repository at this point in the history
fixes #274
  • Loading branch information
micha149 authored and gaearon committed May 9, 2016
1 parent ac3dc18 commit 7b5adf9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@
"react-addons-test-utils": "^0.14.0",
"react-dom": "^0.14.0",
"rimraf": "^2.3.4",
"rxjs": "^5.0.0-beta.6",
"webpack": "^1.11.0"
},
"peerDependencies": {
"redux": "^3.0.0",
"redux": "^3.5.0",
"react": "^0.14.0 || ^15.0.0-rc.1"
},
"dependencies": {
"lodash": "^4.2.0",
"react-redux": "^4.0.0",
"redux": "^3.2.1"
"redux": "^3.5.2",
"symbol-observable": "^0.2.4"
}
}
38 changes: 31 additions & 7 deletions src/instrument.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import difference from 'lodash/difference';
import union from 'lodash/union';
import $$observable from 'symbol-observable';

export const ActionTypes = {
PERFORM_ACTION: 'PERFORM_ACTION',
Expand Down Expand Up @@ -383,6 +384,14 @@ function unliftState(liftedState) {
function unliftStore(liftedStore, liftReducer) {
let lastDefinedState;

function getState() {
const state = unliftState(liftedStore.getState());
if (state !== undefined) {
lastDefinedState = state;
}
return lastDefinedState;
}

return {
...liftedStore,

Expand All @@ -393,16 +402,31 @@ function unliftStore(liftedStore, liftReducer) {
return action;
},

getState() {
const state = unliftState(liftedStore.getState());
if (state !== undefined) {
lastDefinedState = state;
}
return lastDefinedState;
},
getState,

replaceReducer(nextReducer) {
liftedStore.replaceReducer(liftReducer(nextReducer));
},

[$$observable]() {
return {
...liftedStore[$$observable](),
subscribe(observer) {
if (typeof observer !== 'object') {
throw new TypeError('Expected the observer to be an object.');
}

function observeState() {
if (observer.next) {
observer.next(getState());
}
}

observeState();
const unsubscribe = liftedStore.subscribe(observeState);
return { unsubscribe };
}
};
}
};
}
Expand Down
18 changes: 18 additions & 0 deletions test/instrument.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import expect, { spyOn } from 'expect';
import { createStore, compose } from 'redux';
import instrument, { ActionCreators } from '../src/instrument';
import {Observable} from 'rxjs';

import 'rxjs/add/observable/from';

function counter(state = 0, action) {
switch (action.type) {
Expand Down Expand Up @@ -53,6 +56,21 @@ describe('instrument', () => {
expect(store.getState()).toBe(2);
});

it('should provide observable', () => {
let lastValue;
let calls = 0;

Observable.from(store)
.subscribe(state => {
lastValue = state;
calls++;
});

expect(lastValue).toBe(0);
store.dispatch({ type: 'INCREMENT' });
expect(lastValue).toBe(1);
});

it('should rollback state to the last committed state', () => {
store.dispatch({ type: 'INCREMENT' });
store.dispatch({ type: 'INCREMENT' });
Expand Down

0 comments on commit 7b5adf9

Please sign in to comment.