public sealed class RunicConnectionState<T> : IDisposable A value per connection of a Window, such as a selection, a cursor or a draft that each page keeps for itself, removed when its connection disconnects.
Type parameters
TThe value of one connection.
Remarks
Create it in a ViewModel's constructor from the Window's IRunicConnections, and read the caller's value in a command with RunicConnectionState.TryGetCaller: _drafts = new RunicConnectionState<Draft>(connections, _ => new Draft());
[RelayCommand]
private void Type(string text) { if (_drafts.TryGetCaller(out var draft)) draft.Text = text; }
A value is created on first use, only for a connection that IRunicConnections.Current lists, so that connection's IRunicConnections.Disconnected is still to come; it then removes the value and disposes it when it is IDisposable. RunicConnectionState.Dispose stops it and disposes every value; dispose it with the ViewModel. A value whose Dispose throws is logged as event 1231 ConnectionStateDisposeFailed, and the others are still disposed. A closing Window raises no IRunicConnections.Disconnected, so its values stay until RunicConnectionState.Dispose.
It is thread-safe. The factory runs under its lock: it must not call RunicConnectionState.TryGet or RunicConnectionState.TryGetCaller of the same state, which would create the value again.
Constructors
RunicConnectionState
public RunicConnectionState(IRunicConnections connections, Func<RunicConnection, T> create) Keeps a value for each connection of connections, created with create.
Parameters
connectionsThe Window's connections.
createCreates the value of a connection on first use. It receives the Window's own
RunicConnection.
Properties
Values
public IReadOnlyDictionary<string, T> Values { get; } The values that exist now, by connection key.
Methods
Dispose
public void Dispose() Stops following the connections and disposes the values that are IDisposable.
TryGet
public bool TryGet(RunicConnection connection, out T value) Gets the value of connection, creating it on first use, while the Window lists the connection.
Parameters
connectionA connection of the Window, such as one from
IRunicConnections.Current. Both keys must be the Window's: aRunicConnectionwith the right connection key and another client key gets nothing.valueThe connection's value.
Returns
false when the Window does not list the connection with these keys, or the state was disposed.
TryGetCaller
public bool TryGetCaller(out T value) Gets the value of IRunicConnections.Caller, creating it on first use, while the Window lists that connection.
Parameters
valueThe caller's value.
Returns
false outside a command of this Window, after the command ended, or when the caller's connection is not listed, for example because it closed.