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
@@ -0,0 +1,30 @@
// 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';
describe('cache', () => {
const f = cache((api: ApiRx) => (a: number, b: number) => [api, a, b]);
const api = {} as ApiRx;
it('should hit cache when all arguments are the same', () => {
const first = f(api)(2, 3);
const second = f(api)(2, 3);
expect(first).toBe(second);
});
it('should not hit cache when some arguments are not the same', () => {
const first = f(api)(2, 3);
const second1 = f(api)(5, 3);
const second2 = f(api)(2, 5);
const second3 = f({} as ApiRx)(2, 3);
expect(first).not.toBe(second1);
expect(first).not.toBe(second2);
expect(first).not.toBe(second3);
});
});
+30
View File
@@ -0,0 +1,30 @@
// 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 memoizee from 'memoizee';
import ApiRx from '@polkadot/api/rx';
/**
* Create memoization for a 2-currified function.
*
* @param fn - The function to memoize.
* @param map - The Map used to cache function returns.
* @example
* ```javascript
* const f = (api: ApiRx) => (b: number) => a * b;
* const cached = cache(f);
* f(api)(1, 2);
* f(api)(1); // Cache not hit
* f({})(1, 2); // Cache not hit
* f(api)(1, 2); // Cache hit
* ```
*/
export const cache = (fn: Function, map = new WeakMap()) => (api: ApiRx) => {
if (!map.has(api)) {
const innerFn = fn(api);
map.set(api, memoizee(innerFn));
}
return map.get(api);
};
+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()
);