Replace memoizee with memoize from @polkadot/util (#2946)

This commit is contained in:
Jaco Greeff
2020-12-13 14:15:13 +01:00
committed by GitHub
parent 9b45901760
commit e2911a33c4
15 changed files with 112 additions and 251 deletions
+7 -10
View File
@@ -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;