| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="Debouncer.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Threading; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Utilities.Threading; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Coalesces rapid repeated signals into a single callback after a quiet period. |
| | | 14 | | /// </summary> |
| | | 15 | | internal sealed class Debouncer : IDisposable |
| | | 16 | | { |
| | 3 | 17 | | private readonly Lock _lock = new(); |
| | | 18 | | private readonly Timer _timer; |
| | | 19 | | private readonly int _delayMilliseconds; |
| | | 20 | | private Action? _pending; |
| | | 21 | | private bool _disposed; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="Debouncer"/> class with the specified delay in milliseconds. The de |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="delayMilliseconds">The delay in milliseconds.</param> |
| | | 27 | | /// <exception cref="ArgumentOutOfRangeException">Thrown if the delay is negative.</exception> |
| | | 28 | | public Debouncer(int delayMilliseconds) |
| | | 29 | | { |
| | 3 | 30 | | ArgumentOutOfRangeException.ThrowIfNegative(delayMilliseconds); |
| | 3 | 31 | | _delayMilliseconds = delayMilliseconds; |
| | 3 | 32 | | _timer = new(static s => ((Debouncer)s!).Fire(), this, Timeout.Infinite, Timeout.Infinite); |
| | 3 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Schedules the specified action to be invoked after the delay. If called again before the delay elapses, the prev |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="action">The action to be invoked after the delay.</param> |
| | | 39 | | /// <exception cref="ObjectDisposedException">Thrown if the debouncer has been disposed.</exception> |
| | | 40 | | /// <exception cref="ArgumentNullException">Thrown if the action is null.</exception> |
| | | 41 | | public void Schedule(Action action) |
| | | 42 | | { |
| | 9 | 43 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 9 | 44 | | ArgumentNullException.ThrowIfNull(action); |
| | | 45 | | |
| | | 46 | | lock (_lock) |
| | | 47 | | { |
| | 9 | 48 | | _pending = action; |
| | 9 | 49 | | _ = _timer.Change(_delayMilliseconds, Timeout.Infinite); |
| | 9 | 50 | | } |
| | 9 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Invokes the pending action if any, and clears it. This method is called by the timer callback when the delay ela |
| | | 55 | | /// </summary> |
| | | 56 | | private void Fire() |
| | 3 | 57 | | { |
| | | 58 | | Action? action; |
| | | 59 | | |
| | | 60 | | lock (_lock) |
| | | 61 | | { |
| | 3 | 62 | | action = _pending; |
| | 3 | 63 | | _pending = null; |
| | 3 | 64 | | } |
| | | 65 | | |
| | 3 | 66 | | action?.Invoke(); |
| | 3 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Disposes the debouncer, preventing any pending or future scheduled actions from being invoked. |
| | | 71 | | /// </summary> |
| | | 72 | | public void Dispose() |
| | 3 | 73 | | { |
| | | 74 | | lock (_lock) |
| | | 75 | | { |
| | 3 | 76 | | if (_disposed) |
| | 0 | 77 | | return; |
| | | 78 | | |
| | 3 | 79 | | _disposed = true; |
| | 3 | 80 | | _pending = null; |
| | 3 | 81 | | _timer.Dispose(); |
| | 3 | 82 | | } |
| | 3 | 83 | | } |
| | | 84 | | } |
| | | 85 | | |