< Summary

Information
Class: MyNet.Collections.SynchronizationContextCollectionEventDispatcher
Assembly: MyNet.Collections
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Collections/SynchronizationContextCollectionEventDispatcher.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 10
Uncovered lines: 0
Coverable lines: 10
Total lines: 49
Line coverage: 100%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CaptureCurrentOrImmediate()50%22100%
CheckAccess()100%22100%
Dispatch(...)100%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Collections/SynchronizationContextCollectionEventDispatcher.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SynchronizationContextCollectionEventDispatcher.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Threading;
 9
 10namespace MyNet.Collections;
 11
 12/// <summary>
 13/// Dispatches events through a synchronization context, typically the UI context.
 14/// </summary>
 15/// <remarks>
 16/// Initializes a new instance of the <see cref="SynchronizationContextCollectionEventDispatcher"/> class.
 17/// </remarks>
 18/// <param name="context">The synchronization context to use.</param>
 19public sealed class SynchronizationContextCollectionEventDispatcher(SynchronizationContext? context) : ICollectionEventD
 20{
 21    /// <summary>
 22    /// Captures the current synchronization context or falls back to immediate dispatch.
 23    /// </summary>
 24    public static ICollectionEventDispatcher CaptureCurrentOrImmediate()
 325        => SynchronizationContext.Current is null
 326            ? ImmediateCollectionEventDispatcher.Default
 327            : new SynchronizationContextCollectionEventDispatcher(SynchronizationContext.Current);
 28
 29    /// <summary>
 30    /// Checks if the current thread has access to the configured synchronization context, allowing for optimized dispat
 31    /// </summary>
 32    /// <returns>True if the current thread has access to the synchronization context; otherwise, false.</returns>
 633    public bool CheckAccess() => context is null || SynchronizationContext.Current == context;
 34
 35    /// <inheritdoc />
 36    public void Dispatch(Action action)
 37    {
 638        ArgumentNullException.ThrowIfNull(action);
 39
 640        if (context is null)
 41        {
 342            action();
 343            return;
 44        }
 45
 346        context.Post(_ => action(), null);
 347    }
 48}
 49