Inject custom derives (#711)

This commit is contained in:
Jaco Greeff
2019-02-18 11:58:50 +01:00
committed by GitHub
parent 1a3134fee1
commit b16ccb253a
4 changed files with 86 additions and 31 deletions
+53 -22
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Observable } from 'rxjs';
import { Observable, from } from 'rxjs';
import ApiRx from '@polkadot/api/rx';
import { ApiInterface$Rx } from '@polkadot/api/types';
import MockProvider from '@polkadot/rpc-provider/mock';
@@ -34,31 +34,62 @@ const testFunction = (api: ApiInterface$Rx) => {
};
describe('derive', () => {
const api = new ApiRx(new MockProvider());
describe('builtin', () => {
const api = new ApiRx(new MockProvider());
beforeAll((done) => api.isReady.subscribe(() => done()));
beforeAll((done) => {
api.isReady.subscribe(() => done());
});
testFunction(api)('accounts', 'idAndIndex', []);
testFunction(api)('accounts', 'idToIndex', []);
testFunction(api)('accounts', 'indexes', []);
testFunction(api)('accounts', 'indexToId', []);
testFunction(api)('accounts', 'idAndIndex', []);
testFunction(api)('accounts', 'idToIndex', []);
testFunction(api)('accounts', 'indexes', []);
testFunction(api)('accounts', 'indexToId', []);
testFunction(api)('balances', 'fees', []);
testFunction(api)('balances', 'validatingBalance', []);
testFunction(api)('balances', 'validatingBalances', []);
testFunction(api)('balances', 'votingBalance', []);
testFunction(api)('balances', 'votingBalances', []);
testFunction(api)('balances', 'votingBalancesNominatorsFor', []);
testFunction(api)('balances', 'fees', []);
testFunction(api)('balances', 'validatingBalance', []);
testFunction(api)('balances', 'validatingBalances', []);
testFunction(api)('balances', 'votingBalance', []);
testFunction(api)('balances', 'votingBalances', []);
testFunction(api)('balances', 'votingBalancesNominatorsFor', []);
testFunction(api)('chain', 'bestNumber', []);
testFunction(api)('chain', 'bestNumberFinalised', []);
testFunction(api)('chain', 'bestNumber', []);
testFunction(api)('chain', 'bestNumberFinalised', []);
testFunction(api)('democracy', 'referendumInfos', []);
testFunction(api)('democracy', 'referendums', []);
testFunction(api)('democracy', 'referendumVotesFor', []);
testFunction(api)('democracy', 'votes', []);
testFunction(api)('democracy', 'referendumInfos', []);
testFunction(api)('democracy', 'referendums', []);
testFunction(api)('democracy', 'referendumVotesFor', []);
testFunction(api)('democracy', 'votes', []);
testFunction(api)('session', 'eraLength', []);
testFunction(api)('session', 'eraProgress', []);
testFunction(api)('session', 'sessionProgress', []);
testFunction(api)('session', 'eraLength', []);
testFunction(api)('session', 'eraProgress', []);
testFunction(api)('session', 'sessionProgress', []);
});
describe('custom', () => {
const api = new ApiRx({
derives: {
balances: {
fees: () => () => from(['a', 'b'])
},
custom: {
test: () => () => from([1, 2, 3])
}
},
provider: new MockProvider()
});
beforeAll((done) => {
api.isReady.subscribe(() => done());
});
// override
testFunction(api)('balances', 'fees', ['a', 'b']);
// new
testFunction(api)('custom' as any, 'test', [1, 2, 3]);
// existing
testFunction(api)('chain', 'bestNumber', []);
});
});
+27 -8
View File
@@ -2,15 +2,16 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
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';
import * as democracy from './democracy';
import * as session from './session';
import * as staking from './staking';
import { cache } from './util/cache';
// Put all derived functions in an object, for easier Object.keys()-ing.
const functions = { accounts, balances, chain, democracy, session, staking };
@@ -23,6 +24,16 @@ type ReturnTypes<T extends Record<keyof T, (...args: any[]) => any>> = {
[P in keyof T]: ReturnType<T[P]>
};
export interface DeriveCustomMethod {
(api: ApiInterface$Rx): (...args: Array<any>) => Observable<any>;
}
export interface DeriveCustom {
[index: string]: {
[index: string]: DeriveCustomMethod
};
}
export interface Derive {
accounts: ReturnTypes<typeof accounts>;
balances: ReturnTypes<typeof balances>;
@@ -32,22 +43,30 @@ export interface Derive {
staking: ReturnTypes<typeof staking>;
}
export default function decorateDerive (api: ApiInterface$Rx): Derive {
const derive = {} as Derive;
function injectFunctions (api: ApiInterface$Rx, derive: Derive, functions: DeriveCustom): Derive {
Object.keys(functions).forEach((sectionName: string) => {
const section = functions[sectionName as keyof Derive];
derive[sectionName as keyof Derive] = Object.keys(section).reduce((result, methodName) => {
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);
return result;
}, {} as ReturnTypes<typeof section>);
});
});
return derive;
}
export default function decorateDerive (api: ApiInterface$Rx, custom: DeriveCustom = {}): Derive {
const derive = {} as Derive;
injectFunctions(api, derive, functions);
injectFunctions(api, derive, custom);
return derive;
}