Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

55 lines
1.7 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using Squidex.Infrastructure.Tasks;
using Squidex.Infrastructure.Timers;
namespace Squidex.Infrastructure.EventSourcing
{
public sealed class PollingSubscription : IEventSubscription
{
private readonly CompletionTimer timer;
public PollingSubscription(
IEventStore eventStore,
IEventSubscriber eventSubscriber,
string? streamFilter,
string? position)
{
Guard.NotNull(eventStore, nameof(eventStore));
Guard.NotNull(eventSubscriber, nameof(eventSubscriber));
timer = new CompletionTimer(5000, async ct =>
{
try
{
await eventStore.QueryAsync(async storedEvent =>
{
await eventSubscriber.OnEventAsync(this, storedEvent);
position = storedEvent.EventPosition;
}, streamFilter, position, ct);
}
catch (Exception ex)
{
await eventSubscriber.OnErrorAsync(this, ex);
}
});
}
public void WakeUp()
{
timer.SkipCurrentDelay();
}
public void Unsubscribe()
{
timer.StopAsync().Forget();
}
}
}