| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DeferredAction.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Utilities.Deferring; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides a simple mechanism to defer execution of an action until deferral scopes are ended. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// Use <see cref="Defer"/> to create a scope that postpones execution. When all scopes are disposed, the bound action i |
| | | 16 | | /// </remarks> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// Initializes a new instance of the <see cref="DeferredAction"/> class and binds the provided action. |
| | | 19 | | /// </remarks> |
| | | 20 | | /// <param name="action">The action to execute when deferral ends.</param> |
| | | 21 | | public class DeferredAction(Action action) : IDeferrable |
| | | 22 | | { |
| | | 23 | | private int _deferCount; |
| | | 24 | | private bool _pending; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets a value indicating whether execution is currently deferred. Returns true if there are active deferral scope |
| | | 28 | | /// </summary> |
| | 207 | 29 | | public bool IsDeferred => _deferCount > 0; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Creates a new deferral scope. While the scope is active, execution is deferred. |
| | | 33 | | /// Dispose the returned <see cref="IDisposable"/> to end the scope. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <returns>An <see cref="IDisposable"/> representing the deferral scope.</returns> |
| | | 36 | | public IDisposable Defer() |
| | | 37 | | { |
| | 48 | 38 | | _deferCount++; |
| | 48 | 39 | | return new Scope(this); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Executes the bound action immediately, regardless of the current deferral state. This allows bypassing deferral |
| | | 44 | | /// </summary> |
| | 3 | 45 | | public void ExecuteNow() => action(); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Requests execution of the bound action. If deferral is active, marks the action as pending and defers execution |
| | | 49 | | /// </summary> |
| | | 50 | | public void Request() |
| | | 51 | | { |
| | 198 | 52 | | if (IsDeferred) |
| | | 53 | | { |
| | 54 | 54 | | _pending = true; |
| | 54 | 55 | | return; |
| | | 56 | | } |
| | | 57 | | |
| | 144 | 58 | | action.Invoke(); |
| | 144 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Ends a deferral scope and executes the bound action if no other scopes remain active. |
| | | 63 | | /// </summary> |
| | | 64 | | private void EndDefer() |
| | | 65 | | { |
| | 48 | 66 | | _deferCount--; |
| | | 67 | | |
| | 48 | 68 | | if (_deferCount == 0 && _pending) |
| | | 69 | | { |
| | 27 | 70 | | _pending = false; |
| | 27 | 71 | | action.Invoke(); |
| | | 72 | | } |
| | 45 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Internal scope type representing a deferral. Disposing this instance ends the scope. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <remarks> |
| | | 79 | | /// Initializes a new instance of the <see cref="Scope"/> class and registers it with the provided <see cref="Deferr |
| | | 80 | | /// </remarks> |
| | | 81 | | /// <param name="owner">The deferrer that created this scope.</param> |
| | | 82 | | private sealed class Scope(DeferredAction owner) : IDisposable |
| | | 83 | | { |
| | | 84 | | private bool _disposed; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Ends the deferral scope and triggers deferred execution if no other scopes remain. |
| | | 88 | | /// </summary> |
| | | 89 | | public void Dispose() |
| | | 90 | | { |
| | 51 | 91 | | if (_disposed) |
| | 3 | 92 | | return; |
| | | 93 | | |
| | 48 | 94 | | _disposed = true; |
| | | 95 | | |
| | 48 | 96 | | owner.EndDefer(); |
| | 45 | 97 | | } |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |