diff --git a/src/GameCult.Networking/CultNetDatabaseSubscriptionClient.cs b/src/GameCult.Networking/CultNetDatabaseSubscriptionClient.cs index 151722d6..6540e9f9 100644 --- a/src/GameCult.Networking/CultNetDatabaseSubscriptionClient.cs +++ b/src/GameCult.Networking/CultNetDatabaseSubscriptionClient.cs @@ -45,16 +45,21 @@ namespace GameCult.Networking public Task> SubscribeAsync( string subscriptionId, IEnumerable? recordKeys = null, - IEnumerable? schemaIds = null) + IEnumerable? schemaIds = null, + bool includeSnapshot = true) { if (_disposed) throw new ObjectDisposedException(nameof(CultNetDatabaseSubscriptionClient)); if (!_client.Connected) throw new InvalidOperationException("CultNet schema client must be connected before subscribing."); if (string.IsNullOrWhiteSpace(subscriptionId)) throw new ArgumentException("Subscription id is required.", nameof(subscriptionId)); var messageId = Guid.NewGuid().ToString("N"); - var completion = new TaskCompletionSource>(TaskCreationOptions.RunContinuationsAsynchronously); - if (!_initialSnapshots.TryAdd(messageId, completion)) - throw new InvalidOperationException($"Duplicate database subscription message id '{messageId}'."); + TaskCompletionSource>? completion = null; + if (includeSnapshot) + { + completion = new TaskCompletionSource>(TaskCreationOptions.RunContinuationsAsynchronously); + if (!_initialSnapshots.TryAdd(messageId, completion)) + throw new InvalidOperationException($"Duplicate database subscription message id '{messageId}'."); + } _client.SendCultNet(new CultNetDatabaseSubscribeMessage { @@ -62,9 +67,9 @@ namespace GameCult.Networking SubscriptionId = subscriptionId, RecordKeys = recordKeys?.Where(value => !string.IsNullOrWhiteSpace(value)).Distinct(StringComparer.Ordinal).ToArray(), SchemaIds = schemaIds?.Where(value => !string.IsNullOrWhiteSpace(value)).Distinct(StringComparer.Ordinal).ToArray(), - IncludeSnapshot = true + IncludeSnapshot = includeSnapshot }); - return completion.Task; + return completion?.Task ?? Task.FromResult>(Array.Empty()); } /// Stops one live remote subscription.