Align single-shot memoization (#2546)
* Align single-shot memoization * Small tweaks * Test for delayed creates * With actual sub * Expand delay test * Bump delay
This commit is contained in:
@@ -91,10 +91,25 @@ describe('Cached Observables', (): void => {
|
|||||||
expect(observable2).not.toBe(observable1);
|
expect(observable2).not.toBe(observable1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates multiple observables for one-shots', (): void => {
|
it('creates single observables for subsequent one-shots', (): void => {
|
||||||
const observable1 = rpc.chain.getBlockHash(123);
|
const observable1 = rpc.chain.getBlockHash(123);
|
||||||
const observable2 = rpc.chain.getBlockHash(123);
|
const observable2 = rpc.chain.getBlockHash(123);
|
||||||
|
|
||||||
expect(observable2).not.toBe(observable1);
|
expect(observable2).toBe(observable1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('creates multiple observables for subsequent one-shots delayed', (done): void => {
|
||||||
|
const observable1 = rpc.chain.getBlockHash(123);
|
||||||
|
|
||||||
|
const sub = observable1.subscribe((): void => {
|
||||||
|
sub.unsubscribe();
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(rpc.chain.getBlockHash(123)).toBe(observable1);
|
||||||
|
|
||||||
|
setTimeout((): void => {
|
||||||
|
expect(rpc.chain.getBlockHash(123)).not.toBe(observable1);
|
||||||
|
done();
|
||||||
|
}, 1000);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ import { AnyJson, Codec, DefinitionRpc, DefinitionRpcExt, DefinitionRpcSub, Regi
|
|||||||
import { RpcInterface, RpcInterfaceMethod } from './types';
|
import { RpcInterface, RpcInterfaceMethod } from './types';
|
||||||
|
|
||||||
import memoizee from 'memoizee';
|
import memoizee from 'memoizee';
|
||||||
import { from, Observable, Observer, of, throwError } from 'rxjs';
|
import { Observable, Observer } from 'rxjs';
|
||||||
import { catchError, publishReplay, refCount, switchMap } from 'rxjs/operators';
|
import { publishReplay } from 'rxjs/operators';
|
||||||
import jsonrpc from '@polkadot/types/interfaces/jsonrpc';
|
import jsonrpc from '@polkadot/types/interfaces/jsonrpc';
|
||||||
import { Option, StorageKey, Vec, createClass, createTypeUnsafe } from '@polkadot/types';
|
import { Option, StorageKey, Vec, createClass, createTypeUnsafe } from '@polkadot/types';
|
||||||
import { assert, hexToU8a, isFunction, isNull, isUndefined, logger, u8aToU8a } from '@polkadot/util';
|
import { assert, hexToU8a, isFunction, isNull, isUndefined, logger, u8aToU8a } from '@polkadot/util';
|
||||||
|
|
||||||
import { drr } from './rxjs';
|
import { drr, refCountDelay } from './rxjs';
|
||||||
|
|
||||||
interface StorageChangeSetJSON {
|
interface StorageChangeSetJSON {
|
||||||
block: string;
|
block: string;
|
||||||
@@ -202,6 +202,7 @@ export default class Rpc implements RpcInterface {
|
|||||||
private _createMethodSend (section: string, method: string, def: DefinitionRpc): RpcInterfaceMethod {
|
private _createMethodSend (section: string, method: string, def: DefinitionRpc): RpcInterfaceMethod {
|
||||||
const rpcName = `${section}_${method}`;
|
const rpcName = `${section}_${method}`;
|
||||||
const hashIndex = def.params.findIndex(({ isHistoric }) => isHistoric);
|
const hashIndex = def.params.findIndex(({ isHistoric }) => isHistoric);
|
||||||
|
let memoized: null | RpcInterfaceMethod & memoizee.Memoized<RpcInterfaceMethod> = null;
|
||||||
|
|
||||||
// execute the RPC call, doing a registry swap for historic as applicable
|
// execute the RPC call, doing a registry swap for historic as applicable
|
||||||
const callWithRegistry = async (isRaw: boolean, values: any[]): Promise<Codec | Codec[]> => {
|
const callWithRegistry = async (isRaw: boolean, values: any[]): Promise<Codec | Codec[]> => {
|
||||||
@@ -220,28 +221,35 @@ export default class Rpc implements RpcInterface {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const creator = (isRaw: boolean) => (...values: any[]): Observable<any> => {
|
const creator = (isRaw: boolean) => (...values: any[]): Observable<any> => {
|
||||||
// Here, logically, it should be `of(this.formatInputs(method, values))`.
|
return new Observable((observer: Observer<any>): VoidCallback => {
|
||||||
// However, formatInputs can throw, and when it does, the above way
|
callWithRegistry(isRaw, values)
|
||||||
// doesn't throw in the "Observable loop" (which is internally wrapped in
|
.then((value): void => {
|
||||||
// a try/catch block). So we:
|
observer.next(value);
|
||||||
// - first do `of(1)` - won't throw
|
observer.complete();
|
||||||
// - then do `map(()=>this.formatInputs)` - might throw, but inside Observable.
|
})
|
||||||
return of(1).pipe(
|
.catch((error): void => {
|
||||||
switchMap(() => from(callWithRegistry(isRaw, values))),
|
logErrorMessage(method, def, error);
|
||||||
catchError((error): any => {
|
|
||||||
logErrorMessage(method, def, error);
|
|
||||||
|
|
||||||
return throwError(error);
|
observer.error(error);
|
||||||
}),
|
observer.complete();
|
||||||
|
});
|
||||||
|
|
||||||
|
return (): void => {
|
||||||
|
// delete old results from cache
|
||||||
|
memoized?.delete(...values);
|
||||||
|
};
|
||||||
|
}).pipe(
|
||||||
publishReplay(1), // create a Replay(1)
|
publishReplay(1), // create a Replay(1)
|
||||||
refCount() // Unsubscribe WS when there are no more subscribers
|
refCountDelay(250) // Unsubscribe after delay
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// We voluntarily don't cache the "one-shot" RPC calls. For example,
|
memoized = memoizee(this._createMethodWithRaw(creator), {
|
||||||
// `getStorage('123')` returns the current value, but this value can change
|
length: false,
|
||||||
// over time, so we wouldn't want to cache the Observable.
|
normalizer: JSON.stringify
|
||||||
return this._createMethodWithRaw(creator);
|
});
|
||||||
|
|
||||||
|
return memoized;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a subscriptor, it subscribes once and resolves with the id as subscribe
|
// create a subscriptor, it subscribes once and resolves with the id as subscribe
|
||||||
@@ -305,16 +313,8 @@ export default class Rpc implements RpcInterface {
|
|||||||
|
|
||||||
// Teardown logic
|
// Teardown logic
|
||||||
return (): void => {
|
return (): void => {
|
||||||
// Delete from cache
|
// Delete from cache, so old results don't hang around
|
||||||
// Reason:
|
memoized?.delete(...values);
|
||||||
// ```
|
|
||||||
// const s = api.query.system.account(addr1).subscribe(); // let's say it's 6
|
|
||||||
// s.unsubscribe();
|
|
||||||
// // wait a bit, for the nonce to increase to 7
|
|
||||||
// api.query.system.account(addr1).subscribe(); // will output 6 instead of 7 if we don't clear cache
|
|
||||||
// // that's because all our observables are replay(1)
|
|
||||||
// ```
|
|
||||||
memoized && memoized.delete(...values);
|
|
||||||
|
|
||||||
// Unsubscribe from provider
|
// Unsubscribe from provider
|
||||||
subscriptionPromise
|
subscriptionPromise
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { refCountDelay } from './refCountDelay';
|
|||||||
export type DrrResult = <T> (source$: Observable<T>) => Observable<T>;
|
export type DrrResult = <T> (source$: Observable<T>) => Observable<T>;
|
||||||
|
|
||||||
interface Options {
|
interface Options {
|
||||||
|
delay?: number;
|
||||||
skipChange?: boolean;
|
skipChange?: boolean;
|
||||||
skipTimeout?: boolean;
|
skipTimeout?: boolean;
|
||||||
}
|
}
|
||||||
@@ -35,7 +36,7 @@ const NOOP = (): void => undefined;
|
|||||||
* @ignore
|
* @ignore
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export const drr = ({ skipChange = false, skipTimeout = false }: Options = {}): DrrResult => <T> (source$: Observable<T>): Observable<T> =>
|
export const drr = ({ delay, skipChange = false, skipTimeout = false }: Options = {}): DrrResult => <T> (source$: Observable<T>): Observable<T> =>
|
||||||
source$.pipe(
|
source$.pipe(
|
||||||
catchError(ERR),
|
catchError(ERR),
|
||||||
skipChange
|
skipChange
|
||||||
@@ -44,5 +45,5 @@ export const drr = ({ skipChange = false, skipTimeout = false }: Options = {}):
|
|||||||
publishReplay(1),
|
publishReplay(1),
|
||||||
skipTimeout
|
skipTimeout
|
||||||
? refCount()
|
? refCount()
|
||||||
: refCountDelay()
|
: refCountDelay(delay)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -4,45 +4,40 @@
|
|||||||
|
|
||||||
import { asapScheduler, ConnectableObservable, MonoTypeOperatorFunction, Observable, Subscription, TeardownLogic } from 'rxjs';
|
import { asapScheduler, ConnectableObservable, MonoTypeOperatorFunction, Observable, Subscription, TeardownLogic } from 'rxjs';
|
||||||
|
|
||||||
const DELAY = 1750;
|
|
||||||
|
|
||||||
/** @internal */
|
/** @internal */
|
||||||
function refCountDelayInner <T> (source: Observable<T>): Observable<T> {
|
export function refCountDelay <T> (delay = 1750): MonoTypeOperatorFunction<T> {
|
||||||
// state: 0 = disconnected, 1 = disconnecting, 2 = connecting, 3 = connected
|
return (source: Observable<T>): Observable<T> => {
|
||||||
let [state, refCount, connection, scheduler] = [0, 0, Subscription.EMPTY, Subscription.EMPTY];
|
// state: 0 = disconnected, 1 = disconnecting, 2 = connecting, 3 = connected
|
||||||
|
let [state, refCount, connection, scheduler] = [0, 0, Subscription.EMPTY, Subscription.EMPTY];
|
||||||
|
|
||||||
return new Observable((ob): TeardownLogic => {
|
return new Observable((ob): TeardownLogic => {
|
||||||
source.subscribe(ob);
|
source.subscribe(ob);
|
||||||
|
|
||||||
if (refCount++ === 0) {
|
if (refCount++ === 0) {
|
||||||
if (state === 1) {
|
if (state === 1) {
|
||||||
scheduler.unsubscribe();
|
|
||||||
} else {
|
|
||||||
connection = (source as ConnectableObservable<T>).connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
state = 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (): void => {
|
|
||||||
if (--refCount === 0) {
|
|
||||||
if (state === 2) {
|
|
||||||
state = 0;
|
|
||||||
scheduler.unsubscribe();
|
scheduler.unsubscribe();
|
||||||
} else {
|
} else {
|
||||||
// state === 3
|
connection = (source as ConnectableObservable<T>).connect();
|
||||||
state = 1;
|
|
||||||
scheduler = asapScheduler.schedule((): void => {
|
|
||||||
state = 0;
|
|
||||||
connection.unsubscribe();
|
|
||||||
}, DELAY);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @internal */
|
state = 3;
|
||||||
export function refCountDelay <T> (): MonoTypeOperatorFunction<T> {
|
}
|
||||||
return refCountDelayInner;
|
|
||||||
|
return (): void => {
|
||||||
|
if (--refCount === 0) {
|
||||||
|
if (state === 2) {
|
||||||
|
state = 0;
|
||||||
|
scheduler.unsubscribe();
|
||||||
|
} else {
|
||||||
|
// state === 3
|
||||||
|
state = 1;
|
||||||
|
scheduler = asapScheduler.schedule((): void => {
|
||||||
|
state = 0;
|
||||||
|
connection.unsubscribe();
|
||||||
|
}, delay);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user