< Summary

Information
Class: MyNet.Utilities.Deferring.DeferredOperations
Assembly: MyNet.Utilities
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Utilities/Deferring/DeferredOperations.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 92
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
.ctor()100%11100%
get_IsDeferred()100%11100%
Defer()100%11100%
Execute(...)100%22100%
EndDefer()100%44100%
Dispose()100%22100%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DeferredOperations.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9
 10namespace 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>
 15public sealed class DeferredOperations
 16{
 3017    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>
 36023    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    {
 3931        _deferCount++;
 3932        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    {
 35141        if (IsDeferred)
 42        {
 34543            _operations.Add(action);
 34544            return;
 45        }
 46
 647        action();
 648    }
 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    {
 3955        _deferCount--;
 56
 3957        if (_deferCount == 0)
 58        {
 3059            var operations = _operations.ToArray();
 3060            _operations.Clear();
 61
 74162            foreach (var operation in operations)
 34263                operation();
 64        }
 3665    }
 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        {
 4283            if (_disposed)
 384                return;
 85
 3986            _disposed = true;
 87
 3988            owner.EndDefer();
 3689        }
 90    }
 91}
 92