| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DeferredOperations.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Utilities.Deferring; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides a simple mechanism to defer execution of actions until deferral scopes are ended. Use <see cref="Defer"/> t |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class DeferredOperations |
| | | 16 | | { |
| | 30 | 17 | | private readonly List<Action> _operations = []; |
| | | 18 | | private int _deferCount; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets a value indicating whether execution is currently deferred. Returns true if there are active deferral scope |
| | | 22 | | /// </summary> |
| | 360 | 23 | | public bool IsDeferred => _deferCount > 0; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Creates a new deferral scope. While the scope is active, execution is deferred. Dispose the returned <see cref=" |
| | | 27 | | /// </summary> |
| | | 28 | | /// <returns>An <see cref="IDisposable"/> representing the deferral scope.</returns> |
| | | 29 | | public IDisposable Defer() |
| | | 30 | | { |
| | 39 | 31 | | _deferCount++; |
| | 39 | 32 | | return new Scope(this); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Executes the provided action immediately if deferral is not active; otherwise, defers execution until deferral s |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="action">The action to execute or defer.</param> |
| | | 39 | | public void Execute(Action action) |
| | | 40 | | { |
| | 351 | 41 | | if (IsDeferred) |
| | | 42 | | { |
| | 345 | 43 | | _operations.Add(action); |
| | 345 | 44 | | return; |
| | | 45 | | } |
| | | 46 | | |
| | 6 | 47 | | action(); |
| | 6 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Ends a deferral scope and executes all deferred actions if no other scopes remain active. |
| | | 52 | | /// </summary> |
| | | 53 | | private void EndDefer() |
| | | 54 | | { |
| | 39 | 55 | | _deferCount--; |
| | | 56 | | |
| | 39 | 57 | | if (_deferCount == 0) |
| | | 58 | | { |
| | 30 | 59 | | var operations = _operations.ToArray(); |
| | 30 | 60 | | _operations.Clear(); |
| | | 61 | | |
| | 741 | 62 | | foreach (var operation in operations) |
| | 342 | 63 | | operation(); |
| | | 64 | | } |
| | 36 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Internal scope type representing a deferral. Disposing this instance ends the scope. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <remarks> |
| | | 71 | | /// Initializes a new instance of the <see cref="Scope"/> class and registers it with the provided <see cref="Deferr |
| | | 72 | | /// </remarks> |
| | | 73 | | /// <param name="owner">The deferrer that created this scope.</param> |
| | | 74 | | private sealed class Scope(DeferredOperations owner) : IDisposable |
| | | 75 | | { |
| | | 76 | | private bool _disposed; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Ends the deferral scope and triggers deferred execution if no other scopes remain. |
| | | 80 | | /// </summary> |
| | | 81 | | public void Dispose() |
| | | 82 | | { |
| | 42 | 83 | | if (_disposed) |
| | 3 | 84 | | return; |
| | | 85 | | |
| | 39 | 86 | | _disposed = true; |
| | | 87 | | |
| | 39 | 88 | | owner.EndDefer(); |
| | 36 | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |