/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { SimpleObservingDependent } from "./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 {
    constructor(compute, computationName) {
        super(() => {
            this.cache = undefined;
        }, computationName);
        this.compute = compute;
    }
    get() {
        this.cache ?? (this.cache = this.compute(this));
        return this.cache;
    }
}
/**
 * Create a ICachedValue that is invalidated when `observer` (as provided to `compute`) gets invalidated.
 */
export function cachedValue(compute, computationName = "CachedValue") {
    return new CachedValue(compute, computationName);
}
//# sourceMappingURL=cachedValue.js.map