"use strict";
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.v
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const memoizee_1 = __importDefault(require("memoizee"));
const rxjs_1 = require("rxjs");
const rxjs_2 = require("../../rpc-core/rxjs");
// Normalize via JSON.stringify, allow e.g. AccountId -> ss58
// eslint-disable-next-line @typescript-eslint/unbound-method
const normalizer = JSON.stringify;
// Wraps a derive, doing 2 things to optimize calls -
//   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 */
function memo(inner) {
    const cached = memoizee_1.default((...params) => rxjs_1.Observable.create((observer) => {
        const sub = inner(...params).subscribe(observer);
        return () => {
            cached.delete(...params);
            sub.unsubscribe();
        };
    }).pipe(rxjs_2.drr()), { normalizer });
    return cached;
}
exports.memo = memo;
