Allow useCall to not have .entries as any hack (#4115)
This commit is contained in:
parent
bbc4f9021d
commit
ef125783de
@ -190,9 +190,9 @@ export default function useScheduled (): EntryInfo[] {
|
||||
const councilMotions = useCall<DeriveCollectiveProposal[]>(api.derive.council?.proposals);
|
||||
const dispatches = useCall<DeriveDispatch[]>(api.derive.democracy?.dispatchQueue);
|
||||
const referendums = useCall<DeriveReferendumExt[]>(api.derive.democracy?.referendums);
|
||||
const scheduled = useCall<ScheduleEntry[]>(api.query.scheduler?.agenda.entries as any);
|
||||
const scheduled = useCall<ScheduleEntry[]>(api.query.scheduler?.agenda.entries);
|
||||
const sessionInfo = useCall<DeriveSessionProgress>(api.query.staking && api.derive.session?.progress);
|
||||
const slashes = useCall<SlashEntry[]>(api.query.staking?.unappliedSlashes.entries as any);
|
||||
const slashes = useCall<SlashEntry[]>(api.query.staking?.unappliedSlashes.entries);
|
||||
const [state, setState] = useState<EntryInfo[]>([]);
|
||||
|
||||
useEffect((): void => {
|
||||
|
||||
@ -38,7 +38,7 @@ const transformEntries = {
|
||||
function Schedule ({ className = '' }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const items = useCall<ScheduledExt[]>(api.query.scheduler.agenda.entries as any, undefined, transformEntries);
|
||||
const items = useCall<ScheduledExt[]>(api.query.scheduler.agenda.entries, undefined, transformEntries);
|
||||
|
||||
const headerRef = useRef([
|
||||
[t('scheduled'), 'start'],
|
||||
|
||||
@ -33,7 +33,7 @@ const transformProposals = {
|
||||
function Overview ({ isMine, sudoKey }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const parachains = useCall<DeriveParachain[]>(api.derive.parachains?.overview);
|
||||
const proposals = useCall<ProposalExt[]>(api.query.proposeParachain?.proposals.entries as any, undefined, transformProposals);
|
||||
const proposals = useCall<ProposalExt[]>(api.query.proposeParachain?.proposals.entries, undefined, transformProposals);
|
||||
const nextFreeId = useCall<BN>(api.query.registrar?.nextFreeId);
|
||||
|
||||
return (
|
||||
|
||||
@ -98,7 +98,7 @@ function CurrentList ({ favorites, hasQueries, isIntentions, next, stakingOvervi
|
||||
const { api } = useApi();
|
||||
const { byAuthor, eraPoints } = useContext(isIntentions ? EmptyAuthorsContext : BlockAuthorsContext);
|
||||
const recentlyOnline = useCall<DeriveHeartbeats>(!isIntentions && api.derive.imOnline?.receivedHeartbeats);
|
||||
const nominators = useCall<[StorageKey, Option<Nominations>][]>(isIntentions && api.query.staking.nominators.entries as any);
|
||||
const nominators = useCall<[StorageKey, Option<Nominations>][]>(isIntentions && api.query.staking.nominators.entries);
|
||||
const [nameFilter, setNameFilter] = useState<string>('');
|
||||
const [withIdentity, setWithIdentity] = useState(false);
|
||||
|
||||
|
||||
@ -14,7 +14,6 @@ export type CallParams = [] | CallParam[];
|
||||
|
||||
export interface CallOptions <T> {
|
||||
defaultValue?: T;
|
||||
isSingle?: boolean;
|
||||
paramMap?: (params: any) => CallParams;
|
||||
transform?: (value: any) => T;
|
||||
withParams?: boolean;
|
||||
|
||||
@ -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(<blokHash>, <account>, (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<unknown>;
|
||||
|
||||
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 <T> (mountedRef: MountedRef, tracker: TrackerRef, fn: TrackFn | undefined, params: CallParams, setValue: (value: T) => void, { isSingle, transform = transformIdentity, withParams, withParamsTransform }: CallOptions<T> = {}): void {
|
||||
function subscribe <T> (mountedRef: MountedRef, tracker: TrackerRef, fn: TrackFn | undefined, params: CallParams, setValue: (value: T) => void, { transform = transformIdentity, withParams, withParamsTransform }: CallOptions<T> = {}): void {
|
||||
const validParams = params.filter((p) => !isUndefined(p));
|
||||
|
||||
unsubscribe(tracker);
|
||||
@ -66,16 +71,12 @@ function subscribe <T> (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 <T> (mountedRef: MountedRef, tracker: TrackerRef, fn: TrackFn
|
||||
// FIXME The typings here need some serious TLC
|
||||
export function useCall <T> (fn: TrackFn | undefined | null | false, params?: CallParams, options?: CallOptions<T>): T | undefined {
|
||||
const mountedRef = useIsMountedRef();
|
||||
const tracker = useRef<Tracker>({ count: 0, isActive: false, serialized: null, subscriber: null });
|
||||
const tracker = useRef<Tracker>({ isActive: false, serialized: null, subscriber: null });
|
||||
const [value, setValue] = useState<T | undefined>((options || {}).defaultValue);
|
||||
|
||||
// initial effect, we need an un-subscription
|
||||
useEffect((): () => void => {
|
||||
return (): void => unsubscribe(tracker);
|
||||
return () => unsubscribe(tracker);
|
||||
}, []);
|
||||
|
||||
// on changes, re-subscribe
|
||||
|
||||
Loading…
Reference in New Issue
Block a user