< Summary

Information
Class: MyNet.Observable.Collections.SchedulerCollectionEventDispatcher
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/SchedulerCollectionEventDispatcher.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 53
Line coverage: 100%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
CheckAccess()83.33%66100%
Dispatch(...)100%11100%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SchedulerCollectionEventDispatcher.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Reactive.Concurrency;
 9using MyNet.Collections;
 10
 11namespace MyNet.Observable.Collections;
 12
 13/// <summary>
 14/// Adapter that bridges an Rx scheduler to the collection event dispatcher abstraction.
 15/// </summary>
 16/// <remarks>
 17/// Initializes a new instance of the <see cref="SchedulerCollectionEventDispatcher"/> class.
 18/// </remarks>
 19/// <param name="scheduler">The scheduler used to dispatch actions.</param>
 20public sealed class SchedulerCollectionEventDispatcher(IScheduler scheduler) : ICollectionEventDispatcher
 21{
 922    private readonly IScheduler _scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler));
 923    private volatile int _schedulerThreadId = -1;
 24
 25    /// <summary>
 26    /// Checks if the current thread can invoke collection notifications without marshaling.
 27    /// </summary>
 28    /// <returns><c>true</c> when already on the scheduler's execution context; otherwise <c>false</c>.</returns>
 29    public bool CheckAccess()
 30    {
 1231        if (ReferenceEquals(_scheduler, ImmediateScheduler.Instance) ||
 1232            ReferenceEquals(_scheduler, CurrentThreadScheduler.Instance))
 33        {
 634            return true;
 35        }
 36
 637        var threadId = _schedulerThreadId;
 638        return threadId >= 0 && threadId == Environment.CurrentManagedThreadId;
 39    }
 40
 41    /// <inheritdoc />
 42    public void Dispatch(Action action)
 43    {
 344        ArgumentNullException.ThrowIfNull(action);
 45
 346        _scheduler.Schedule(() =>
 347        {
 348            _schedulerThreadId = Environment.CurrentManagedThreadId;
 349            action();
 350        });
 351    }
 52}
 53