From 6781bbaa265b93677464e960801ae072a77debdb Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Wed, 9 Jan 2019 13:03:05 +0100 Subject: [PATCH] Publishreplay(1) for all derive functions (#557) * Add drr * Transform all to publishreplay(1) * Add comment in test * Update headers * Update headers * Lint --- packages/api-derive/src/chain/bestNumber.ts | 2 +- packages/api-derive/src/chain/index.ts | 2 +- packages/api-derive/src/index.spec.ts | 8 +++-- packages/api-derive/src/index.ts | 4 +-- packages/api-derive/src/session/index.ts | 2 +- .../api-derive/src/session/sessionProgress.ts | 20 +++++++----- .../api-derive/src/{ => util}/cache.spec.ts | 2 +- packages/api-derive/src/{ => util}/cache.ts | 2 +- packages/api-derive/src/util/drr.spec.ts | 32 +++++++++++++++++++ packages/api-derive/src/util/drr.ts | 18 +++++++++++ 10 files changed, 75 insertions(+), 17 deletions(-) rename packages/api-derive/src/{ => util}/cache.spec.ts (92%) rename packages/api-derive/src/{ => util}/cache.ts (92%) create mode 100644 packages/api-derive/src/util/drr.spec.ts create mode 100644 packages/api-derive/src/util/drr.ts diff --git a/packages/api-derive/src/chain/bestNumber.ts b/packages/api-derive/src/chain/bestNumber.ts index 0750c50208..6b35e6408d 100644 --- a/packages/api-derive/src/chain/bestNumber.ts +++ b/packages/api-derive/src/chain/bestNumber.ts @@ -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. diff --git a/packages/api-derive/src/chain/index.ts b/packages/api-derive/src/chain/index.ts index 5c0f229d0d..f6c675022a 100644 --- a/packages/api-derive/src/chain/index.ts +++ b/packages/api-derive/src/chain/index.ts @@ -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. diff --git a/packages/api-derive/src/index.spec.ts b/packages/api-derive/src/index.spec.ts index 263f2b8df5..7fb01a4475 100644 --- a/packages/api-derive/src/index.spec.ts +++ b/packages/api-derive/src/index.spec.ts @@ -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', () => { diff --git a/packages/api-derive/src/index.ts b/packages/api-derive/src/index.ts index 234a0dff7e..c126f87360 100644 --- a/packages/api-derive/src/index.ts +++ b/packages/api-derive/src/index.ts @@ -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 }; diff --git a/packages/api-derive/src/session/index.ts b/packages/api-derive/src/session/index.ts index 755fb613f4..322a23c189 100644 --- a/packages/api-derive/src/session/index.ts +++ b/packages/api-derive/src/session/index.ts @@ -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. diff --git a/packages/api-derive/src/session/sessionProgress.ts b/packages/api-derive/src/session/sessionProgress.ts index 2c5fbeaced..0d050f46e0 100644 --- a/packages/api-derive/src/session/sessionProgress.ts +++ b/packages/api-derive/src/session/sessionProgress.ts @@ -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 => @@ -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() + ); } diff --git a/packages/api-derive/src/cache.spec.ts b/packages/api-derive/src/util/cache.spec.ts similarity index 92% rename from packages/api-derive/src/cache.spec.ts rename to packages/api-derive/src/util/cache.spec.ts index 636ba15d9e..407674d4d1 100644 --- a/packages/api-derive/src/cache.spec.ts +++ b/packages/api-derive/src/util/cache.spec.ts @@ -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. diff --git a/packages/api-derive/src/cache.ts b/packages/api-derive/src/util/cache.ts similarity index 92% rename from packages/api-derive/src/cache.ts rename to packages/api-derive/src/util/cache.ts index ad87802c70..5fe45e42f2 100644 --- a/packages/api-derive/src/cache.ts +++ b/packages/api-derive/src/util/cache.ts @@ -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. diff --git a/packages/api-derive/src/util/drr.spec.ts b/packages/api-derive/src/util/drr.spec.ts new file mode 100644 index 0000000000..59cb5291c8 --- /dev/null +++ b/packages/api-derive/src/util/drr.spec.ts @@ -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); + }); +}); diff --git a/packages/api-derive/src/util/drr.ts b/packages/api-derive/src/util/drr.ts new file mode 100644 index 0000000000..dbba4b3575 --- /dev/null +++ b/packages/api-derive/src/util/drr.ts @@ -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 = () => (source$: Observable): Observable => + source$.pipe( + distinctUntilChanged(), + publishReplay(1), + refCount() + );