| | 1 | | using System; |
| | 2 | | using System.ComponentModel; |
| | 3 | | using System.Threading.Channels; |
| | 4 | |
|
| | 5 | | using Microsoft.Extensions.Logging; |
| | 6 | |
|
| | 7 | | namespace mkmrk.Channels; |
| | 8 | |
|
| | 9 | | /// <inheritdoc cref="IBroadcastChannel{TData,TResponse}" /> |
| | 10 | | public class BroadcastChannel<TData, TResponse> : IBroadcastChannel<TData, TResponse>, IBroadcastChannel<TData> where TR |
| | 11 | | private IBroadcastChannelWriter<TData, TResponse>? _writer; |
| | 12 | | private readonly ILoggerFactory? _loggerFactory; |
| | 13 | |
|
| | 14 | | /// <inheritdoc cref="BroadcastChannel{TData,TResponse}"/> |
| | 15 | | public BroadcastChannel( ILoggerFactory? loggerFactory = null ) => this._loggerFactory = loggerFactory; |
| | 16 | |
|
| | 17 | | /// <inheritdoc cref="BroadcastChannel{TData,TResponse}"/> |
| | 18 | | /// <remarks>For Dependency Injection and should not be used directly.</remarks> |
| | 19 | | [ EditorBrowsable( EditorBrowsableState.Never ) ] |
| | 20 | | public BroadcastChannel( IBroadcastChannelWriter<TData, TResponse> broadcastChannelWriter, ILoggerFactory? loggerFac |
| | 21 | | this._loggerFactory = loggerFactory; |
| | 22 | | this._writer = broadcastChannelWriter; |
| | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Get the single <see cref="BroadcastChannelWriter{TData,TResponse}"/> |
| | 27 | | /// </summary> |
| | 28 | | public IBroadcastChannelWriter<TData, TResponse> Writer |
| | 29 | | => _writer ??= new BroadcastChannelWriter<TData, TResponse>( _loggerFactory ); |
| | 30 | |
|
| | 31 | | IBroadcastChannelWriter<TData> IBroadcastChannel<TData>.Writer => this.Writer; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Create a new <see cref="BroadcastChannelReader{TData,TResponse}"/> and return it. |
| | 35 | | /// </summary> |
| | 36 | | public IBroadcastChannelReader<TData, TResponse> CreateReader( ) |
| | 37 | | => Writer.CreateReader(); |
| | 38 | | IBroadcastChannelReader<TData> IBroadcastChannel<TData>.CreateReader( ) => this.CreateReader(); |
| | 39 | |
|
| | 40 | | /// <inheritdoc /> |
| | 41 | | RemoveWriterByHashCode IBroadcastChannelAddReaderProvider<TData>.AddReader( ChannelWriter<TData> reader ) => this.Wr |
| | 42 | |
|
| | 43 | | /// <inheritdoc /> |
| | 44 | | public IBroadcastChannelReaderSource<TData, TResponse> CreateReaderSource( ) => new BroadcastChannelReaderSource<TDa |
| | 45 | | IBroadcastChannelReaderSource<TData> IBroadcastChannel<TData>.CreateReaderSource( ) => this.CreateReaderSource(); |
| | 46 | |
|
| | 47 | | private bool _isDisposed; |
| | 48 | |
|
| | 49 | | /// <inheritdoc /> |
| | 50 | | public void Dispose( ) { |
| | 51 | | Dispose( true ); |
| | 52 | | GC.SuppressFinalize( this ); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | // ReSharper disable once InconsistentNaming |
| | 56 | | /// <inheritdoc cref="IDisposable.Dispose"/> |
| | 57 | | protected virtual void Dispose( bool disposing ) { |
| | 58 | | if ( this._isDisposed ) { |
| | 59 | | return; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | if ( disposing ) { |
| | 63 | | this._writer?.Dispose(); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | this._isDisposed = true; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | /// <inheritdoc /> |
| | 70 | | public override string ToString( ) => $"{this.GetType().GenericTypeShortDescriptor( useShortGenericName: false )} [H |
| | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// <see cref="BroadcastChannel{TData}"/> with a default Response type of <see cref="IBroadcastChannelResponse"/> potent |
| | 75 | | /// </summary> |
| | 76 | | /// <typeparam name="TData">Type of the messages which <see cref="BroadcastChannelWriter{TData,TResponse}"/> writes and |
| | 77 | | /// <inheritdoc cref="BroadcastChannel{TData,TResponse}" path="/remarks" /> |
| | 78 | | public class BroadcastChannel<TData> : BroadcastChannel<TData, IBroadcastChannelResponse> { |
| | 79 | | /// <inheritdoc cref="M:mkmrk.Common.BroadcastChannel`2.#ctor(Microsoft.Extensions.Logging.ILoggerFactory)"/> |
| 2 | 80 | | public BroadcastChannel( ILoggerFactory? loggerFactory = null ) : base( loggerFactory ) { } |
| | 81 | |
|
| | 82 | | /// <inheritdoc cref="BroadcastChannel{TData}"/> |
| | 83 | | /// <remarks>For Dependency Injection and should not be used directly.</remarks> |
| | 84 | | [ EditorBrowsable( EditorBrowsableState.Never ) ] |
| | 85 | | public BroadcastChannel( IBroadcastChannelWriter<TData> broadcastChannelWriter, ILoggerFactory? loggerFactory = null |
| 0 | 86 | | base( broadcastChannelWriter as IBroadcastChannelWriter<TData, IBroadcastChannelResponse> |
| 0 | 87 | | ?? ThrowHelper.ThrowInvalidCastException<IBroadcastChannelWriter<TData>, IBroadcastChannelWriter<TData, IB |
| | 88 | | } |