Skip to content

Commit 82fe27b

Browse files
committed
add event generic
1 parent bbbcb07 commit 82fe27b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

TaleKit/Game/Event/IEventProcessor.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,19 @@ public void Process(IEvent e)
1616
}
1717

1818
protected abstract void Process(T e);
19+
}
20+
21+
public class SimpleProcessor<T> : EventProcessor<T> where T : IEvent
22+
{
23+
private readonly Action<T> processor;
24+
25+
public SimpleProcessor(Action<T> processor)
26+
{
27+
this.processor = processor;
28+
}
29+
30+
protected override void Process(T e)
31+
{
32+
processor(e);
33+
}
1934
}

TaleKit/Game/Session.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public void Close()
5858
this.network.Disconnect();
5959
}
6060

61-
public Session AddEventProcessor<T>() where T : IEventProcessor
61+
public Session AddProcessor<T>() where T : IEventProcessor
6262
{
6363
var processor = Activator.CreateInstance<T>();
64-
AddEventProcessor(processor);
64+
AddProcessor(processor);
6565
return this;
6666
}
6767

68-
public void AddEventProcessor<T>(T processor) where T : IEventProcessor
68+
public void AddProcessor<T>(T processor) where T : IEventProcessor
6969
{
7070
var eventProcessors = processors.GetValueOrDefault(processor.EventType);
7171
if (eventProcessors is null)
@@ -76,6 +76,11 @@ public void AddEventProcessor<T>(T processor) where T : IEventProcessor
7676
eventProcessors.Add(processor);
7777
}
7878

79+
public void AddProcessor<T>(Action<T> task) where T : IEvent
80+
{
81+
AddProcessor(new SimpleProcessor<T>(task));
82+
}
83+
7984
public void Emit<T>(T e) where T : IEvent
8085
{
8186
var eventProcessors = processors.GetValueOrDefault(e.GetType());

0 commit comments

Comments
 (0)