diff --git a/Implab.Playground/Implab.Playground.csproj b/Implab.Playground/Implab.Playground.csproj --- a/Implab.Playground/Implab.Playground.csproj +++ b/Implab.Playground/Implab.Playground.csproj @@ -19,7 +19,9 @@ - + + + diff --git a/Implab.Playground/Program.cs b/Implab.Playground/Program.cs --- a/Implab.Playground/Program.cs +++ b/Implab.Playground/Program.cs @@ -12,6 +12,7 @@ using Unity.Injection; using Unity.Registration; namespace Implab.Playground { + using System.Reactive.Linq; using static Trace; class Foo { @@ -27,14 +28,10 @@ namespace Implab.Playground { Trace.Log("First!"); Log("+1!"); - using(TraceRegistry.Global.Subscribe(x => { - var ch = x as TraceSourceChannel; - if (ch == null) - return; - + using(TraceRegistry.Global.OfType().Subscribe(ch => { Console.WriteLine($"{ch.Id}: {ch.Source.Name}"); - }, true)) { + })) { Trace.Log("Hi!"); Log("Respect!"); } diff --git a/Implab.ServiceHost/Implab.ServiceHost.csproj b/Implab.ServiceHost/Implab.ServiceHost.csproj --- a/Implab.ServiceHost/Implab.ServiceHost.csproj +++ b/Implab.ServiceHost/Implab.ServiceHost.csproj @@ -17,7 +17,7 @@ - + diff --git a/Implab.Test/DiagnosticsTest.cs b/Implab.Test/DiagnosticsTest.cs --- a/Implab.Test/DiagnosticsTest.cs +++ b/Implab.Test/DiagnosticsTest.cs @@ -23,14 +23,7 @@ namespace Implab.Test { TraceRegistry.Global.Subscribe(x => { visited++; found = x as TraceSourceChannel; - }, false); - - Assert.Equal(0, visited); - - TraceRegistry.Global.Subscribe(x => { - visited++; - found = x as TraceSourceChannel; - }, true); + }); Assert.Equal(1,visited); Assert.Equal(channel, found); diff --git a/Implab.Test/Implab.Test.csproj b/Implab.Test/Implab.Test.csproj --- a/Implab.Test/Implab.Test.csproj +++ b/Implab.Test/Implab.Test.csproj @@ -14,6 +14,7 @@ + diff --git a/Implab/Diagnostics/TraceRegistry.cs b/Implab/Diagnostics/TraceRegistry.cs --- a/Implab/Diagnostics/TraceRegistry.cs +++ b/Implab/Diagnostics/TraceRegistry.cs @@ -4,11 +4,10 @@ using System.Diagnostics; using Implab.Parallels; namespace Implab.Diagnostics { - public class TraceRegistry { + public class TraceRegistry: IObservable { class Subscription : IDisposable { readonly WeakReference m_registry; - readonly Action m_unsubscribe; public Subscription(TraceRegistry registry) { m_registry = new WeakReference(registry); @@ -21,28 +20,32 @@ namespace Implab.Diagnostics { } } + /// + /// The global collection of available diagnostic channels + /// + /// public static TraceRegistry Global { get; } = new TraceRegistry(); readonly object m_lock = new object(); - readonly Dictionary> m_subscriptions = new Dictionary>(); + readonly Dictionary> m_subscriptions = new Dictionary>(); readonly SimpleAsyncQueue m_channels = new SimpleAsyncQueue(); - internal void Register(TraceChannel channel) { + public void Register(TraceChannel channel) { // notifications can run in parallel - Action[] handlers = null; + IObserver[] handlers = null; lock(m_lock) { m_channels.Enqueue(channel); if (m_subscriptions.Count > 0) { - handlers = new Action[m_subscriptions.Count]; + handlers = new IObserver[m_subscriptions.Count]; m_subscriptions.Values.CopyTo(handlers, 0); } } if (handlers != null) foreach(var h in handlers) - h(channel); + h.OnNext(channel); } /// @@ -51,7 +54,7 @@ namespace Implab.Diagnostics { /// /// /// - public IDisposable Subscribe(Action handler, bool existing) { + public IDisposable Subscribe(IObserver handler) { Safe.ArgumentNotNull(handler, nameof(handler)); var cookie = new Subscription(this); @@ -66,9 +69,9 @@ namespace Implab.Diagnostics { } // announce previously declared channels if required - if (existing) { + if (snap != null) { foreach(var c in snap) - handler(c); + handler.OnNext(c); } // return the subscription diff --git a/Implab/Implab.csproj b/Implab/Implab.csproj --- a/Implab/Implab.csproj +++ b/Implab/Implab.csproj @@ -8,7 +8,7 @@ and SharedLock, Trace helpers on top of System.Diagnostics, ObjectPool etc. 2012-2018 Sergey Smirnov - 3.0.12 + 3.0.14 https://hg.implab.org/pub/ImplabNet/file/tip/Implab/license.txt https://implab.org https://hg.implab.org/pub/ImplabNet/