// Copyright (c) Microsoft. All rights reserved. namespace Harness.ConsoleReactiveFramework; /// /// Caches the result of a mapping function and only recomputes when the input changes. /// /// The type of the input value. /// The type of the mapped output value. public class ConsoleReactiveMemo { private TInput? _previousInput; private TOutput? _cachedOutput; private bool _hasValue; /// /// Returns the cached output if equals the previously stored input; /// otherwise invokes to compute and cache a new output. /// /// The current input value. /// A function that maps the input to an output value. /// The cached or newly computed output. public TOutput Map(TInput input, Func mapper) { ArgumentNullException.ThrowIfNull(mapper); if (!this._hasValue || !EqualityComparer.Default.Equals(input, this._previousInput)) { this._previousInput = input; this._cachedOutput = mapper(input); this._hasValue = true; } return this._cachedOutput!; } }