From ef125783dec4bb1b7608932cb882eb1caf0270dd Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 30 Nov 2020 13:11:51 +0100 Subject: [PATCH] Allow useCall to not have .entries as any hack (#4115) --- packages/page-calendar/src/useScheduled.ts | 4 +-- .../page-democracy/src/Execute/Scheduler.tsx | 2 +- .../page-parachains/src/Overview/index.tsx | 2 +- .../page-staking/src/Overview/CurrentList.tsx | 2 +- packages/react-hooks/src/types.ts | 1 - packages/react-hooks/src/useCall.ts | 27 ++++++++++--------- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/page-calendar/src/useScheduled.ts b/packages/page-calendar/src/useScheduled.ts index f1f54f531a..f8f14970b8 100644 --- a/packages/page-calendar/src/useScheduled.ts +++ b/packages/page-calendar/src/useScheduled.ts @@ -190,9 +190,9 @@ export default function useScheduled (): EntryInfo[] { const councilMotions = useCall(api.derive.council?.proposals); const dispatches = useCall(api.derive.democracy?.dispatchQueue); const referendums = useCall(api.derive.democracy?.referendums); - const scheduled = useCall(api.query.scheduler?.agenda.entries as any); + const scheduled = useCall(api.query.scheduler?.agenda.entries); const sessionInfo = useCall(api.query.staking && api.derive.session?.progress); - const slashes = useCall(api.query.staking?.unappliedSlashes.entries as any); + const slashes = useCall(api.query.staking?.unappliedSlashes.entries); const [state, setState] = useState([]); useEffect((): void => { diff --git a/packages/page-democracy/src/Execute/Scheduler.tsx b/packages/page-democracy/src/Execute/Scheduler.tsx index 7bc8f7be3f..70bc4cacb2 100644 --- a/packages/page-democracy/src/Execute/Scheduler.tsx +++ b/packages/page-democracy/src/Execute/Scheduler.tsx @@ -38,7 +38,7 @@ const transformEntries = { function Schedule ({ className = '' }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); - const items = useCall(api.query.scheduler.agenda.entries as any, undefined, transformEntries); + const items = useCall(api.query.scheduler.agenda.entries, undefined, transformEntries); const headerRef = useRef([ [t('scheduled'), 'start'], diff --git a/packages/page-parachains/src/Overview/index.tsx b/packages/page-parachains/src/Overview/index.tsx index 836d29bb89..0fe43f6bd5 100644 --- a/packages/page-parachains/src/Overview/index.tsx +++ b/packages/page-parachains/src/Overview/index.tsx @@ -33,7 +33,7 @@ const transformProposals = { function Overview ({ isMine, sudoKey }: Props): React.ReactElement { const { api } = useApi(); const parachains = useCall(api.derive.parachains?.overview); - const proposals = useCall(api.query.proposeParachain?.proposals.entries as any, undefined, transformProposals); + const proposals = useCall(api.query.proposeParachain?.proposals.entries, undefined, transformProposals); const nextFreeId = useCall(api.query.registrar?.nextFreeId); return ( diff --git a/packages/page-staking/src/Overview/CurrentList.tsx b/packages/page-staking/src/Overview/CurrentList.tsx index 88d01c9846..761f516ce8 100644 --- a/packages/page-staking/src/Overview/CurrentList.tsx +++ b/packages/page-staking/src/Overview/CurrentList.tsx @@ -98,7 +98,7 @@ function CurrentList ({ favorites, hasQueries, isIntentions, next, stakingOvervi const { api } = useApi(); const { byAuthor, eraPoints } = useContext(isIntentions ? EmptyAuthorsContext : BlockAuthorsContext); const recentlyOnline = useCall(!isIntentions && api.derive.imOnline?.receivedHeartbeats); - const nominators = useCall<[StorageKey, Option][]>(isIntentions && api.query.staking.nominators.entries as any); + const nominators = useCall<[StorageKey, Option][]>(isIntentions && api.query.staking.nominators.entries); const [nameFilter, setNameFilter] = useState(''); const [withIdentity, setWithIdentity] = useState(false); diff --git a/packages/react-hooks/src/types.ts b/packages/react-hooks/src/types.ts index bc5d4eee34..2ea334294f 100644 --- a/packages/react-hooks/src/types.ts +++ b/packages/react-hooks/src/types.ts @@ -14,7 +14,6 @@ export type CallParams = [] | CallParam[]; export interface CallOptions { defaultValue?: T; - isSingle?: boolean; paramMap?: (params: any) => CallParams; transform?: (value: any) => T; withParams?: boolean; diff --git a/packages/react-hooks/src/useCall.ts b/packages/react-hooks/src/useCall.ts index 94330b79df..9e9e098a29 100644 --- a/packages/react-hooks/src/useCall.ts +++ b/packages/react-hooks/src/useCall.ts @@ -10,7 +10,13 @@ import { isNull, isUndefined } from '@polkadot/util'; import { useIsMountedRef, MountedRef } from './useIsMountedRef'; -type TrackFnResult = Promise<() => void>; +type VoidFn = () => void; + +// This should be VoidFn, however the API actually does allow us to use any general single-shot queries with +// a result callback, so `api.query.system.account.at(, , (info) => {... })` does work +// (The same applies to e.g. keys or entries). So where we actally use the unsub, we cast `unknown` to `VoidFn` +// to cater for our usecase. +type TrackFnResult = Promise; interface TrackFn { (...params: CallParam[]): TrackFnResult; @@ -23,7 +29,6 @@ interface TrackFn { interface Tracker { isActive: boolean; - count: number; serialized: string | null; subscriber: TrackFnResult | null; } @@ -52,13 +57,13 @@ function unsubscribe (tracker: TrackerRef): void { tracker.current.isActive = false; if (tracker.current.subscriber) { - tracker.current.subscriber.then((unsubFn) => unsubFn()).catch(console.error); + tracker.current.subscriber.then((unsubFn) => (unsubFn as VoidFn)()).catch(console.error); tracker.current.subscriber = null; } } // subscribe, trying to play nice with the browser threads -function subscribe (mountedRef: MountedRef, tracker: TrackerRef, fn: TrackFn | undefined, params: CallParams, setValue: (value: T) => void, { isSingle, transform = transformIdentity, withParams, withParamsTransform }: CallOptions = {}): void { +function subscribe (mountedRef: MountedRef, tracker: TrackerRef, fn: TrackFn | undefined, params: CallParams, setValue: (value: T) => void, { transform = transformIdentity, withParams, withParamsTransform }: CallOptions = {}): void { const validParams = params.filter((p) => !isUndefined(p)); unsubscribe(tracker); @@ -66,16 +71,12 @@ function subscribe (mountedRef: MountedRef, tracker: TrackerRef, fn: TrackFn setTimeout((): void => { if (mountedRef.current) { if (fn && (!fn.meta || !fn.meta.type?.isDoubleMap || validParams.length === 2)) { - // swap to acive mode and reset our count + // swap to acive mode tracker.current.isActive = true; - tracker.current.count = 0; tracker.current.subscriber = (fn as (...params: unknown[]) => Promise<() => void>)(...params, (value: Codec): void => { - // when we don't have an active sub, or single-shot, ignore (we use the isActive flag here - // since .subscriber may not be set on immediate callback) - if (mountedRef.current && tracker.current.isActive && (!isSingle || !tracker.current.count)) { - tracker.current.count++; - + // we use the isActive flag here since .subscriber may not be set on immediate callback) + if (mountedRef.current && tracker.current.isActive) { mountedRef.current && tracker.current.isActive && setValue( withParams ? [params, transform(value)] as any @@ -98,12 +99,12 @@ function subscribe (mountedRef: MountedRef, tracker: TrackerRef, fn: TrackFn // FIXME The typings here need some serious TLC export function useCall (fn: TrackFn | undefined | null | false, params?: CallParams, options?: CallOptions): T | undefined { const mountedRef = useIsMountedRef(); - const tracker = useRef({ count: 0, isActive: false, serialized: null, subscriber: null }); + const tracker = useRef({ isActive: false, serialized: null, subscriber: null }); const [value, setValue] = useState((options || {}).defaultValue); // initial effect, we need an un-subscription useEffect((): () => void => { - return (): void => unsubscribe(tracker); + return () => unsubscribe(tracker); }, []); // on changes, re-subscribe