< Summary

Information
Class: MyNet.Utilities.Deferring.DeferredOperations
Assembly: MyNet.Utilities
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Utilities/Deferring/DeferredOperations.cs
Tag: 342_31701152804
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 90
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/5aaaab311aa14541478b9e402357d98b657a23e4/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
 4857        if (_deferCount != 0) return;
 3058        var operations = _operations.ToArray();
 3059        _operations.Clear();
 60
 74161        foreach (var operation in operations)
 34262            operation();
 2763    }
 64
 65    /// <summary>
 66    /// Internal scope type representing a deferral. Disposing this instance ends the scope.
 67    /// </summary>
 68    /// <remarks>
 69    /// Initializes a new instance of the <see cref="Scope"/> class and registers it with the provided <see cref="Deferr
 70    /// </remarks>
 71    /// <param name="owner">The deferrer that created this scope.</param>
 72    private sealed class Scope(DeferredOperations owner) : IDisposable
 73    {
 74        private bool _disposed;
 75
 76        /// <summary>
 77        /// Ends the deferral scope and triggers deferred execution if no other scopes remain.
 78        /// </summary>
 79        public void Dispose()
 80        {
 4281            if (_disposed)
 382                return;
 83
 3984            _disposed = true;
 85
 3986            owner.EndDefer();
 3687        }
 88    }
 89}
 90