patch observable and substrate client with next version

This commit is contained in:
Victor Oliva
2024-10-30 11:58:12 +01:00
parent fa315aa72c
commit 23b05a85a3
7 changed files with 107 additions and 13 deletions

View File

@@ -60,7 +60,9 @@
"packageManager": "pnpm@9.12.2+sha512.22721b3a11f81661ae1ec68ce1a7b879425a1ca5b991c975b074ac220b187ce56c708fe5db69f4c962c989452eee76c82877f4ee80f474cebd61ee13461b6228",
"pnpm": {
"patchedDependencies": {
"react18-json-view@0.2.8": "patches/react18-json-view@0.2.8.patch"
"react18-json-view@0.2.8": "patches/react18-json-view@0.2.8.patch",
"@polkadot-api/observable-client": "patches/@polkadot-api__observable-client.patch",
"@polkadot-api/substrate-client": "patches/@polkadot-api__substrate-client.patch"
}
}
}

View File

@@ -0,0 +1,44 @@
diff --git a/dist/esm/getObservableClient.mjs b/dist/esm/getObservableClient.mjs
index 511be34f2b04e0125858e03ff1dfce2759d3041b..b86e02a655903e3f5277269c5f46ca03b00b7ada 100644
--- a/dist/esm/getObservableClient.mjs
+++ b/dist/esm/getObservableClient.mjs
@@ -2,15 +2,30 @@ import { getChainHead$ } from './chainHead/chainHead.mjs';
import 'rxjs';
import getBroadcastTx$ from './tx.mjs';
-const getObservableClient = ({
- chainHead,
- transaction,
- destroy
-}) => ({
- chainHead$: () => getChainHead$(chainHead),
- broadcastTx$: getBroadcastTx$(transaction),
- destroy
-});
+const clientCache = /* @__PURE__ */ new Map();
+const getObservableClient = (substrateClient) => {
+ const cached = clientCache.get(substrateClient);
+ if (cached) {
+ cached.refCount++;
+ return cached.client;
+ }
+ const destroy = () => {
+ const cached2 = clientCache.get(substrateClient);
+ if (!cached2 || cached2.refCount <= 1) {
+ clientCache.delete(substrateClient);
+ substrateClient.destroy();
+ } else {
+ cached2.refCount--;
+ }
+ };
+ const client = {
+ chainHead$: () => getChainHead$(substrateClient.chainHead),
+ broadcastTx$: getBroadcastTx$(substrateClient.transaction),
+ destroy
+ };
+ clientCache.set(substrateClient, { client, refCount: 1 });
+ return client;
+};
export { getObservableClient };
//# sourceMappingURL=getObservableClient.mjs.map

View File

@@ -0,0 +1,42 @@
diff --git a/dist/esm/index.mjs b/dist/esm/index.mjs
index 67004332b2bd4d8ce97a6e2c04f64a810597a95a..6219a52e26932c9b9f698eeb2491281175660b16 100644
--- a/dist/esm/index.mjs
+++ b/dist/esm/index.mjs
@@ -8,18 +8,35 @@ import { abortablePromiseFn } from './internal-utils/abortablePromiseFn.mjs';
import { createGetChainSpec } from './chainspec.mjs';
export { AbortError } from '@polkadot-api/utils';
+const clientCache = /* @__PURE__ */ new Map();
const createClient = (provider) => {
+ const cached = clientCache.get(provider);
+ if (cached) {
+ cached.refCount++;
+ return cached.client;
+ }
const { request, disconnect } = createClient$1(provider);
- return {
+ const destroy = () => {
+ const cached2 = clientCache.get(provider);
+ if (!cached2 || cached2.refCount <= 1) {
+ clientCache.delete(provider);
+ disconnect();
+ } else {
+ cached2.refCount--;
+ }
+ };
+ const client = {
chainHead: getChainHead(request),
transaction: getTransaction(request),
getChainSpecData: createGetChainSpec(request),
- destroy: disconnect,
+ destroy,
request: abortablePromiseFn(
(onSuccess, onError, method, params) => request(method, params, { onSuccess, onError })
),
_request: request
};
+ clientCache.set(provider, { client, refCount: 1 });
+ return client;
};
export { createClient };

22
pnpm-lock.yaml generated
View File

@@ -5,6 +5,12 @@ settings:
excludeLinksFromLockfile: false
patchedDependencies:
'@polkadot-api/observable-client':
hash: 2dyuwkurmwrhcez6heyjslsxsu
path: patches/@polkadot-api__observable-client.patch
'@polkadot-api/substrate-client':
hash: lsuryiezeyeaoedwoxh2e4wda4
path: patches/@polkadot-api__substrate-client.patch
react18-json-view@0.2.8:
hash: tfqbi5gr5whnc64n7zptszjgcy
path: patches/react18-json-view@0.2.8.patch
@@ -24,7 +30,7 @@ importers:
version: 0.9.3
'@polkadot-api/substrate-client':
specifier: ^0.2.2
version: 0.2.2
version: 0.2.2(patch_hash=lsuryiezeyeaoedwoxh2e4wda4)
'@polkadot-api/utils':
specifier: ^0.1.2
version: 0.1.2
@@ -2965,12 +2971,12 @@ snapshots:
'@polkadot-api/json-rpc-provider': 0.0.4
'@polkadot-api/known-chains': 0.5.5
'@polkadot-api/metadata-compatibility': 0.1.11
'@polkadot-api/observable-client': 0.5.14(@polkadot-api/substrate-client@0.2.2)(rxjs@7.8.1)
'@polkadot-api/observable-client': 0.5.14(patch_hash=2dyuwkurmwrhcez6heyjslsxsu)(@polkadot-api/substrate-client@0.2.2(patch_hash=lsuryiezeyeaoedwoxh2e4wda4))(rxjs@7.8.1)
'@polkadot-api/polkadot-sdk-compat': 2.3.1
'@polkadot-api/sm-provider': 0.1.3(smoldot@2.0.30)
'@polkadot-api/smoldot': 0.3.3
'@polkadot-api/substrate-bindings': 0.9.3
'@polkadot-api/substrate-client': 0.2.2
'@polkadot-api/substrate-client': 0.2.2(patch_hash=lsuryiezeyeaoedwoxh2e4wda4)
'@polkadot-api/utils': 0.1.2
'@polkadot-api/wasm-executor': 0.1.1
'@polkadot-api/ws-provider': 0.3.2
@@ -3032,11 +3038,11 @@ snapshots:
'@polkadot-api/metadata-builders': 0.9.1
'@polkadot-api/substrate-bindings': 0.9.3
'@polkadot-api/observable-client@0.5.14(@polkadot-api/substrate-client@0.2.2)(rxjs@7.8.1)':
'@polkadot-api/observable-client@0.5.14(patch_hash=2dyuwkurmwrhcez6heyjslsxsu)(@polkadot-api/substrate-client@0.2.2(patch_hash=lsuryiezeyeaoedwoxh2e4wda4))(rxjs@7.8.1)':
dependencies:
'@polkadot-api/metadata-builders': 0.9.1
'@polkadot-api/substrate-bindings': 0.9.3
'@polkadot-api/substrate-client': 0.2.2
'@polkadot-api/substrate-client': 0.2.2(patch_hash=lsuryiezeyeaoedwoxh2e4wda4)
'@polkadot-api/utils': 0.1.2
rxjs: 7.8.1
@@ -3090,7 +3096,7 @@ snapshots:
'@scure/base': 1.1.9
scale-ts: 1.6.1
'@polkadot-api/substrate-client@0.2.2':
'@polkadot-api/substrate-client@0.2.2(patch_hash=lsuryiezeyeaoedwoxh2e4wda4)':
dependencies:
'@polkadot-api/json-rpc-provider': 0.0.4
'@polkadot-api/utils': 0.1.2
@@ -4319,7 +4325,7 @@ snapshots:
'@polkadot-api/logs-provider': 0.0.6
'@polkadot-api/metadata-builders': 0.9.1
'@polkadot-api/metadata-compatibility': 0.1.11
'@polkadot-api/observable-client': 0.5.14(@polkadot-api/substrate-client@0.2.2)(rxjs@7.8.1)
'@polkadot-api/observable-client': 0.5.14(patch_hash=2dyuwkurmwrhcez6heyjslsxsu)(@polkadot-api/substrate-client@0.2.2(patch_hash=lsuryiezeyeaoedwoxh2e4wda4))(rxjs@7.8.1)
'@polkadot-api/pjs-signer': 0.5.2
'@polkadot-api/polkadot-sdk-compat': 2.3.1
'@polkadot-api/polkadot-signer': 0.1.6
@@ -4327,7 +4333,7 @@ snapshots:
'@polkadot-api/sm-provider': 0.1.3(smoldot@2.0.30)
'@polkadot-api/smoldot': 0.3.3
'@polkadot-api/substrate-bindings': 0.9.3
'@polkadot-api/substrate-client': 0.2.2
'@polkadot-api/substrate-client': 0.2.2(patch_hash=lsuryiezeyeaoedwoxh2e4wda4)
'@polkadot-api/utils': 0.1.2
'@polkadot-api/ws-provider': 0.3.2
rxjs: 7.8.1

View File

@@ -27,7 +27,7 @@ export const CEnum: EditEnum = ({
case "primitive":
return ` (${innerType.value.value})`
case "compact":
return ` (${(innerType.value as any).size ?? "u128"})`
return ` (${innerType.value.size})`
case "AccountId20":
case "AccountId32":
return ` (${innerType.value.type})`

View File

@@ -369,7 +369,7 @@ export function getViewCodecComponent(
switch (entry.type) {
case "compact": {
ResultComponent = entry.isBig ? CBigNumber : CNumber
type = (entry as any).size ?? "u128"
type = entry.size
break
}
case "primitive": {
@@ -833,7 +833,7 @@ export function getCodecComponent(
switch (entry.type) {
case "compact": {
ResultComponent = entry.isBig ? CBigNumber : CNumber
type = (entry as any).size ?? "u128"
type = entry.size
break
}
case "primitive": {

View File

@@ -201,7 +201,7 @@ const StorageKeyInput: FC<{ idx: number; type: number; disabled: boolean }> = ({
case "primitive":
return lookupEntry.value
case "compact":
return (lookupEntry as any).size ?? "u128"
return lookupEntry.size
case "enum":
return "Enum"
case "array":