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

Fix redux support for observables #275

Merged
merged 1 commit into from
May 9, 2016
Merged
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
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