-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldRegistry.cs
More file actions
42 lines (36 loc) · 1.17 KB
/
Copy pathFieldRegistry.cs
File metadata and controls
42 lines (36 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
namespace Screenplay
{
public class FieldRegistry : IDisposable
{
private readonly ScreenplayGraph _graph;
private readonly Dictionary<Type, (object obj, Action? cleanup)> _values = new();
public FieldRegistry(ScreenplayGraph graph)
{
_graph = graph;
}
public T2 Get<T, T2>() where T : ICustomField<T2>
{
if (_values.TryGetValue(typeof(T), out var obj))
return (T2)obj.obj;
foreach (var field in _graph.Fields)
{
if (field is T t)
{
t.GetValue(_graph, out var value, out var cleanup);
Action? a = cleanup is null ? null : () => cleanup(value);
_values.Add(typeof(T), (value!, a));
return value;
}
}
throw new Exception($"Could not find field {typeof(T)} on {_graph}");
}
public void Dispose()
{
foreach (var valueTuple in _values)
valueTuple.Value.cleanup?.Invoke();
_values.Clear();
}
}
}