ProviderEvent
IProviderEvent is observable event. IProvider class may optionally implement IObservable<IProviderEvent>.
/// <summary>Provider base event</summary>
public interface IProviderEvent
{
/// <summary>Provider instance where event occured</summary>
IProvider Provider { get; set; }
}
/// <summary>Previously acquired content may have been modified</summary>
public interface IProviderContentModifyingEvent : IProviderEvent { }
/// <summary>Previously acquired content may have been modified</summary>
public interface IProviderContentModifiedEvent : IProviderEvent { }
.TrySubscribeChain(observer, out subscription) tries to subscribe provider chain all the way to the root. Typically there is only one provider in a chain that can be subscribed.
// Create data source
var source = new Dictionary<string, string> { { "Key", "Value" } };
// Create cache
var provider = Providers.Func(new TryCreate<string, string>(source.TryGetValue!)).Cached();
// Create observer
IObserver<IProviderEvent> observer = new DelegateObserver<IProviderEvent>(null, null, @event => Console.WriteLine(@event));
// Subscribe provider
provider.TrySubscribeChain(observer, out IDisposable subsription);
// Get value from source through provider (gets cached)
WriteLine(provider["Key"]); // "Value"
// Modify source
source["Key"] = "New Value";
// Read again from provider that has previously cached value
WriteLine(provider["Key"]); // "Value"
// Invalidate cache, observer is notified with IProviderContentModifiedEvent.
provider.InvalidateCache(); // Observer prints "ProviderContentModifiedEvent { Provider = Avalanche.Utilities.Provider.Internal.TryCreateProvider<System.String,System.String>.Cached() }"
// Read again from provider that is invalidated.
WriteLine(provider["Key"]); // "New Value"
// Stop subscription
subsription.Dispose();
Full Example
Full example
using System;
using System.Collections.Generic;
using Avalanche.Utilities;
using Avalanche.Utilities.Provider;
using static System.Console;
class provider_event
{
public static void Run()
{
{
// <01>
// Create data source
var source = new Dictionary<string, string> { { "Key", "Value" } };
// Create cache
var provider = Providers.Func(new TryCreate<string, string>(source.TryGetValue!)).Cached();
// Create observer
IObserver<IProviderEvent> observer = new DelegateObserver<IProviderEvent>(null, null, @event => Console.WriteLine(@event));
// Subscribe provider
provider.TrySubscribeChain(observer, out IDisposable subsription);
// Get value from source through provider (gets cached)
WriteLine(provider["Key"]); // "Value"
// Modify source
source["Key"] = "New Value";
// Read again from provider that has previously cached value
WriteLine(provider["Key"]); // "Value"
// Invalidate cache, observer is notified with IProviderContentModifiedEvent.
provider.InvalidateCache(); // Observer prints "ProviderContentModifiedEvent { Provider = Avalanche.Utilities.Provider.Internal.TryCreateProvider<System.String,System.String>.Cached() }"
// Read again from provider that is invalidated.
WriteLine(provider["Key"]); // "New Value"
// Stop subscription
subsription.Dispose();
// </01>
}
}
}