From c52116adb9686f1e9ee6554e3a01e6e4b3fcfb41 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 21 Jan 2019 15:05:09 +0100 Subject: [PATCH] Don't make storage queries with missing params (#631) * Don't make storage queries with missing params * Remove debug logs --- .../app-staking/src/StakeList/Preferences.tsx | 2 +- packages/apps/src/Content/index.tsx | 4 +- packages/ui-api/src/with/call.tsx | 56 +++++++++---------- packages/ui-app/src/InputNumber.tsx | 4 +- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/packages/app-staking/src/StakeList/Preferences.tsx b/packages/app-staking/src/StakeList/Preferences.tsx index 1c05450c2b..3c4586743b 100644 --- a/packages/app-staking/src/StakeList/Preferences.tsx +++ b/packages/app-staking/src/StakeList/Preferences.tsx @@ -128,7 +128,7 @@ class Preferences extends React.PureComponent { } } - private onChangeThreshold = (unstakeThreshold: BN = new BN(0)) => { + private onChangeThreshold = (unstakeThreshold?: BN) => { if (unstakeThreshold) { this.setState({ unstakeThreshold }); } diff --git a/packages/apps/src/Content/index.tsx b/packages/apps/src/Content/index.tsx index 4b531ce3f2..f5f18b607e 100644 --- a/packages/apps/src/Content/index.tsx +++ b/packages/apps/src/Content/index.tsx @@ -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') ); diff --git a/packages/ui-api/src/with/call.tsx b/packages/ui-api/src/with/call.tsx index 24639588a5..63fbe29a2b 100644 --- a/packages/ui-api/src/with/call.tsx +++ b/packages/ui-api/src/with/call.tsx @@ -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) => Promise; } -type State = CallState & { - destroy?: () => void, - propName: string, - timerId: number -}; +type State = CallState; const NOOP = () => { // ignore @@ -31,19 +27,21 @@ export default function withCall

(endpoint: string, { at, at return (Inner: React.ComponentType): React.ComponentType> => { class WithPromise extends React.Component { 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

(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

(endpoint: string, { at, at } }, 500); - this.nextState({ - timerId - }); - this .subscribe(this.getParams(this.props)) .then(NOOP) @@ -80,15 +75,13 @@ export default function withCall

(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) { @@ -136,6 +129,14 @@ export default function withCall

(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

(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

(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

(endpoint: string, { at, at ...this.props, callUpdated, callUpdatedAt, - [propName || this.state.propName]: callResult + [propName || this.propName]: callResult }; return ( diff --git a/packages/ui-app/src/InputNumber.tsx b/packages/ui-app/src/InputNumber.tsx index 5e34012f3d..c2c1ea0ce2 100644 --- a/packages/ui-app/src/InputNumber.tsx +++ b/packages/ui-app/src/InputNumber.tsx @@ -62,9 +62,7 @@ class InputNumber extends React.PureComponent { : text })), siUnit: '-', - valueBN: this.props.value - ? new BN(this.props.value) - : new BN(0) + valueBN: new BN(this.props.value || 0) }; }