Skip to content

Commit

Permalink
Uses ConnectableObservable for store
Browse files Browse the repository at this point in the history
  • Loading branch information
godu committed May 24, 2016
1 parent 2ed2b40 commit fc5ce99
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ const createSquirrel = options => {
);

const node$ = createCombiner$(events$);
const {store, observable} = createStore(
const {store, subscription} = createStore(
save(node$),
indexBuilder
);
const api = createAPI(store);

const subscription = observable.subscribe();
subscription.add(() => {
debug('Unsubscribe');
watcher.stop();
Expand Down
12 changes: 3 additions & 9 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ const createStore = (node$, indexer) => {
indexes: indexer(node)
}));

let store = null;
const storeReady = store$.take(1).do(_store => {
store = _store;
}).toPromise();
const observable = store$.do(_store => {
store = _store;
});
const replayed$ = store$.publishReplay(1);

const ready = key => storeReady.then(() => store[key]);
const ready = key => replayed$.first().pluck(key).toPromise();

return {
store: ready,
observable
subscription: replayed$.connect()
};
};

Expand Down
28 changes: 13 additions & 15 deletions src/test/store.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Observable, Subject} from 'rxjs';
import {Observable, Subject, Subscription} from 'rxjs';
import test from 'ava';
import {identity} from 'lodash/fp';

Expand Down Expand Up @@ -38,33 +38,31 @@ test('should wait first event', t => {
]));
});

test('should return observable', t => {
test('should return subscription', t => {
const node$ = Observable.empty();

const {
observable
subscription
} = createStore(node$, identity);

t.true(observable instanceof Observable);
t.true(subscription instanceof Subscription);
});

test('should', t => {
const node$ = Observable.of('foo', 'bar');

const {
store,
observable
subscription
} = createStore(node$, identity);

return observable.toPromise().then(
Promise.all([
store('node').then(store =>
t.deepEqual(store, 'bar')
),
store('indexes').then(indexes =>
t.deepEqual(indexes, 'bar')
)
])
);
return Promise.all([
store('node').then(store =>
t.deepEqual(store, 'bar')
),
store('indexes').then(indexes =>
t.deepEqual(indexes, 'bar')
)
]).then(() => subscription.unsubscribe());
});

0 comments on commit fc5ce99

Please sign in to comment.