Publishreplay(1) for all derive functions (#557)

* Add drr

* Transform all to publishreplay(1)

* Add comment in test

* Update headers

* Update headers

* Lint
This commit is contained in:
Amaury Martiny
2019-01-09 13:03:05 +01:00
committed by Jaco Greeff
parent 07d7112f8a
commit 6781bbaa26
10 changed files with 75 additions and 17 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2017-2019 @polkadot/ui-react-rx authors & contributors
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2017-2019 @polkadot/ui-react-rx authors & contributors
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
+6 -2
View File
@@ -1,4 +1,4 @@
// Copyright 2017-2019 @polkadot/ui-react-rx authors & contributors
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
@@ -14,8 +14,12 @@ const testFunction = (api: ApiRx) => {
Method extends keyof (typeof api.derive[Section])
>(section: Section, method: Method, inputs: any[]) => {
describe(`derive.${section}.${method}`, () => {
it('should be a function', () => {
expect(typeof api.derive[section][method]).toBe('function');
});
it('should return an Observable', () => {
expect((api.derive[section][method] as any)(...inputs) instanceof Observable);
expect((api.derive[section][method] as any)(...inputs)).toBeInstanceOf(Observable);
});
it('should be memoized', () => {
+2 -2
View File
@@ -1,12 +1,12 @@
// Copyright 2017-2019 @polkadot/ui-react-rx authors & contributors
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import ApiRx from '@polkadot/api/rx';
import { cache } from './cache';
import * as chain from './chain';
import * as session from './session';
import { cache } from './util/cache';
// Put all derived functions in an object, for easier Object.keys()-ing.
const functions = { chain, session };
+1 -1
View File
@@ -1,4 +1,4 @@
// Copyright 2017-2019 @polkadot/ui-react-rx authors & contributors
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
@@ -1,4 +1,4 @@
// Copyright 2017-2019 @polkadot/ui-react-rx authors & contributors
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
@@ -9,6 +9,7 @@ import ApiRx from '@polkadot/api/rx';
import { BlockNumber } from '@polkadot/types/index';
import { bestNumber } from '../chain';
import { drr } from '../util/drr';
export function sessionProgress (api: ApiRx) {
return (): Observable<BN> =>
@@ -16,11 +17,14 @@ export function sessionProgress (api: ApiRx) {
bestNumber(api)(),
api.query.session.sessionLength(),
api.query.session.lastLengthChange()
]).pipe(map(
([bestNumber, sessionLength, lastLengthChange]) =>
(bestNumber as BlockNumber || new BN(0))
.sub(lastLengthChange as BlockNumber || new BN(0))
.add(sessionLength as BlockNumber || new BN(1))
.mod(sessionLength as BlockNumber || new BN(1))
));
]).pipe(
map(
([bestNumber, sessionLength, lastLengthChange]) =>
(bestNumber as BlockNumber || new BN(0))
.sub(lastLengthChange as BlockNumber || new BN(0))
.add(sessionLength as BlockNumber || new BN(1))
.mod(sessionLength as BlockNumber || new BN(1))
),
drr()
);
}
@@ -1,4 +1,4 @@
// Copyright 2017-2019 @polkadot/ui-react-rx authors & contributors
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
@@ -1,4 +1,4 @@
// Copyright 2017-2019 @polkadot/ui-react-rx authors & contributors
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
+32
View File
@@ -0,0 +1,32 @@
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { of, timer } from 'rxjs';
import { drr } from './drr';
describe('drr', () => {
it('should not fire twice the same value', (done) => {
let count = 0;
of(1, 1).pipe(drr()).subscribe(() => { ++count; });
setTimeout(() => {
expect(count).toBe(1);
done();
}, 50);
});
it('should be a ReplaySubject(1)', (done) => {
const obs = timer(0, 50).pipe(drr()); // Starts at 0, increments every 50ms
obs.subscribe(); // Fire the observable
// Subscribe another time after some time, i.e. after the observable has fired
setTimeout(() => {
obs.subscribe((value) => {
expect(value).toBe(2);
done();
});
}, 120);
});
});
+18
View File
@@ -0,0 +1,18 @@
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { distinctUntilChanged, publishReplay, refCount } from 'rxjs/operators';
import { Observable } from 'rxjs';
/**
* Shorthand for distinctUntilChanged(), publishReplay(1) and refCount().
*
* @ignore
*/
export const drr = () => <T>(source$: Observable<T>): Observable<T> =>
source$.pipe(
distinctUntilChanged(),
publishReplay(1),
refCount()
);