tgstation-server  4.4.0
The /tg/station 13 server suite
ChatTrackingContext.cs
Go to the documentation of this file.
1 using Microsoft.Extensions.Logging;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Threading;
6 using System.Threading.Tasks;
8 
9 namespace Tgstation.Server.Host.Components.Chat
10 {
13  {
15  public bool Active
16  {
17  get => active;
18  set
19  {
20  if (active == value)
21  return;
22  logger.LogTrace(value ? "Activated" : "Deactivated");
23  active = value;
24  }
25  }
26 
28  public IReadOnlyCollection<ChannelRepresentation> Channels { get; private set; }
29 
31  public IEnumerable<CustomCommand> CustomCommands
32  {
33  get => customCommands;
34  set
35  {
36  customCommands = (value ?? throw new InvalidOperationException("value cannot be null!"))
37  .Select(customCommand =>
38  {
39  customCommand.SetHandler(customCommandHandler);
40  return customCommand;
41  })
42  .ToList();
43  logger.LogTrace("Custom commands set.");
44  }
45  }
46 
51 
55  readonly ILogger<ChatTrackingContext> logger;
56 
60  readonly object synchronizationLock;
61 
65  IReadOnlyCollection<CustomCommand> customCommands;
66 
71 
75  Action onDispose;
76 
80  bool active;
81 
90  ICustomCommandHandler customCommandHandler,
91  IEnumerable<ChannelRepresentation> initialChannels,
92  ILogger<ChatTrackingContext> logger,
93  Action onDispose)
94  {
95  this.customCommandHandler = customCommandHandler ?? throw new ArgumentNullException(nameof(customCommandHandler));
96  Channels = initialChannels?.ToList() ?? throw new ArgumentNullException(nameof(initialChannels));
97  this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
98  this.onDispose = onDispose ?? throw new ArgumentNullException(nameof(onDispose));
99 
100  synchronizationLock = new object();
101  Active = true;
102  customCommands = Array.Empty<CustomCommand>();
103  }
104 
106  public void Dispose()
107  {
108  lock (synchronizationLock)
109  {
110  onDispose?.Invoke();
111  onDispose = null;
112  }
113  }
114 
116  public void SetChannelSink(IChannelSink channelSink)
117  {
118  if (channelSink == null)
119  throw new ArgumentNullException(nameof(channelSink));
120 
121  lock (synchronizationLock)
122  {
123  if (this.channelSink != null)
124  throw new InvalidOperationException("channelSink already set!");
125 
126  this.channelSink = channelSink;
127  }
128  }
129 
131  public Task UpdateChannels(IEnumerable<ChannelRepresentation> newChannels, CancellationToken cancellationToken)
132  {
133  logger.LogTrace("UpdateChannels...");
134  var completed = newChannels.ToList();
135  Task updateTask;
136  lock (synchronizationLock)
137  {
138  Channels = completed;
139  updateTask = channelSink?.UpdateChannels(newChannels, cancellationToken) ?? Task.CompletedTask;
140  }
141 
142  return updateTask;
143  }
144  }
145 }
readonly object synchronizationLock
object for modifying onDispose, channelSink, and Channels.
Notifyee of when ChannelRepresentations in a IChatTrackingContext are updated.
Definition: IChannelSink.cs:10
readonly ILogger< ChatTrackingContext > logger
The ILogger for the ChatTrackingContext.
Task UpdateChannels(IEnumerable< ChannelRepresentation > newChannels, CancellationToken cancellationToken)
Called when newChannels are set.
void SetChannelSink(IChannelSink channelSink)
Sets the channelSink for the IChatTrackingContext.
Handles Commands.ICommands that map to those defined in a IChatTrackingContext
Represents a tracking of dynamic chat json files
Task UpdateChannels(IEnumerable< ChannelRepresentation > newChannels, CancellationToken cancellationToken)
Called when newChannels are set.
ChatTrackingContext(ICustomCommandHandler customCommandHandler, IEnumerable< ChannelRepresentation > initialChannels, ILogger< ChatTrackingContext > logger, Action onDispose)
Initializes a new instance of the ChatTrackingContext .
readonly ICustomCommandHandler customCommandHandler
The ICustomCommandHandler for the ChatTrackingContext.
IReadOnlyCollection< CustomCommand > customCommands
Backing field for CustomCommands.