From 1b6bd4eeaf0e1eff43201b51cc77cc5e7a931c1c Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 9 Jul 2020 18:08:08 +0200 Subject: [PATCH] Allow final prcedence for registry types (#3154) --- packages/apps/src/initSettings.ts | 13 ------------- packages/react-api/src/Api.tsx | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/apps/src/initSettings.ts b/packages/apps/src/initSettings.ts index 3be57a16f1..0c1cafb675 100644 --- a/packages/apps/src/initSettings.ts +++ b/packages/apps/src/initSettings.ts @@ -5,7 +5,6 @@ import queryString from 'query-string'; import store from 'store'; import { createEndpoints } from '@polkadot/apps-config/settings'; -import { registry } from '@polkadot/react-api'; import { extractIpfsDetails } from '@polkadot/react-hooks/useIpfs'; import settings from '@polkadot/ui-settings'; @@ -53,15 +52,3 @@ const apiUrl = getApiUrl(); settings.set({ apiUrl }); console.log('WS endpoint=', apiUrl); - -try { - const types = store.get('types') as Record> || {}; - const names = Object.keys(types); - - if (names.length) { - registry.register(types); - console.log('Type registration:', names.join(', ')); - } -} catch (error) { - console.error('Type registration failed', error); -} diff --git a/packages/react-api/src/Api.tsx b/packages/react-api/src/Api.tsx index 59bbf30faf..590f1b3a0d 100644 --- a/packages/react-api/src/Api.tsx +++ b/packages/react-api/src/Api.tsx @@ -7,6 +7,7 @@ import { ChainProperties, ChainType } from '@polkadot/types/interfaces'; import { ApiProps, ApiState } from './types'; import React, { useContext, useEffect, useMemo, useState } from 'react'; +import store from 'store'; import ApiPromise from '@polkadot/api/promise'; import { setDeriveCache, deriveMapCache } from '@polkadot/api-derive/util'; import { typesChain, typesSpec } from '@polkadot/apps-config/api'; @@ -57,6 +58,15 @@ let api: ApiPromise; export { api }; +function getDevTypes (): Record> { + const types = store.get('types', {}) as Record>; + const names = Object.keys(types); + + names.length && console.log('Injected types:', names.join(', ')); + + return types; +} + async function retrieve (api: ApiPromise): Promise { const [properties, systemChain, systemChainType, systemName, systemVersion, injectedAccounts] = await Promise.all([ api.rpc.system.properties(), @@ -93,7 +103,9 @@ async function retrieve (api: ApiPromise): Promise { }; } -async function loadOnReady (api: ApiPromise, store?: KeyringStore): Promise { +async function loadOnReady (api: ApiPromise, store: KeyringStore | undefined, types: Record>): Promise { + registry.register(types); + const { injectedAccounts, properties, systemChain, systemChainType, systemName, systemVersion } = await retrieve(api); const ss58Format = uiSettings.prefix === -1 ? properties.ss58Format.unwrapOr(DEFAULT_SS58).toNumber() @@ -162,14 +174,15 @@ function Api ({ children, store, url }: Props): React.ReactElement | null useEffect((): void => { const provider = new WsProvider(url); const signer = new ApiSigner(queuePayload, queueSetTxStatus); + const types = getDevTypes(); - api = new ApiPromise({ provider, registry, signer, typesChain, typesSpec }); + api = new ApiPromise({ provider, registry, signer, types, typesChain, typesSpec }); api.on('connected', () => setIsApiConnected(true)); api.on('disconnected', () => setIsApiConnected(false)); api.on('ready', async (): Promise => { try { - setState(await loadOnReady(api, store)); + setState(await loadOnReady(api, store, types)); } catch (error) { console.error('Unable to load chain', error); }