using System.Collections.Generic; using Microsoft.Ccr.Core; namespace Roblox; public class AsyncDictionary : IAsyncDictionary where TValue : new() { private readonly Interleaver interleaver = new Interleaver(); private readonly IDictionary dictionary = new Dictionary(); public void Add(TKey key, TValue value, Port result) { interleaver.DoExclusive(delegate { dictionary.Add(key, value); result.Post(EmptyValue.SharedInstance); }); } public void GetOrCreate(TKey key, Port result) { interleaver.DoExclusive(delegate { if (!dictionary.TryGetValue(key, out var value)) { value = new TValue(); dictionary.Add(key, value); } result.Post(value); }); } public void ContainsKey(TKey key, Port result) { interleaver.DoConcurrent(delegate { result.Post(dictionary.ContainsKey(key)); }); } public void Remove(TKey key, Port result) { interleaver.DoExclusive(delegate { result.Post(dictionary.Remove(key)); }); } public void TryGetValue(TKey key, PortSet result) { interleaver.DoConcurrent(delegate { if (dictionary.TryGetValue(key, out var value)) { result.Post(value); } else { result.Post(EmptyValue.SharedInstance); } }); } }