"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.cachedValue = void 0;
const simpleObservingDependent_1 = require("./simpleObservingDependent");
/**
 * A basic {@link ObservingDependent} implementation.
 *
 * Does not support caching results which are "undefined": Will always be recomputed.
 *
 * Currently does not implement Dependee so other values can't depend on it,
 * nor support the Cleanable protocol or any eviction system other than invalidation.
 */
class CachedValue extends simpleObservingDependent_1.SimpleObservingDependent {
    constructor(compute, computationName) {
        super(() => {
            this.cache = undefined;
        }, computationName);
        this.compute = compute;
    }
    get() {
        var _a;
        (_a = this.cache) !== null && _a !== void 0 ? _a : (this.cache = this.compute(this));
        return this.cache;
    }
}
/**
 * Create a ICachedValue that is invalidated when `observer` (as provided to `compute`) gets invalidated.
 */
function cachedValue(compute, computationName = "CachedValue") {
    return new CachedValue(compute, computationName);
}
exports.cachedValue = cachedValue;
//# sourceMappingURL=cachedValue.js.map