From 533f9ce2498555d93cb3a15b5a68d5b1d7a4e70c Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 31 Aug 2020 15:20:06 +0200 Subject: [PATCH] Explicit delay for per-hash single queries (#2548) * Explicit delay for per-hash single queries * Expand delay * Update packages/rpc-core/src/index.ts --- packages/rpc-core/src/cached.spec.ts | 2 +- packages/rpc-core/src/index.ts | 9 +++++++-- packages/types/src/interfaces/chain/definitions.ts | 1 + packages/types/src/interfaces/state/definitions.ts | 2 ++ packages/types/src/types/definitions.ts | 1 + 5 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/rpc-core/src/cached.spec.ts b/packages/rpc-core/src/cached.spec.ts index a85fdfbcea..ce8cea3eb0 100644 --- a/packages/rpc-core/src/cached.spec.ts +++ b/packages/rpc-core/src/cached.spec.ts @@ -110,6 +110,6 @@ describe('Cached Observables', (): void => { setTimeout((): void => { expect(rpc.chain.getBlockHash(123)).not.toBe(observable1); done(); - }, 1000); + }, 3500); }); }); diff --git a/packages/rpc-core/src/index.ts b/packages/rpc-core/src/index.ts index c49c781d9b..de3b9c8854 100644 --- a/packages/rpc-core/src/index.ts +++ b/packages/rpc-core/src/index.ts @@ -9,7 +9,7 @@ import { RpcInterface, RpcInterfaceMethod } from './types'; import memoizee from 'memoizee'; import { Observable, Observer } from 'rxjs'; -import { publishReplay } from 'rxjs/operators'; +import { publishReplay, refCount } from 'rxjs/operators'; import jsonrpc from '@polkadot/types/interfaces/jsonrpc'; import { Option, StorageKey, Vec, createClass, createTypeUnsafe } from '@polkadot/types'; import { assert, hexToU8a, isFunction, isNull, isUndefined, logger, u8aToU8a } from '@polkadot/util'; @@ -202,6 +202,7 @@ export default class Rpc implements RpcInterface { private _createMethodSend (section: string, method: string, def: DefinitionRpc): RpcInterfaceMethod { const rpcName = `${section}_${method}`; const hashIndex = def.params.findIndex(({ isHistoric }) => isHistoric); + const cacheIndex = def.params.findIndex(({ isCached }) => isCached); let memoized: null | RpcInterfaceMethod & memoizee.Memoized = null; // execute the RPC call, doing a registry swap for historic as applicable @@ -221,6 +222,8 @@ export default class Rpc implements RpcInterface { }; const creator = (isRaw: boolean) => (...values: any[]): Observable => { + const isDelayed = (hashIndex !== -1 && !!values[hashIndex]) || (cacheIndex !== -1 && !!values[cacheIndex]); + return new Observable((observer: Observer): VoidCallback => { callWithRegistry(isRaw, values) .then((value): void => { @@ -240,7 +243,9 @@ export default class Rpc implements RpcInterface { }; }).pipe( publishReplay(1), // create a Replay(1) - refCountDelay(250) // Unsubscribe after delay + isDelayed + ? refCountDelay() // Unsubscribe after delay + : refCount() ); }; diff --git a/packages/types/src/interfaces/chain/definitions.ts b/packages/types/src/interfaces/chain/definitions.ts index f327c3b721..ae2fafe67f 100644 --- a/packages/types/src/interfaces/chain/definitions.ts +++ b/packages/types/src/interfaces/chain/definitions.ts @@ -16,6 +16,7 @@ export default { { name: 'hash', type: 'BlockHash', + isCached: true, isOptional: true } ], diff --git a/packages/types/src/interfaces/state/definitions.ts b/packages/types/src/interfaces/state/definitions.ts index 766363b6b6..24cf0bca01 100644 --- a/packages/types/src/interfaces/state/definitions.ts +++ b/packages/types/src/interfaces/state/definitions.ts @@ -257,6 +257,7 @@ export default { { name: 'at', type: 'BlockHash', + isCached: true, isOptional: true } ], @@ -269,6 +270,7 @@ export default { { name: 'at', type: 'BlockHash', + isCached: true, isOptional: true } ], diff --git a/packages/types/src/types/definitions.ts b/packages/types/src/types/definitions.ts index 9cb828ce83..a45ca253dc 100644 --- a/packages/types/src/types/definitions.ts +++ b/packages/types/src/types/definitions.ts @@ -13,6 +13,7 @@ export type DefinitionTypeStruct = Record | { _alias export type DefinitionType = string | DefinitionTypeEnum | DefinitionTypeSet | DefinitionTypeStruct; export interface DefinitionRpcParam { + isCached?: boolean; isHistoric?: boolean; isOptional?: boolean; name: string;