tgstation-server 6.8.0
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
11
13{
16 {
18 public bool Active
19 {
20 get => active && !IsDisposed;
21 set
22 {
23 if (active == value)
24 return;
25 if (value)
26 logger.LogTrace("Activated");
27 else
28 logger.LogTrace("Deactivated");
29
30 active = value;
31 }
32 }
33
35 public IReadOnlyCollection<ChannelRepresentation> Channels { get; private set; }
36
38 public IEnumerable<CustomCommand> CustomCommands
39 {
40 get => customCommands;
41 set
42 {
43 customCommands = (value ?? throw new InvalidOperationException("value cannot be null!"))
44 .Select(customCommand =>
45 {
46 customCommand.SetHandler(customCommandHandler);
47 return customCommand;
48 })
49 .ToList();
50 logger.LogTrace("Custom commands set.");
51 }
52 }
53
58
62 readonly ILogger<ChatTrackingContext> logger;
63
67 readonly object synchronizationLock;
68
73
77 IReadOnlyCollection<CustomCommand> customCommands;
78
82 bool active;
83
93 IEnumerable<ChannelRepresentation> initialChannels,
94 ILogger<ChatTrackingContext> logger,
95 Action disposeAction)
96 : base(disposeAction)
97 {
98 this.customCommandHandler = customCommandHandler ?? throw new ArgumentNullException(nameof(customCommandHandler));
99 Channels = initialChannels?.ToList() ?? throw new ArgumentNullException(nameof(initialChannels));
100 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
101
102 synchronizationLock = new object();
103 Active = true;
104 customCommands = Array.Empty<CustomCommand>();
105 }
106
109 {
110 ArgumentNullException.ThrowIfNull(channelSink);
111
112 var originalValue = Interlocked.CompareExchange(ref this.channelSink, channelSink, null);
113 if (originalValue != null)
114 throw new InvalidOperationException("channelSink already set!");
115 }
116
118 public ValueTask UpdateChannels(IEnumerable<ChannelRepresentation> newChannels, CancellationToken cancellationToken)
119 {
120 logger.LogTrace("UpdateChannels...");
121 var completed = newChannels.ToList();
122 ValueTask updateTask;
124 {
125 Channels = completed;
126 updateTask = channelSink?.UpdateChannels(newChannels, cancellationToken) ?? ValueTask.CompletedTask;
127 }
128
129 return updateTask;
130 }
131 }
132}
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.
ChatTrackingContext(ICustomCommandHandler customCommandHandler, IEnumerable< ChannelRepresentation > initialChannels, ILogger< ChatTrackingContext > logger, Action disposeAction)
Initializes a new instance of the ChatTrackingContext class.
volatile? IChannelSink channelSink
The IChannelSink if any.
readonly object synchronizationLock
lock object for modifying Channels and calling IChannelSink.UpdateChannels(IEnumerable<ChannelReprese...
ValueTask UpdateChannels(IEnumerable< ChannelRepresentation > newChannels, CancellationToken cancellationToken)
Called when newChannels are set. A ValueTask representing the running operation.
bool Active
If the CustomCommands should be used. This should only be set by the object that sets the CustomComma...
IReadOnlyCollection< ChannelRepresentation > Channels
IReadOnlyCollection<T> of ChannelRepresentations in the IChatTrackingContext.
Runs a given disposeAction on Dispose.
readonly Action disposeAction
The Action to run on Dispose.
bool IsDisposed
If Dispose was called.
Notifyee of when ChannelRepresentations in a IChatTrackingContext are updated.
Definition: IChannelSink.cs:11
ValueTask 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.