Skip to content

Commit

Permalink
fix: should return unsubscribe method
Browse files Browse the repository at this point in the history
  • Loading branch information
wxnet2013 committed Oct 9, 2018
1 parent 2702e14 commit f923931
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/rxloop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Subject, BehaviorSubject, throwError, combineLatest } from "rxjs";
import { filter, scan, map, publishReplay, refCount, catchError } from "rxjs/operators";
import invariant from 'invariant';
import checkModel from './check-model';
import { isFunction } from './utils';
import initPlugins from './plugins';

export function rxloop( config = {} ) {
Expand Down Expand Up @@ -186,8 +187,19 @@ export function rxloop( config = {} ) {
return _state;
}

function subscribe(fn = () => {}) {
return this.stream().subscribe(fn);
function subscribe(listener = () => {}) {
invariant(
isFunction(listener),
'Expected the listener to be a function',
);
const sub = this.stream().subscribe(listener);

let isSubscribed = true
return function unsubscribe() {
if (!isSubscribed) return;
isSubscribed = false;
sub.unsubscribe();
};
}

function stream(name) {
Expand Down
4 changes: 2 additions & 2 deletions test/rxloop.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ describe('Single store', () => {
});

test('Subscribe single stream', (done) => {
const sub = app.subscribe(() => {
const unsubscribe = app.subscribe(() => {
expect(app.getState()).toEqual({
user: {
user: 'user',
Expand All @@ -362,7 +362,7 @@ describe('Single store', () => {
});
done();
});
sub.unsubscribe();
unsubscribe();
});

test('get single store test', () => {
Expand Down

0 comments on commit f923931

Please sign in to comment.