fix: remove cache from api-derive (#779)

* Remove cache from api-derive

* Skip e2e

* Update headers to 2019
This commit is contained in:
Amaury Martiny
2019-03-15 12:45:50 +01:00
committed by Jaco Greeff
parent a74367e924
commit 3e43bc5ea9
7 changed files with 35 additions and 77 deletions
-7
View File
@@ -22,13 +22,6 @@ const testFunction = (api: ApiInterface$Rx) => {
it('should return an Observable', () => {
expect((api.derive[section][method] as any)(...inputs)).toBeInstanceOf(Observable);
});
it('should be memoized', () => {
const first = (api.derive[section][method] as any)(...inputs);
const second = (api.derive[section][method] as any)(...inputs);
expect(first).toBe(second);
});
});
};
};
+1 -7
View File
@@ -5,7 +5,6 @@
import { Observable } from 'rxjs';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { cache } from './util/cache';
import * as accounts from './accounts';
import * as balances from './balances';
import * as chain from './chain';
@@ -49,13 +48,8 @@ function injectFunctions (api: ApiInterface$Rx, derive: Derive, functions: Deriv
const result = derive[sectionName as keyof Derive] = derive[sectionName as keyof Derive] || {};
Object.keys(section).forEach((methodName) => {
// Create cache for the section_method function
// @ts-ignore No idea how to make this work...
const cached = cache(section[methodName as keyof section]);
// Add this cached function into the result
// @ts-ignore No idea how to make this work...
result[methodName as keyof section] = cached(api);
result[methodName as keyof section] = section[methodName as keyof section](api);
});
});
@@ -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);
});
});
-31
View File
@@ -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);
};
@@ -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 ApiPromise from '@polkadot/api/promise/Api';
import { BlockNumber } from '@polkadot/types';
import { WsProvider } from '@polkadot/rpc-provider';
const WS_LOCAL = 'ws://127.0.0.1:9944/';
// const WS_POC3 = 'wss://poc3-rpc.polkadot.io/';
describe.skip('derive e2e', () => {
let api: ApiPromise;
beforeAll(() => {
jest.setTimeout(30000);
});
beforeEach(async (done) => {
api = await ApiPromise.create(new WsProvider(WS_LOCAL));
done();
});
it('returns correct results', async () => {
// https://github.com/polkadot-js/api/issues/777
const block1 = await api.derive.chain.bestNumber();
await new Promise((resolve) => setTimeout(resolve, 15000));
const block2 = await api.derive.chain.bestNumber();
expect((block1 as BlockNumber).eq(block2)).toBe(false);
});
});
@@ -1,4 +1,4 @@
// Copyright 2017-2018 @polkadot/api-derive 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-2018 @polkadot/types authors & contributors
// Copyright 2017-2019 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.