Add isMulti param for withCall (#1090)

This commit is contained in:
Jaco Greeff 2019-05-15 16:11:26 +02:00 committed by GitHub
parent 24c5d8a263
commit 7887f56336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View File

@ -149,9 +149,10 @@ class Address extends React.PureComponent<Props, State> {
private getNominators () {
const { staking_stakers } = this.props;
return staking_stakers
? staking_stakers.others.map(({ who, value }): [AccountId, Balance] => [who, value])
: [];
? staking_stakers.others.map(({ who, value }): [AccountId, Balance] => [who, value])
: [];
}
private iNominated () {

View File

@ -15,6 +15,7 @@ import withApi from './api';
interface Method {
(...params: Array<any>): Promise<any>;
at: (hash: Uint8Array | string, ...params: Array<any>) => Promise<any>;
multi: (params: Array<any>, cb: (value?: any) => void) => Promise<any>;
}
type State = CallState;
@ -23,7 +24,7 @@ const NOOP = () => {
// ignore
};
export default function withCall<P extends ApiProps> (endpoint: string, { at, atProp, callOnResult, params = [], paramName, paramValid = false, propName, transform = echoTransform }: Options = {}): (Inner: React.ComponentType<ApiProps>) => React.ComponentType<any> {
export default function withCall<P extends ApiProps> (endpoint: string, { at, atProp, callOnResult, isMulti = false, params = [], paramName, paramValid = false, propName, transform = echoTransform }: Options = {}): (Inner: React.ComponentType<ApiProps>) => React.ComponentType<any> {
return (Inner: React.ComponentType<ApiProps>): React.ComponentType<Subtract<P, ApiProps>> => {
class WithPromise extends React.Component<P, State> {
state: State = {
@ -134,7 +135,7 @@ export default function withCall<P extends ApiProps> (endpoint: string, { at, at
assert(area.length && section.length && method.length && others.length === 0, `Invalid API format, expected <area>.<section>.<method>, found ${endpoint}`);
assert(['rpc', 'query', 'derive'].includes(area), `Unknown api.${area}, expected rpc, query or derive`);
assert(!at || area === 'query', 'Only able todo an at query on the api.query interface');
assert(!at || area === 'query', `Only able to do an 'at' query on the api.query interface`);
const apiSection = (api as any)[area][section];
@ -172,9 +173,12 @@ export default function withCall<P extends ApiProps> (endpoint: string, { at, at
await this.unsubscribe();
if (isSubscription) {
this.destroy = await apiMethod(...params, (value?: any) =>
this.triggerUpdate(this.props, value)
);
const updateCb = (value?: any) =>
this.triggerUpdate(this.props, value);
this.destroy = isMulti
? await apiMethod.multi(params, updateCb)
: await apiMethod(...params, updateCb);
} else {
const value: any = at
? await apiMethod.at(at, ...params)

View File

@ -17,6 +17,7 @@ export type Options = {
at?: Uint8Array | string,
atProp?: string,
callOnResult?: OnChangeCb,
isMulti?: boolean,
params?: Array<any>,
paramName?: string,
paramValid?: boolean,