public static Supplier <T> MemoizeWithExpiration <T>([CanBeNull] this Supplier <T> supplier, [NotNull] IClock clock, TimeSpan expiration) { if (supplier == null) { return(null); } clock = clock.As(TimeUnit.Tick); var expirationTicks = expiration.Ticks; var expireAt = clock.Time; var value = default(T); return(() => { var now = clock.Time; if (now < expireAt) { return value; } value = supplier(); expireAt = now + expirationTicks; return value; }); }
/// <summary> /// Create a stage program. /// </summary> /// <param name="clock">Clock for timing</param> /// <param name="stageProvider">Stage provider that providing stages</param> /// <param name="preferPreloaded">Prefer pre-loaded stages if possible</param> public StageProgram([NotNull] IClock clock, [NotNull] IStageProvider stageProvider, bool preferPreloaded = true) { if (stageProvider == null) { throw new ArgumentNullException(nameof(stageProvider)); } OriginalClock = clock ?? throw new ArgumentNullException(nameof(clock)); FreezableClock = new FreezableClock(clock.As(TimeUnit.Millisecond)); FreezableClock.Frozen += (sender, e) => _pausedEvent.Set(); FreezableClock.Unfrozen += (sender, e) => _pausedEvent.Reset(); _stageProvider = preferPreloaded && stageProvider.TryPreload(out var preloaded) ? preloaded : stageProvider; _cyclicExecutor = new AsyncCyclicExecutor("StageProgram Worker", DoWork, true, ThreadPriority.Highest, null, StoppingAction.Abort); _cyclicExecutor.Starting += (sender, e) => { CurrentStage = null; _pausedEvent.Set(); StartTime = Time; _nextUpdateTime = 0; }; _cyclicExecutor.Started += (sender, e) => Started?.Invoke(this, EventArgs.Empty); _cyclicExecutor.Stopped += (sender, e) => Stopped?.Invoke(this, EventArgs.Empty); _stageSkipped = new AtomicBool(false); _pausedEvent = new ManualResetEvent(false); }
public MinimumInterval([NotNull] IClock clock, TimeSpan timeSpan) : this(clock.As(TimeUnit.Tick), timeSpan.Ticks) { }