namespace Zcbot.WindowsNode; internal sealed class SoftwareProbeService { private readonly Func probe; private readonly object sync = new(); private AdapterRuntimeStatus? cachedRuntime; private DateTimeOffset runtimeCheckedAt; internal SoftwareProbeService(AdapterExecutionService execution) : this(execution.Probe) { } internal SoftwareProbeService(Func probe) { this.probe = probe; } internal AdapterRuntimeStatus Detect() { lock (sync) { if (cachedRuntime is null || DateTimeOffset.UtcNow - runtimeCheckedAt > TimeSpan.FromSeconds(30)) { cachedRuntime = probe(); runtimeCheckedAt = DateTimeOffset.UtcNow; } return cachedRuntime; } } internal void Invalidate() { lock (sync) { cachedRuntime = null; runtimeCheckedAt = default; } } }