-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
EventKeyBase.cs
53 lines (44 loc) · 1.68 KB
/
EventKeyBase.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
48
49
50
51
52
53
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Threading.Tasks.Dataflow;
using Stride.Core.Diagnostics;
namespace Stride.Engine.Events
{
/// <summary>
/// Creates a new EventKey used to broadcast T type events.
/// </summary>
/// <typeparam name="T">The data type of the event you wish to send</typeparam>
public class EventKeyBase<T> : IDisposable
{
internal readonly Logger Logger;
internal readonly ulong EventId = EventKeyCounter.New();
internal readonly string EventName;
private readonly string broadcastDebug;
private readonly BroadcastBlock<T> broadcastBlock;
internal EventKeyBase(string category = "General", string eventName = "Event")
{
broadcastBlock = new BroadcastBlock<T>(null, new DataflowBlockOptions { TaskScheduler = EventTaskScheduler.Scheduler });
EventName = eventName;
Logger = GlobalLogger.GetLogger($"Event - {category}");
broadcastDebug = $"Broadcasting '{eventName}' ({EventId})";
}
~EventKeyBase()
{
Dispose();
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
internal IDisposable Connect(EventReceiverBase<T> target)
{
return broadcastBlock.LinkTo(target.BufferBlock);
}
protected void InternalBroadcast(T data)
{
Logger.Debug(broadcastDebug);
broadcastBlock.Post(data);
}
}
}