< Summary

Information
Class: MyNet.Utilities.Deferring.DeferredAction
Assembly: MyNet.Utilities
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Utilities/Deferring/DeferredAction.cs
Tag: 342_31701152804
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 98
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/5aaaab311aa14541478b9e402357d98b657a23e4/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
 6968        if (_deferCount != 0 || !_pending) return;
 2769        _pending = false;
 2770        action.Invoke();
 2471    }
 72
 73    /// <summary>
 74    /// Internal scope type representing a deferral. Disposing this instance ends the scope.
 75    /// </summary>
 76    /// <remarks>
 77    /// Initializes a new instance of the <see cref="Scope"/> class and registers it with the provided <see cref="Deferr
 78    /// </remarks>
 79    /// <param name="owner">The deferrer that created this scope.</param>
 80    private sealed class Scope(DeferredAction owner) : IDisposable
 81    {
 82        private bool _disposed;
 83
 84        /// <summary>
 85        /// Ends the deferral scope and triggers deferred execution if no other scopes remain.
 86        /// </summary>
 87        public void Dispose()
 88        {
 5189            if (_disposed)
 390                return;
 91
 4892            _disposed = true;
 93
 4894            owner.EndDefer();
 4595        }
 96    }
 97}
 98