< Summary

Information
Class: MyNet.Utilities.Deferring.DeferredAction
Assembly: MyNet.Utilities
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Utilities/Deferring/DeferredAction.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 100
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_IsDeferred()100%11100%
Defer()100%11100%
ExecuteNow()100%11100%
Request()100%22100%
EndDefer()100%44100%
Dispose()100%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Utilities/Deferring/DeferredAction.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DeferredAction.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8
 9namespace 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>
 21public 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>
 20729    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    {
 4838        _deferCount++;
 4839        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>
 345    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    {
 19852        if (IsDeferred)
 53        {
 5454            _pending = true;
 5455            return;
 56        }
 57
 14458        action.Invoke();
 14459    }
 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    {
 4866        _deferCount--;
 67
 4868        if (_deferCount == 0 && _pending)
 69        {
 2770            _pending = false;
 2771            action.Invoke();
 72        }
 4573    }
 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        {
 5191            if (_disposed)
 392                return;
 93
 4894            _disposed = true;
 95
 4896            owner.EndDefer();
 4597        }
 98    }
 99}
 100