| | | 1 | | namespace Kestrun.SharedState; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Thread‑safe, case‑insensitive global key/value store for reference‑type objects. |
| | | 5 | | /// </summary> |
| | | 6 | | public static class GlobalStore |
| | | 7 | | { |
| | 1 | 8 | | private static readonly SharedState _store = new(); |
| | | 9 | | |
| | | 10 | | // ── public API ────────────────────────────────────────────────── |
| | | 11 | | /// <summary>Add or overwrite a value (reference‑types only).</summary> |
| | | 12 | | public static bool Set(string name, object? value, bool allowsValueType = false) => |
| | 312 | 13 | | _store.Set(name, value, allowsValueType); |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Checks if a variable with the specified name exists in the shared state. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="name">The name of the variable to check.</param> |
| | | 19 | | /// <returns> <c>true</c> if the variable exists; otherwise, <c>false</c>.</returns> |
| | 20 | 20 | | public static bool Contains(string name) => _store.Contains(name); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Strongly‑typed fetch. Returns <c>false</c> if the key is missing |
| | | 24 | | /// or the stored value can’t be cast to <typeparamref name="T"/>. |
| | | 25 | | /// </summary> |
| | | 26 | | public static bool TryGet<T>(string name, out T? value) => |
| | 10 | 27 | | _store.TryGet(name, out value); |
| | | 28 | | |
| | | 29 | | /// <summary>Untyped fetch; <c>null</c> if absent.</summary> |
| | | 30 | | public static object? Get(string name) => |
| | 57 | 31 | | _store.Get(name); |
| | | 32 | | /// <summary>Snapshot of *all* current variables (shallow copy).</summary> |
| | | 33 | | public static IReadOnlyDictionary<string, object?> Snapshot() => |
| | 63 | 34 | | _store.Snapshot(); |
| | | 35 | | |
| | | 36 | | /// <summary>Snapshot of keys only—handy for quick listings.</summary> |
| | | 37 | | public static IReadOnlyCollection<string> KeySnapshot() => |
| | 2 | 38 | | _store.KeySnapshot(); |
| | | 39 | | /// <summary> |
| | | 40 | | /// Clears all entries in the shared state. |
| | | 41 | | /// </summary> |
| | | 42 | | public static void Clear() => |
| | 107 | 43 | | _store.Clear(); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the number of key/value pairs in the shared state. |
| | | 47 | | /// </summary> |
| | | 48 | | public static int Count => |
| | 15 | 49 | | _store.Count; |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets an enumerable collection of all keys in the shared state. |
| | | 52 | | /// </summary> |
| | | 53 | | public static IEnumerable<string> Keys => |
| | 2 | 54 | | _store.Keys; |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets an enumerable collection of all values in the shared state. |
| | | 57 | | /// </summary> |
| | | 58 | | public static IEnumerable<object?> Values => |
| | 2 | 59 | | _store.Values; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Attempts to remove the value with the specified name from the shared state. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="name">The name of the variable to remove.</param> |
| | | 65 | | /// <returns><c>true</c> if the variable was successfully removed; otherwise, <c>false</c>.</returns> |
| | | 66 | | public static bool Remove(string name) => |
| | 105 | 67 | | _store.Remove(name); |
| | | 68 | | } |