Explicit delay for per-hash single queries (#2548)

* Explicit delay for per-hash single queries

* Expand delay

* Update packages/rpc-core/src/index.ts
This commit is contained in:
Jaco Greeff
2020-08-31 15:20:06 +02:00
committed by GitHub
parent 797ee9c24a
commit 533f9ce249
5 changed files with 12 additions and 3 deletions
+1 -1
View File
@@ -110,6 +110,6 @@ describe('Cached Observables', (): void => {
setTimeout((): void => {
expect(rpc.chain.getBlockHash(123)).not.toBe(observable1);
done();
}, 1000);
}, 3500);
});
});
+7 -2
View File
@@ -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<RpcInterfaceMethod> = 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<any> => {
const isDelayed = (hashIndex !== -1 && !!values[hashIndex]) || (cacheIndex !== -1 && !!values[cacheIndex]);
return new Observable((observer: Observer<any>): 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()
);
};
@@ -16,6 +16,7 @@ export default {
{
name: 'hash',
type: 'BlockHash',
isCached: true,
isOptional: true
}
],
@@ -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
}
],
+1
View File
@@ -13,6 +13,7 @@ export type DefinitionTypeStruct = Record<string, DefinitionTypeType> | { _alias
export type DefinitionType = string | DefinitionTypeEnum | DefinitionTypeSet | DefinitionTypeStruct;
export interface DefinitionRpcParam {
isCached?: boolean;
isHistoric?: boolean;
isOptional?: boolean;
name: string;