Replace memoizee with memoize from @polkadot/util (#2946)
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@polkadot/api": "2.10.2-16",
|
||||
"@polkadot/types": "2.10.2-16",
|
||||
"@polkadot/util": "^4.2.2-19",
|
||||
"@polkadot/util": "^4.2.2-21",
|
||||
"@polkadot/x-rxjs": "2.10.2-16",
|
||||
"bn.js": "^4.11.9"
|
||||
}
|
||||
|
||||
@@ -16,14 +16,13 @@
|
||||
"@polkadot/api": "2.10.2-16",
|
||||
"@polkadot/rpc-core": "2.10.2-16",
|
||||
"@polkadot/types": "2.10.2-16",
|
||||
"@polkadot/util": "^4.2.2-19",
|
||||
"@polkadot/util-crypto": "^4.2.2-19",
|
||||
"@polkadot/util": "^4.2.2-21",
|
||||
"@polkadot/util-crypto": "^4.2.2-21",
|
||||
"@polkadot/x-rxjs": "2.10.2-16",
|
||||
"bn.js": "^4.11.9",
|
||||
"memoizee": "^0.4.14"
|
||||
"bn.js": "^4.11.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/keyring": "^4.2.2-19",
|
||||
"@polkadot/keyring": "^4.2.2-21",
|
||||
"@polkadot/rpc-provider": "2.10.2-16"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Memoized } from '@polkadot/util/types';
|
||||
import type { Observer, TeardownLogic } from '@polkadot/x-rxjs';
|
||||
|
||||
import createMemo from 'memoizee';
|
||||
|
||||
import { drr, normalizer } from '@polkadot/rpc-core/util';
|
||||
import { drr } from '@polkadot/rpc-core/util';
|
||||
import { memoize } from '@polkadot/util';
|
||||
import { Observable } from '@polkadot/x-rxjs';
|
||||
|
||||
type ObsFn <T> = (...params: any[]) => Observable<T>;
|
||||
@@ -14,21 +14,18 @@ type ObsFn <T> = (...params: any[]) => Observable<T>;
|
||||
// 1. creates a memo of the inner fn -> Observable, removing when unsubscribed
|
||||
// 2. wraps the observable in a drr() (which includes an unsub delay)
|
||||
/** @internal */
|
||||
export function memo <T> (instanceId: string, inner: ObsFn<T>): ObsFn<T> {
|
||||
const cached = createMemo(
|
||||
export function memo <T> (instanceId: string, inner: ObsFn<T>): Memoized<ObsFn<T>> {
|
||||
const cached = memoize(
|
||||
(...params: any[]): Observable<T> =>
|
||||
new Observable((observer: Observer<T>): TeardownLogic => {
|
||||
const subscription = inner(...params).subscribe(observer);
|
||||
|
||||
return (): void => {
|
||||
cached.delete(...params);
|
||||
cached.unmemoize(...params);
|
||||
subscription.unsubscribe();
|
||||
};
|
||||
}).pipe(drr()),
|
||||
{
|
||||
// Normalize via JSON.stringify, allow e.g. AccountId -> ss58
|
||||
normalizer: normalizer(instanceId)
|
||||
}
|
||||
{ getInstanceId: () => instanceId }
|
||||
);
|
||||
|
||||
return cached;
|
||||
|
||||
@@ -14,14 +14,14 @@
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@polkadot/api-derive": "2.10.2-16",
|
||||
"@polkadot/keyring": "^4.2.2-19",
|
||||
"@polkadot/keyring": "^4.2.2-21",
|
||||
"@polkadot/metadata": "2.10.2-16",
|
||||
"@polkadot/rpc-core": "2.10.2-16",
|
||||
"@polkadot/rpc-provider": "2.10.2-16",
|
||||
"@polkadot/types": "2.10.2-16",
|
||||
"@polkadot/types-known": "2.10.2-16",
|
||||
"@polkadot/util": "^4.2.2-19",
|
||||
"@polkadot/util-crypto": "^4.2.2-19",
|
||||
"@polkadot/util": "^4.2.2-21",
|
||||
"@polkadot/util-crypto": "^4.2.2-21",
|
||||
"@polkadot/x-rxjs": "2.10.2-16",
|
||||
"bn.js": "^4.11.9",
|
||||
"eventemitter3": "^4.0.7"
|
||||
|
||||
@@ -15,11 +15,11 @@
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@polkadot/types": "2.10.2-16",
|
||||
"@polkadot/types-known": "2.10.2-16",
|
||||
"@polkadot/util": "^4.2.2-19",
|
||||
"@polkadot/util-crypto": "^4.2.2-19",
|
||||
"@polkadot/util": "^4.2.2-21",
|
||||
"@polkadot/util-crypto": "^4.2.2-21",
|
||||
"bn.js": "^4.11.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/keyring": "^4.2.2-19"
|
||||
"@polkadot/keyring": "^4.2.2-21"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,10 @@
|
||||
"@polkadot/metadata": "2.10.2-16",
|
||||
"@polkadot/rpc-provider": "2.10.2-16",
|
||||
"@polkadot/types": "2.10.2-16",
|
||||
"@polkadot/util": "^4.2.2-19",
|
||||
"@polkadot/x-rxjs": "2.10.2-16",
|
||||
"memoizee": "^0.4.14"
|
||||
"@polkadot/util": "^4.2.2-21",
|
||||
"@polkadot/x-rxjs": "2.10.2-16"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/keyring": "^4.2.2-19"
|
||||
"@polkadot/keyring": "^4.2.2-21"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,19 +5,18 @@ import type { ProviderInterface, ProviderInterfaceCallback } from '@polkadot/rpc
|
||||
import type { StorageKey, Vec } from '@polkadot/types';
|
||||
import type { Hash } from '@polkadot/types/interfaces';
|
||||
import type { AnyJson, Codec, DefinitionRpc, DefinitionRpcExt, DefinitionRpcSub, Registry } from '@polkadot/types/types';
|
||||
import type { Memoized } from '@polkadot/util/types';
|
||||
import type { Observer } from '@polkadot/x-rxjs';
|
||||
import type { RpcInterface, RpcInterfaceMethod } from './types';
|
||||
|
||||
import memoizee from 'memoizee';
|
||||
|
||||
import { Option } from '@polkadot/types';
|
||||
import { createClass, createTypeUnsafe } from '@polkadot/types/create';
|
||||
import jsonrpc from '@polkadot/types/interfaces/jsonrpc';
|
||||
import { assert, hexToU8a, isFunction, isNull, isUndefined, logger, u8aToU8a } from '@polkadot/util';
|
||||
import { assert, hexToU8a, isFunction, isNull, isUndefined, logger, memoize, u8aToU8a } from '@polkadot/util';
|
||||
import { Observable } from '@polkadot/x-rxjs';
|
||||
import { publishReplay, refCount } from '@polkadot/x-rxjs/operators';
|
||||
|
||||
import { drr, normalizer, refCountDelay } from './util';
|
||||
import { drr, refCountDelay } from './util';
|
||||
|
||||
interface StorageChangeSetJSON {
|
||||
block: string;
|
||||
@@ -210,14 +209,9 @@ export class RpcCore implements RpcInterface {
|
||||
}, {} as RpcInterface[Section]);
|
||||
}
|
||||
|
||||
private _memomize (creator: (outputAs: OutputType) => (...values: any[]) => Observable<any>): RpcInterfaceMethod & memoizee.Memoized<RpcInterfaceMethod> {
|
||||
const memoized = memoizee(creator('scale') as RpcInterfaceMethod, {
|
||||
// Dynamic length for argument
|
||||
length: false,
|
||||
// Normalize args so that different args that should be cached
|
||||
// together are cached together.
|
||||
// E.g.: `query.my.method('abc') === query.my.method(createType('AccountId', 'abc'));`
|
||||
normalizer: normalizer(this.#instanceId)
|
||||
private _memomize (creator: (outputAs: OutputType) => (...values: any[]) => Observable<any>): Memoized<RpcInterfaceMethod> {
|
||||
const memoized = memoize(creator('scale') as RpcInterfaceMethod, {
|
||||
getInstanceId: () => this.#instanceId
|
||||
});
|
||||
|
||||
memoized.json = creator('json');
|
||||
@@ -230,7 +224,7 @@ export class RpcCore implements RpcInterface {
|
||||
const rpcName = def.endpoint || `${section}_${method}`;
|
||||
const hashIndex = def.params.findIndex(({ isHistoric }) => isHistoric);
|
||||
const cacheIndex = def.params.findIndex(({ isCached }) => isCached);
|
||||
let memoized: null | RpcInterfaceMethod & memoizee.Memoized<RpcInterfaceMethod> = null;
|
||||
let memoized: null | Memoized<RpcInterfaceMethod> = null;
|
||||
|
||||
// execute the RPC call, doing a registry swap for historic as applicable
|
||||
const callWithRegistry = async (outputAs: OutputType, values: any[]): Promise<Codec | Codec[]> => {
|
||||
@@ -266,7 +260,7 @@ export class RpcCore implements RpcInterface {
|
||||
|
||||
return (): void => {
|
||||
// delete old results from cache
|
||||
memoized?.delete(...values);
|
||||
memoized?.unmemoize(...values);
|
||||
};
|
||||
}).pipe(
|
||||
publishReplay(1), // create a Replay(1)
|
||||
@@ -299,7 +293,7 @@ export class RpcCore implements RpcInterface {
|
||||
const subName = `${section}_${subMethod}`;
|
||||
const unsubName = `${section}_${unsubMethod}`;
|
||||
const subType = `${section}_${updateType}`;
|
||||
let memoized: null | RpcInterfaceMethod & memoizee.Memoized<RpcInterfaceMethod> = null;
|
||||
let memoized: null | Memoized<RpcInterfaceMethod> = null;
|
||||
|
||||
const creator = (outputAs: OutputType) => (...values: unknown[]): Observable<any> => {
|
||||
return new Observable((observer: Observer<any>): VoidCallback => {
|
||||
@@ -343,7 +337,7 @@ export class RpcCore implements RpcInterface {
|
||||
// Teardown logic
|
||||
return (): void => {
|
||||
// Delete from cache, so old results don't hang around
|
||||
memoized?.delete(...values);
|
||||
memoized?.unmemoize(...values);
|
||||
|
||||
// Unsubscribe from provider
|
||||
subscriptionPromise
|
||||
|
||||
@@ -2,5 +2,4 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export * from './drr';
|
||||
export * from './normalizer';
|
||||
export * from './refCountDelay';
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/rpc-core authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { normalizer } from '.';
|
||||
|
||||
describe('normalizer', (): void => {
|
||||
it('works when BigInt is in the data (object)', (): void => {
|
||||
expect(
|
||||
normalizer('instance')({ a: 'bar', b: 1234567890987654321n, c: 123 })
|
||||
).toEqual('instance{"a":"bar","b":"1234567890987654321","c":123}');
|
||||
});
|
||||
|
||||
it('works when BigInt is in the data (array)', (): void => {
|
||||
expect(
|
||||
normalizer('instance')(['bar', 1234567890987654321n, 123])
|
||||
).toEqual('instance["bar","1234567890987654321",123]');
|
||||
});
|
||||
|
||||
it('works when BigInt is in the data (single)', (): void => {
|
||||
expect(
|
||||
normalizer('instance')(1234567890987654321n)
|
||||
).toEqual('instance"1234567890987654321"');
|
||||
});
|
||||
|
||||
it('works when BigInt is in the data (nested)', (): void => {
|
||||
expect(
|
||||
normalizer('instance')({ a: 'bar', b: { foo: 1234567890987654321n }, c: 123 })
|
||||
).toEqual('instance{"a":"bar","b":{"foo":"1234567890987654321"},"c":123}');
|
||||
});
|
||||
});
|
||||
@@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2020 @polkadot/rpc-core authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { isBigInt } from '@polkadot/util';
|
||||
|
||||
export function normalizer (instanceId: string): (value: unknown) => string {
|
||||
return (value: unknown) =>
|
||||
instanceId + JSON.stringify(value, (_, value: unknown) =>
|
||||
isBigInt(value)
|
||||
? value.toString()
|
||||
: value
|
||||
);
|
||||
}
|
||||
@@ -14,15 +14,15 @@
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@polkadot/types": "2.10.2-16",
|
||||
"@polkadot/util": "^4.2.2-19",
|
||||
"@polkadot/util-crypto": "^4.2.2-19",
|
||||
"@polkadot/x-fetch": "^4.2.2-19",
|
||||
"@polkadot/x-ws": "^4.2.2-19",
|
||||
"@polkadot/util": "^4.2.2-21",
|
||||
"@polkadot/util-crypto": "^4.2.2-21",
|
||||
"@polkadot/x-fetch": "^4.2.2-21",
|
||||
"@polkadot/x-ws": "^4.2.2-21",
|
||||
"bn.js": "^4.11.9",
|
||||
"eventemitter3": "^4.0.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/keyring": "^4.2.2-19",
|
||||
"@polkadot/keyring": "^4.2.2-21",
|
||||
"@polkadot/metadata": "2.10.2-16",
|
||||
"mock-socket": "^9.0.3",
|
||||
"nock": "^13.0.5"
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
"@polkadot/metadata": "2.10.2-16",
|
||||
"@polkadot/rpc-provider": "2.10.2-16",
|
||||
"@polkadot/types": "2.10.2-16",
|
||||
"@polkadot/util": "^4.2.2-19",
|
||||
"@polkadot/util": "^4.2.2-21",
|
||||
"handlebars": "^4.7.6",
|
||||
"websocket": "^1.0.33",
|
||||
"yargs": "^16.2.0"
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@polkadot/types": "2.10.2-16",
|
||||
"@polkadot/util": "^4.2.2-19",
|
||||
"@polkadot/util": "^4.2.2-21",
|
||||
"bn.js": "^4.11.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -14,15 +14,13 @@
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@polkadot/metadata": "2.10.2-16",
|
||||
"@polkadot/util": "^4.2.2-19",
|
||||
"@polkadot/util-crypto": "^4.2.2-19",
|
||||
"@polkadot/util": "^4.2.2-21",
|
||||
"@polkadot/util-crypto": "^4.2.2-21",
|
||||
"@polkadot/x-rxjs": "2.10.2-16",
|
||||
"@types/bn.js": "^4.11.6",
|
||||
"bn.js": "^4.11.9",
|
||||
"memoizee": "^0.4.14"
|
||||
"bn.js": "^4.11.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/keyring": "^4.2.2-19",
|
||||
"@types/memoizee": "^0.4.5"
|
||||
"@polkadot/keyring": "^4.2.2-21"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user