< Summary

Information
Class: MyNet.UI.Notifications.NotificationService
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Notifications/NotificationService.cs
Tag: 323_28699572109
Line coverage
78%
Covered lines: 15
Uncovered lines: 4
Coverable lines: 19
Total lines: 68
Line coverage: 78.9%
Branch coverage
70%
Covered branches: 7
Total branches: 10
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%44100%
get_Notifications()100%11100%
Publish(...)50%5462.5%
Dispose()50%2283.33%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Notifications/NotificationService.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="NotificationService.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Linq;
 10using System.Reactive.Linq;
 11using System.Reactive.Subjects;
 12using MyNet.UI.Notifications.Models;
 13using MyNet.UI.Notifications.Processors;
 14
 15namespace MyNet.UI.Notifications;
 16
 17/// <summary>
 18/// Implements a notification service that allows publishing notifications and processing them through a chain of proces
 19/// </summary>
 20public sealed class NotificationService : INotificationService, IDisposable
 21{
 3022    private readonly Subject<INotification> _subject = new();
 23    private readonly ISubject<INotification> _synchronizedSubject;
 24    private readonly IReadOnlyList<INotificationProcessor> _processors;
 25    private bool _isDisposed;
 26
 27    /// <summary>
 28    /// Initializes a new instance of the <see cref="NotificationService"/> class.
 29    /// </summary>
 30    /// <param name="processors">An optional collection of notification processors to apply before publication.</param>
 31    public NotificationService(IEnumerable<INotificationProcessor>? processors = null)
 32    {
 3033        _synchronizedSubject = Subject.Synchronize(_subject);
 3034        _processors = processors?.ToList() ?? [];
 3035    }
 36
 37    /// <inheritdoc/>
 2738    public IObservable<INotification> Notifications => _subject.AsObservable();
 39
 40    /// <inheritdoc/>
 41    public void Publish(INotification notification)
 42    {
 3343        ObjectDisposedException.ThrowIf(_isDisposed, this);
 44
 3045        var current = notification;
 46
 6047        foreach (var processor in _processors)
 48        {
 049            current = processor.Process(current);
 050            if (current is null)
 051                return;
 52        }
 53
 3054        _synchronizedSubject.OnNext(current);
 3055    }
 56
 57    /// <inheritdoc/>
 58    public void Dispose()
 59    {
 3060        if (_isDisposed)
 061            return;
 62
 3063        _isDisposed = true;
 3064        _subject.OnCompleted();
 3065        _subject.Dispose();
 3066    }
 67}
 68