fix: remove cache from api-derive (#779)
* Remove cache from api-derive * Skip e2e * Update headers to 2019
This commit is contained in:
committed by
Jaco Greeff
parent
a74367e924
commit
3e43bc5ea9
@@ -1,30 +0,0 @@
|
||||
// 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 { ApiInterface$Rx } from '@polkadot/api/types';
|
||||
|
||||
import { cache } from './cache';
|
||||
|
||||
describe('cache', () => {
|
||||
const f = cache((api: ApiInterface$Rx) => (a: number, b: number) => [api, a, b]);
|
||||
const api = {} as ApiInterface$Rx;
|
||||
|
||||
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 ApiInterface$Rx)(2, 3);
|
||||
|
||||
expect(first).not.toBe(second1);
|
||||
expect(first).not.toBe(second2);
|
||||
expect(first).not.toBe(second3);
|
||||
});
|
||||
});
|
||||
@@ -1,31 +0,0 @@
|
||||
// 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 { ApiInterface$Decorated } from '@polkadot/api/types';
|
||||
import { Codec } from '@polkadot/types/types';
|
||||
import { Observable } from 'rxjs';
|
||||
/**
|
||||
* 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: ApiInterface$Rx) => (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: ApiInterface$Decorated<Observable<Codec>, Observable<Codec>>) => {
|
||||
if (!map.has(api)) {
|
||||
const innerFn = fn(api);
|
||||
map.set(api, memoizee(innerFn));
|
||||
}
|
||||
|
||||
return map.get(api);
|
||||
};
|
||||
Reference in New Issue
Block a user