@polkadot/api/types#QueryableStorageMultiArg TypeScript Examples
The following examples show how to use
@polkadot/api/types#QueryableStorageMultiArg.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useCallMulti.ts From crust-apps with Apache License 2.0 | 6 votes |
// subscribe, trying to play nice with the browser threads
function subscribe <T> (api: ApiPromise, mountedRef: MountedRef, tracker: TrackerRef, calls: QueryableStorageMultiArg<'promise'>[], setValue: (value: T) => void, { transform = transformIdentity }: CallOptions<T> = {}): void {
unsubscribe(tracker);
setTimeout((): void => {
if (mountedRef.current) {
const included = calls.map((c) => !!c && (!Array.isArray(c) || !!c[0]));
const filtered = calls.filter((_, index) => included[index]);
if (filtered.length) {
// swap to active mode
tracker.current.isActive = true;
tracker.current.subscriber = api.queryMulti(filtered, (value): void => {
// we use the isActive flag here since .subscriber may not be set on immediate callback)
if (mountedRef.current && tracker.current.isActive) {
let valueIndex = -1;
mountedRef.current && tracker.current.isActive && setValue(
transform(
calls.map((_, index) => included[index] ? value[++valueIndex] : undefined)
)
);
}
});
} else {
tracker.current.subscriber = null;
}
}
}, 0);
}
Example #2
Source File: useCallMulti.ts From crust-apps with Apache License 2.0 | 6 votes |
// very much copied from useCall
export function useCallMulti <T> (calls?: QueryableStorageMultiArg<'promise'>[] | null | false, options?: CallOptions<T>): T {
const { api } = useApi();
const mountedRef = useIsMountedRef();
const tracker = useRef<Tracker>({ isActive: false, serialized: null, subscriber: null });
const [value, setValue] = useState<T>((options || {}).defaultValue || [] as unknown as T);
// initial effect, we need an un-subscription
useEffect((): () => void => {
return () => unsubscribe(tracker);
}, []);
// on changes, re-subscribe
useEffect((): void => {
// check if we have a function & that we are mounted
if (mountedRef.current && calls) {
const serialized = JSON.stringify(calls);
if (serialized !== tracker.current.serialized) {
tracker.current.serialized = serialized;
subscribe(api, mountedRef, tracker, calls, setValue, options);
}
}
}, [api, calls, options, mountedRef]);
return value;
}
Example #3
Source File: useCallMulti.ts From subscan-multisig-react with Apache License 2.0 | 6 votes |
// subscribe, trying to play nice with the browser threads
function subscribe<T>(
api: ApiPromise,
mountedRef: MountedRef,
tracker: TrackerRef,
calls: QueryableStorageMultiArg<'promise'>[],
setValue: (value: T) => void,
{ transform = transformIdentity }: CallOptions<T> = {}
): void {
unsubscribe(tracker);
setTimeout((): void => {
if (mountedRef.current) {
const included = calls.map((c) => !!c && (!Array.isArray(c) || !!c[0]));
const filtered = calls.filter((_, index) => included[index]);
if (filtered.length) {
// swap to active mode
tracker.current.isActive = true;
tracker.current.subscriber = api.queryMulti(filtered, (value): void => {
// we use the isActive flag here since .subscriber may not be set on immediate callback)
if (mountedRef.current && tracker.current.isActive) {
let valueIndex = -1;
// eslint-disable-next-line
mountedRef.current &&
tracker.current.isActive &&
setValue(transform(calls.map((_, index) => (included[index] ? value[++valueIndex] : undefined))));
}
});
} else {
tracker.current.subscriber = null;
}
}
}, 0);
}
Example #4
Source File: useCallMulti.ts From subscan-multisig-react with Apache License 2.0 | 6 votes |
// very much copied from useCall
export function useCallMulti<T>(
calls?: QueryableStorageMultiArg<'promise'>[] | null | false,
options?: CallOptions<T>
): T {
const { api } = useApi();
const mountedRef = useIsMountedRef();
const tracker = useRef<Tracker>({ isActive: false, serialized: null, subscriber: null });
const [value, setValue] = useState<T>((options || {}).defaultValue || ([] as unknown as T));
// initial effect, we need an un-subscription
useEffect((): (() => void) => {
return () => unsubscribe(tracker);
}, []);
// on changes, re-subscribe
useEffect((): void => {
// check if we have a function & that we are mounted
if (mountedRef.current && calls) {
const serialized = JSON.stringify(calls);
if (serialized !== tracker.current.serialized) {
tracker.current.serialized = serialized;
subscribe(api, mountedRef, tracker, calls, setValue, options);
}
}
}, [api, calls, options, mountedRef]);
return value;
}