tgstation-server  4.3.2
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  logger.LogTrace(value ? "Activated" : "Deactivated");
21  active = value;
22  }
23  }
24 
26  public IReadOnlyCollection<ChannelRepresentation> Channels { get; private set; }
27 
29  public IEnumerable<CustomCommand> CustomCommands
30  {
31  get => customCommands;
32  set
33  {
34  customCommands = (value ?? throw new InvalidOperationException("value cannot be null!"))
35  .Select(customCommand =>
36  {
37  customCommand.SetHandler(customCommandHandler);
38  return customCommand;
39  })
40  .ToList();
41  logger.LogTrace("Custom commands set.");
42  }
43  }
44 
49 
53  readonly ILogger<ChatTrackingContext> logger;
54 
58  readonly object synchronizationLock;
59 
63  IReadOnlyCollection<CustomCommand> customCommands;
64 
69 
73  Action onDispose;
74 
78  bool active;
79 
88  ICustomCommandHandler customCommandHandler,
89  IEnumerable<ChannelRepresentation> initialChannels,
90  ILogger<ChatTrackingContext> logger,
91  Action onDispose)
92  {
93  this.customCommandHandler = customCommandHandler ?? throw new ArgumentNullException(nameof(customCommandHandler));
94  Channels = initialChannels?.ToList() ?? throw new ArgumentNullException(nameof(initialChannels));
95  this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
96  this.onDispose = onDispose ?? throw new ArgumentNullException(nameof(onDispose));
97 
98  synchronizationLock = new object();
99  Active = true;
100  customCommands = Array.Empty<CustomCommand>();
101  }
102 
104  public void Dispose()
105  {
106  lock (synchronizationLock)
107  {
108  onDispose?.Invoke();
109  onDispose = null;
110  }
111  }
112 
114  public void SetChannelSink(IChannelSink channelSink)
115  {
116  if (channelSink == null)
117  throw new ArgumentNullException(nameof(channelSink));
118 
119  lock (synchronizationLock)
120  {
121  if (this.channelSink != null)
122  throw new InvalidOperationException("channelSink already set!");
123 
124  this.channelSink = channelSink;
125  }
126  }
127 
129  public Task UpdateChannels(IEnumerable<ChannelRepresentation> newChannels, CancellationToken cancellationToken)
130  {
131  var completed = newChannels.ToList();
132  Task updateTask;
133  lock (synchronizationLock)
134  {
135  Channels = completed;
136  updateTask = channelSink?.UpdateChannels(newChannels, cancellationToken) ?? Task.CompletedTask;
137  }
138 
139  return updateTask;
140  }
141  }
142 }
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.