89 lines
2.6 KiB
C#
89 lines
2.6 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
namespace SharpChat.Events {
|
|||
|
public class ChatEventDispatcher {
|
|||
|
private readonly object HandlersAccess = new();
|
|||
|
private readonly List<IChatEventHandler> Handlers = new();
|
|||
|
|
|||
|
public void Subscribe(IChatEventHandler handler) {
|
|||
|
lock(HandlersAccess)
|
|||
|
if(!Handlers.Contains(handler))
|
|||
|
Handlers.Add(handler);
|
|||
|
}
|
|||
|
|
|||
|
public void Unsubscribe(IChatEventHandler handler) {
|
|||
|
lock(HandlersAccess)
|
|||
|
Handlers.Remove(handler);
|
|||
|
}
|
|||
|
|
|||
|
public ChatEventInfo Dispatch(ChatEventInfo info) {
|
|||
|
lock(HandlersAccess)
|
|||
|
foreach(IChatEventHandler handler in Handlers)
|
|||
|
handler.HandleEvent(info);
|
|||
|
|
|||
|
return info;
|
|||
|
}
|
|||
|
|
|||
|
public ChatEventInfo Dispatch(
|
|||
|
string eventType,
|
|||
|
DateTimeOffset eventCreated,
|
|||
|
ChannelInfo channelInfo,
|
|||
|
UserInfo userInfo,
|
|||
|
ChatEventData? eventData = null
|
|||
|
) {
|
|||
|
return Dispatch(new ChatEventInfo(
|
|||
|
SharpId.Next(),
|
|||
|
eventType,
|
|||
|
eventCreated,
|
|||
|
channelInfo.Name,
|
|||
|
userInfo.UserId,
|
|||
|
userInfo.UserName,
|
|||
|
userInfo.Colour,
|
|||
|
userInfo.Rank,
|
|||
|
userInfo.NickName,
|
|||
|
userInfo.Permissions,
|
|||
|
eventData
|
|||
|
));
|
|||
|
}
|
|||
|
|
|||
|
public ChatEventInfo Dispatch(
|
|||
|
string eventType,
|
|||
|
string channelName,
|
|||
|
UserInfo userInfo,
|
|||
|
ChatEventData? eventData = null
|
|||
|
) {
|
|||
|
return Dispatch(new ChatEventInfo(
|
|||
|
SharpId.Next(),
|
|||
|
eventType,
|
|||
|
DateTimeOffset.UtcNow,
|
|||
|
channelName,
|
|||
|
userInfo.UserId,
|
|||
|
userInfo.UserName,
|
|||
|
userInfo.Colour,
|
|||
|
userInfo.Rank,
|
|||
|
userInfo.NickName,
|
|||
|
userInfo.Permissions,
|
|||
|
eventData
|
|||
|
));
|
|||
|
}
|
|||
|
|
|||
|
public ChatEventInfo Dispatch(
|
|||
|
string eventType,
|
|||
|
ChannelInfo channelInfo,
|
|||
|
UserInfo userInfo,
|
|||
|
ChatEventData? eventData = null
|
|||
|
) {
|
|||
|
return Dispatch(eventType, channelInfo.Name, userInfo, eventData);
|
|||
|
}
|
|||
|
|
|||
|
public ChatEventInfo Dispatch(
|
|||
|
string eventType,
|
|||
|
UserInfo userInfo,
|
|||
|
ChatEventData? eventData = null
|
|||
|
) {
|
|||
|
return Dispatch(eventType, string.Empty, userInfo, eventData);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|