Fix caching for different params (#822)

This commit is contained in:
Amaury Martiny
2019-04-03 12:46:33 +02:00
committed by Jaco Greeff
parent 761cf6d52a
commit f20727b46d
2 changed files with 29 additions and 11 deletions
+12
View File
@@ -3,7 +3,9 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { Observable } from 'rxjs';
import testingPairs from '@polkadot/keyring/testingPairs';
import { RpcInterface$Section } from '@polkadot/rpc-core/types';
import { AccountId } from '@polkadot/types';
jest.mock('@polkadot/rpc-provider/ws', () => class {
isConnected = () => true;
@@ -16,6 +18,7 @@ import RpcRx from '.';
describe('createCachedObservable', () => {
let api: RpcRx;
let creator: (...params: Array<any>) => Observable<any>;
const keyring = testingPairs();
let section: RpcInterface$Section;
beforeEach(() => {
@@ -51,6 +54,15 @@ describe('createCachedObservable', () => {
).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', () => {
const observable1 = creator(123);
const observable2 = creator(456);
+17 -11
View File
@@ -97,7 +97,13 @@ export default class RpcRx implements RpcRxInterface {
if (isFunction(section[name].unsubscribe)) {
const memoized: Memoized<RpcRxInterface$Method> = memoize(
(...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;
@@ -126,17 +132,17 @@ export default class RpcRx implements RpcRxInterface {
return this.createReplayUnsub(fn, subscribe, params, memoized);
})
.pipe(
map((value) => {
if (value instanceof Error) {
throw value;
}
.pipe(
map((value) => {
if (value instanceof Error) {
throw value;
}
return value;
}),
publishReplay(1),
refCount()
);
return value;
}),
publishReplay(1),
refCount()
);
}
private createReplayCallback (observer: Observer<any>) {