Don't make storage queries with missing params (#631)

* Don't make storage queries with missing params

* Remove debug logs
This commit is contained in:
Jaco Greeff 2019-01-21 15:05:09 +01:00 committed by GitHub
parent 20a748451e
commit c52116adb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 35 deletions

View File

@ -128,7 +128,7 @@ class Preferences extends React.PureComponent<Props, State> {
}
}
private onChangeThreshold = (unstakeThreshold: BN = new BN(0)) => {
private onChangeThreshold = (unstakeThreshold?: BN) => {
if (unstakeThreshold) {
this.setState({ unstakeThreshold });
}

View File

@ -10,7 +10,7 @@ import './Content.css';
import React from 'react';
import { withRouter } from 'react-router';
import { withApi, withMulti } from '@polkadot/ui-api/index';
import { withCall, withMulti } from '@polkadot/ui-api/index';
import { QueueConsumer } from '@polkadot/ui-app/Status/Context';
import Status from '../Status';
@ -71,5 +71,5 @@ export default withMulti(
// React-router needs to be first, otherwise we have blocked updates
withRouter,
translate,
withApi
withCall('query.session.validators')
);

View File

@ -6,7 +6,7 @@ import { ApiProps, CallState, Subtract } from '../types';
import { Options } from './types';
import React from 'react';
import { assert, isUndefined } from '@polkadot/util';
import { assert, isNull, isUndefined } from '@polkadot/util';
import { isEqual, triggerChange } from '../util/index';
import echoTransform from '../transform/echo';
@ -17,11 +17,7 @@ interface Method {
at: (hash: Uint8Array | string, ...params: Array<any>) => Promise<any>;
}
type State = CallState & {
destroy?: () => void,
propName: string,
timerId: number
};
type State = CallState;
const NOOP = () => {
// ignore
@ -31,19 +27,21 @@ export default function withCall<P extends ApiProps> (endpoint: string, { at, at
return (Inner: React.ComponentType<ApiProps>): React.ComponentType<Subtract<P, ApiProps>> => {
class WithPromise extends React.Component<P, State> {
state: State;
isActive: boolean = true;
private destroy?: () => void;
private isActive: boolean = false;
private propName: string;
private timerId: number = -1;
constructor (props: P) {
super(props);
const [, section, method] = endpoint.split('.');
this.propName = `${section}_${method}`;
this.state = {
propName: `${section}_${method}`,
callResult: void 0,
callUpdated: false,
callUpdatedAt: 0,
timerId: -1
callUpdatedAt: 0
};
}
@ -60,7 +58,8 @@ export default function withCall<P extends ApiProps> (endpoint: string, { at, at
}
componentDidMount () {
const timerId = window.setInterval(() => {
this.isActive = true;
this.timerId = window.setInterval(() => {
const elapsed = Date.now() - (this.state.callUpdatedAt || 0);
const callUpdated = elapsed <= 1500;
@ -69,10 +68,6 @@ export default function withCall<P extends ApiProps> (endpoint: string, { at, at
}
}, 500);
this.nextState({
timerId
});
this
.subscribe(this.getParams(this.props))
.then(NOOP)
@ -80,15 +75,13 @@ export default function withCall<P extends ApiProps> (endpoint: string, { at, at
}
componentWillUnmount () {
const { timerId } = this.state;
this.isActive = false;
if (timerId !== -1) {
clearInterval(timerId);
}
this.unsubscribe();
if (this.timerId !== -1) {
clearInterval(this.timerId);
}
}
private nextState (state: Partial<State>) {
@ -136,6 +129,14 @@ export default function withCall<P extends ApiProps> (endpoint: string, { at, at
assert(apiSection && apiSection[method], `Unable to find api.${area}.${section}.${method}`);
const meta = apiSection[method].meta;
if (area === 'query' && meta && meta.type.isMap) {
const arg = newParams[0];
assert(!isUndefined(arg) && !isNull(arg), `${meta.name} expects one argument`);
}
return [
apiSection[method],
newParams,
@ -156,11 +157,9 @@ export default function withCall<P extends ApiProps> (endpoint: string, { at, at
this.unsubscribe();
if (isSubscription) {
const destroy = await apiMethod(...params, (value?: any) =>
this.destroy = await apiMethod(...params, (value?: any) =>
this.triggerUpdate(this.props, value)
);
this.nextState({ destroy });
} else {
const value: any = at
? await apiMethod.at(at, ...params)
@ -174,10 +173,9 @@ export default function withCall<P extends ApiProps> (endpoint: string, { at, at
}
private unsubscribe () {
const { destroy } = this.state;
if (destroy) {
destroy();
if (this.destroy) {
this.destroy();
this.destroy = undefined;
}
}
@ -207,7 +205,7 @@ export default function withCall<P extends ApiProps> (endpoint: string, { at, at
...this.props,
callUpdated,
callUpdatedAt,
[propName || this.state.propName]: callResult
[propName || this.propName]: callResult
};
return (

View File

@ -62,9 +62,7 @@ class InputNumber extends React.PureComponent<Props, State> {
: text
})),
siUnit: '-',
valueBN: this.props.value
? new BN(this.props.value)
: new BN(0)
valueBN: new BN(this.props.value || 0)
};
}