Skip to content
This repository has been archived by the owner on Dec 21, 2018. It is now read-only.

Commit

Permalink
Fix redux support for observables
Browse files Browse the repository at this point in the history
  • Loading branch information
micha149 authored and zalmoxisus committed May 9, 2016
1 parent d2546f8 commit 8fbfa22
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@
"expect": "^1.6.0",
"isparta": "^3.0.3",
"mocha": "^2.2.5",
"redux": "^3.0.0",
"rimraf": "^2.3.4"
"redux": "^3.5.2",
"rimraf": "^2.3.4",
"rxjs": "^5.0.0-beta.6",
"webpack": "^1.11.0"
},
"dependencies": {
"lodash": "^4.2.0"
"lodash": "^4.2.0",
"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,6 +1,7 @@
import difference from 'lodash/difference';
import union from 'lodash/union';
import isPlainObject from 'lodash/isPlainObject';
import $$observable from 'symbol-observable';

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

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

return {
...liftedStore,

Expand All @@ -402,16 +411,31 @@ export 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 8fbfa22

Please sign in to comment.