From fc5ce9996cfaaf1808a9791d149eaa7efd8b1f27 Mon Sep 17 00:00:00 2001 From: godu Date: Mon, 23 May 2016 17:13:41 +0200 Subject: [PATCH] Uses ConnectableObservable for store --- src/index.js | 3 +-- src/store.js | 12 +++--------- src/test/store.js | 28 +++++++++++++--------------- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/index.js b/src/index.js index a35f564..36d0750 100644 --- a/src/index.js +++ b/src/index.js @@ -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(); diff --git a/src/store.js b/src/store.js index 64c78d2..ee4f02e 100644 --- a/src/store.js +++ b/src/store.js @@ -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() }; }; diff --git a/src/test/store.js b/src/test/store.js index e9851d9..57c8f9e 100644 --- a/src/test/store.js +++ b/src/test/store.js @@ -1,4 +1,4 @@ -import {Observable, Subject} from 'rxjs'; +import {Observable, Subject, Subscription} from 'rxjs'; import test from 'ava'; import {identity} from 'lodash/fp'; @@ -38,14 +38,14 @@ 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 => { @@ -53,18 +53,16 @@ test('should', t => { 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()); });