Fix caching for different params (#822)
This commit is contained in:
committed by
Jaco Greeff
parent
761cf6d52a
commit
f20727b46d
@@ -3,7 +3,9 @@
|
|||||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||||
|
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
import testingPairs from '@polkadot/keyring/testingPairs';
|
||||||
import { RpcInterface$Section } from '@polkadot/rpc-core/types';
|
import { RpcInterface$Section } from '@polkadot/rpc-core/types';
|
||||||
|
import { AccountId } from '@polkadot/types';
|
||||||
|
|
||||||
jest.mock('@polkadot/rpc-provider/ws', () => class {
|
jest.mock('@polkadot/rpc-provider/ws', () => class {
|
||||||
isConnected = () => true;
|
isConnected = () => true;
|
||||||
@@ -16,6 +18,7 @@ import RpcRx from '.';
|
|||||||
describe('createCachedObservable', () => {
|
describe('createCachedObservable', () => {
|
||||||
let api: RpcRx;
|
let api: RpcRx;
|
||||||
let creator: (...params: Array<any>) => Observable<any>;
|
let creator: (...params: Array<any>) => Observable<any>;
|
||||||
|
const keyring = testingPairs();
|
||||||
let section: RpcInterface$Section;
|
let section: RpcInterface$Section;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -51,6 +54,15 @@ describe('createCachedObservable', () => {
|
|||||||
).toBe(observable1);
|
).toBe(observable1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('creates a single observable (multiple calls, different arguments that should be cached together)', () => {
|
||||||
|
const observable1 = creator(keyring.alice.address());
|
||||||
|
const observable2 = creator(new AccountId(keyring.alice.address()));
|
||||||
|
|
||||||
|
expect(
|
||||||
|
observable2
|
||||||
|
).toBe(observable1);
|
||||||
|
});
|
||||||
|
|
||||||
it('creates multiple observables for different values', () => {
|
it('creates multiple observables for different values', () => {
|
||||||
const observable1 = creator(123);
|
const observable1 = creator(123);
|
||||||
const observable2 = creator(456);
|
const observable2 = creator(456);
|
||||||
|
|||||||
@@ -97,7 +97,13 @@ export default class RpcRx implements RpcRxInterface {
|
|||||||
if (isFunction(section[name].unsubscribe)) {
|
if (isFunction(section[name].unsubscribe)) {
|
||||||
const memoized: Memoized<RpcRxInterface$Method> = memoize(
|
const memoized: Memoized<RpcRxInterface$Method> = memoize(
|
||||||
(...params: Array<any>) => this.createReplay(name, params, section, memoized),
|
(...params: Array<any>) => this.createReplay(name, params, section, memoized),
|
||||||
{ length: false }
|
{
|
||||||
|
length: false,
|
||||||
|
// Primitive=true means: If two params `.toString()` methods give the
|
||||||
|
// same result, they'll be cached together.
|
||||||
|
// E.g.: `query.my.method('abc') === query.my.method(new AccountId('abc'));`
|
||||||
|
primitive: true
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return memoized as unknown as RpcRxInterface$Method;
|
return memoized as unknown as RpcRxInterface$Method;
|
||||||
@@ -126,17 +132,17 @@ export default class RpcRx implements RpcRxInterface {
|
|||||||
|
|
||||||
return this.createReplayUnsub(fn, subscribe, params, memoized);
|
return this.createReplayUnsub(fn, subscribe, params, memoized);
|
||||||
})
|
})
|
||||||
.pipe(
|
.pipe(
|
||||||
map((value) => {
|
map((value) => {
|
||||||
if (value instanceof Error) {
|
if (value instanceof Error) {
|
||||||
throw value;
|
throw value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}),
|
}),
|
||||||
publishReplay(1),
|
publishReplay(1),
|
||||||
refCount()
|
refCount()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private createReplayCallback (observer: Observer<any>) {
|
private createReplayCallback (observer: Observer<any>) {
|
||||||
|
|||||||
Reference in New Issue
Block a user