import { ApiPromise } from '@polkadot/api' ; import { logger } from '@polkadot/util' ; import React , { useEffect , useState } from 'react' ; import { ApiRxContextProviderProps } from './types' ; import { useDidUpdateEffect } from './util' ; export interface ApiPromiseContextType { api : ApiPromise ; isApiReady : boolean ; } const l = logger ( 'api-context' ) ; export const ApiPromiseContext = React . createContext ( { } as ApiPromiseContextType ) ; export function ApiPromiseContextProvider ( props ) { const { children = null , provider } = props ; const [ apiPromise , setApiPromise ] = useState < ApiPromise > ( new ApiPromise ( { provider } ) ) ; const [ isReady , setIsReady ] = useState ( false ) ; useDidUpdateEffect ( ( ) => { setApiPromise ( new ApiPromise ( { provider } ) ) ; setIsReady ( false ) ; } , [ provider ] ) ; useEffect ( ( ) => { apiPromise . isReady . then ( _ => { l . log ( `Api ready.` ) ; setIsReady ( true ) ; } ) ; } , [ apiPromise . isReady ] ) ; return ( < ApiPromiseContext . Provider value = { { api : apiPromise , isApiReady : isReady } } > { children } < /ApiPromiseContext.Provider>); }