using System.Collections.Concurrent; using System.Runtime.CompilerServices; using Speckle.Sdk.Serialisation.V2.Send; using Speckle.Sdk.Transports; namespace Speckle.Sdk.Serialisation.V2; public class MemoryServerObjectManager(ConcurrentDictionary objects) : IServerObjectManager { public virtual async IAsyncEnumerable<(string, string)> DownloadObjects( IReadOnlyCollection objectIds, string? attributeMask, IProgress? progress, [EnumeratorCancellation] CancellationToken cancellationToken ) { foreach (var item in objects.Where(x => objectIds.Contains(x.Key))) { cancellationToken.ThrowIfCancellationRequested(); yield return (item.Key, item.Value); } await Task.CompletedTask.ConfigureAwait(false); } public virtual Task DownloadSingleObject( string objectId, IProgress? progress, CancellationToken cancellationToken ) => Task.FromResult(objects.TryGetValue(objectId, out var json) ? json : null); public virtual Task> HasObjects( IReadOnlyCollection objectIds, CancellationToken cancellationToken ) => Task.FromResult(objectIds.ToDictionary(x => x, objects.ContainsKey)); public virtual Task UploadObjects( IReadOnlyList objectToUpload, bool compressPayloads, IProgress? progress, CancellationToken cancellationToken ) { foreach (BaseItem baseItem in objectToUpload) { objects.TryAdd(baseItem.Id.Value, baseItem.Json.Value); } return Task.CompletedTask; } }