tgstation-server 5.12.7
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ChatTrackingContext.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading;
5using System.Threading.Tasks;
6
7using Microsoft.Extensions.Logging;
8
10
12{
15 {
17 public bool Active
18 {
19 get => active;
20 set
21 {
22 if (active == value)
23 return;
24 if (value)
25 logger.LogTrace("Activated");
26 else
27 logger.LogTrace("Deactivated");
28
29 active = value;
30 }
31 }
32
34 public IReadOnlyCollection<ChannelRepresentation> Channels { get; private set; }
35
37 public IEnumerable<CustomCommand> CustomCommands
38 {
39 get => customCommands;
40 set
41 {
42 customCommands = (value ?? throw new InvalidOperationException("value cannot be null!"))
43 .Select(customCommand =>
44 {
45 customCommand.SetHandler(customCommandHandler);
46 return customCommand;
47 })
48 .ToList();
49 logger.LogTrace("Custom commands set.");
50 }
51 }
52
57
61 readonly ILogger<ChatTrackingContext> logger;
62
66 readonly object synchronizationLock;
67
71 IReadOnlyCollection<CustomCommand> customCommands;
72
77
81 Action onDispose;
82
86 bool active;
87
97 IEnumerable<ChannelRepresentation> initialChannels,
98 ILogger<ChatTrackingContext> logger,
99 Action onDispose)
100 {
101 this.customCommandHandler = customCommandHandler ?? throw new ArgumentNullException(nameof(customCommandHandler));
102 Channels = initialChannels?.ToList() ?? throw new ArgumentNullException(nameof(initialChannels));
103 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
104 this.onDispose = onDispose ?? throw new ArgumentNullException(nameof(onDispose));
105
106 synchronizationLock = new object();
107 Active = true;
108 customCommands = Array.Empty<CustomCommand>();
109 }
110
112 public void Dispose()
113 {
115 {
116 onDispose?.Invoke();
117 onDispose = null;
118 }
119 }
120
123 {
124 ArgumentNullException.ThrowIfNull(channelSink);
125
127 {
128 if (this.channelSink != null)
129 throw new InvalidOperationException("channelSink already set!");
130
131 this.channelSink = channelSink;
132 }
133 }
134
136 public Task UpdateChannels(IEnumerable<ChannelRepresentation> newChannels, CancellationToken cancellationToken)
137 {
138 logger.LogTrace("UpdateChannels...");
139 var completed = newChannels.ToList();
140 Task updateTask;
142 {
143 Channels = completed;
144 updateTask = channelSink?.UpdateChannels(newChannels, cancellationToken) ?? Task.CompletedTask;
145 }
146
147 return updateTask;
148 }
149 }
150}
IReadOnlyCollection< CustomCommand > customCommands
Backing field for CustomCommands.
readonly ICustomCommandHandler customCommandHandler
The ICustomCommandHandler for the ChatTrackingContext.
void SetChannelSink(IChannelSink channelSink)
Sets the channelSink for the IChatTrackingContext.
IEnumerable< CustomCommand > CustomCommands
IReadOnlyCollection<T> of CustomCommands in the IChatTrackingContext.
readonly ILogger< ChatTrackingContext > logger
The ILogger for the ChatTrackingContext.
Task UpdateChannels(IEnumerable< ChannelRepresentation > newChannels, CancellationToken cancellationToken)
Called when newChannels are set. A Task representing the running operation.
ChatTrackingContext(ICustomCommandHandler customCommandHandler, IEnumerable< ChannelRepresentation > initialChannels, ILogger< ChatTrackingContext > logger, Action onDispose)
Initializes a new instance of the ChatTrackingContext class.
readonly object synchronizationLock
lock object for modifying onDispose, channelSink, and Channels.
bool Active
If the CustomCommands should be used.
IReadOnlyCollection< ChannelRepresentation > Channels
IReadOnlyCollection<T> of ChannelRepresentations in the IChatTrackingContext.
Notifyee of when ChannelRepresentations in a IChatTrackingContext are updated.
Definition: IChannelSink.cs:11
Task UpdateChannels(IEnumerable< ChannelRepresentation > newChannels, CancellationToken cancellationToken)
Called when newChannels are set.
Represents a tracking of dynamic chat json files.
Handles Commands.ICommands that map to those defined in a IChatTrackingContext.