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
+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);
};