-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGadgetRuntimeStateChannelsFeature.cs
47 lines (40 loc) · 1.57 KB
/
GadgetRuntimeStateChannelsFeature.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
namespace Gadgetry.Channels;
/// <summary>
/// A feature used to interact with the "channels" associated with a <see cref="GadgetRuntimeState"/>.
/// </summary>
public class GadgetRuntimeStateChannelsFeature : IGadgetRuntimeStateFeature
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)] internal readonly Mutex mutex = new();
private readonly List<IGadgetRuntimeChannel> runtimeChannels = new();
/// <summary>
/// Gets or creates a <see cref="GadgetRuntimeChannel{TModel}"/> identified by a <see cref="GadgetChannel{TModel}"/>.
/// </summary>
/// <typeparam name="TModel">The type of model contained within the channel.</typeparam>
/// <param name="channel">The channel used to identify the newly created channel.</param>
/// <returns>A <see cref="GadgetRuntimeChannel{TModel}"/> associated with the <see cref="GadgetChannel{TModel}"/>.</returns>
public GadgetRuntimeChannel<TModel> GetOrCreateChannel<TModel>(GadgetChannel<TModel> channel)
{
mutex.WaitOne();
foreach (var runtimeChannel in runtimeChannels)
{
if (runtimeChannel.Template == channel)
{
mutex.ReleaseMutex();
return (GadgetRuntimeChannel<TModel>)runtimeChannel;
}
}
var newRuntimeChannel = new GadgetRuntimeChannel<TModel>(this, channel);
runtimeChannels.Add(newRuntimeChannel);
mutex.ReleaseMutex();
return newRuntimeChannel;
}
/// <summary>
/// Creates a new instance of the <see cref="GadgetRuntimeStateChannelsFeature"/> class.
/// </summary>
public GadgetRuntimeStateChannelsFeature()
{
}
}