Skip to content

Commit

Permalink
feat: update EventStore
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Mar 14, 2024
1 parent b43f06c commit d458df3
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
14 changes: 10 additions & 4 deletions src/WeihanLi.Common/Event/EventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,22 @@ protected EventBase(string eventId, DateTimeOffset eventAt)
}
}

internal interface IEventWrapper
internal interface IEvent
{
EventProperties Properties { get; }
object Data { get; }
object? Data { get; }
}

internal sealed class EventWrapper<T> : IEventWrapper
public interface IEvent<out T>
{
EventProperties Properties { get; }
T Data { get; }
}

internal sealed class EventWrapper<T> : IEvent, IEvent<T>
{
public required T Data { get; init; }
object IEventWrapper.Data => Data!;
object? IEvent.Data => (object?)Data;
public required EventProperties Properties { get; init; }
}

Expand Down
4 changes: 2 additions & 2 deletions src/WeihanLi.Common/Event/EventQueueInMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace WeihanLi.Common.Event;

public sealed class EventQueueInMemory : IEventQueue
{
private readonly ConcurrentDictionary<string, ConcurrentQueue<IEventWrapper>> _eventQueues = new();
private readonly ConcurrentDictionary<string, ConcurrentQueue<IEvent>> _eventQueues = new();

public ICollection<string> GetQueues() => _eventQueues.Keys;

Expand All @@ -35,7 +35,7 @@ public bool Enqueue<TEvent>(string queueName, TEvent @event, EventProperties? pr
Data = @event,
Properties = properties
};
var queue = _eventQueues.GetOrAdd(queueName, _ => new ConcurrentQueue<IEventWrapper>());
var queue = _eventQueues.GetOrAdd(queueName, _ => new ConcurrentQueue<IEvent>());
queue.Enqueue(internalEvent);
return true;
}
Expand Down
17 changes: 6 additions & 11 deletions src/WeihanLi.Common/Event/EventStoreInMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,19 @@ namespace WeihanLi.Common.Event;

public sealed class EventStoreInMemory : IEventStore
{
private readonly ConcurrentDictionary<string, IEventBase> _events = new();
private readonly ConcurrentDictionary<string, IEvent> _events = new();

public int SaveEvents(ICollection<IEventBase> events)
public Task<int> SaveEventsAsync(ICollection<IEvent> events)

Check failure on line 13 in src/WeihanLi.Common/Event/EventStoreInMemory.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

Inconsistent accessibility: parameter type 'ICollection<IEvent>' is less accessible than method 'EventStoreInMemory.SaveEventsAsync(ICollection<IEvent>)'

Check failure on line 13 in src/WeihanLi.Common/Event/EventStoreInMemory.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

Inconsistent accessibility: parameter type 'ICollection<IEvent>' is less accessible than method 'EventStoreInMemory.SaveEventsAsync(ICollection<IEvent>)'

Check failure on line 13 in src/WeihanLi.Common/Event/EventStoreInMemory.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

Inconsistent accessibility: parameter type 'ICollection<IEvent>' is less accessible than method 'EventStoreInMemory.SaveEventsAsync(ICollection<IEvent>)'

Check failure on line 13 in src/WeihanLi.Common/Event/EventStoreInMemory.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

Inconsistent accessibility: parameter type 'ICollection<IEvent>' is less accessible than method 'EventStoreInMemory.SaveEventsAsync(ICollection<IEvent>)'

Check failure on line 13 in src/WeihanLi.Common/Event/EventStoreInMemory.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

Inconsistent accessibility: parameter type 'ICollection<IEvent>' is less accessible than method 'EventStoreInMemory.SaveEventsAsync(ICollection<IEvent>)'
{
if (events.IsNullOrEmpty())
return 0;
return Task.FromResult(0);

return events.Count(@event => _events.TryAdd(@event.EventId, @event));
return Task.FromResult(events.Count(@event => _events.TryAdd(@event.Properties.EventId, @event)));
}

public Task<int> SaveEventsAsync(ICollection<IEventBase> events) => Task.FromResult(SaveEvents(events));

public int DeleteEvents(ICollection<string> eventIds)
private int DeleteEvents(ICollection<string> eventIds)
{
if (eventIds.IsNullOrEmpty())
return 0;

return eventIds.Count(eventId => _events.TryRemove(eventId, out _));
return eventIds.IsNullOrEmpty() ? 0 : eventIds.Count(eventId => _events.TryRemove(eventId, out _));
}

public Task<int> DeleteEventsAsync(ICollection<string> eventIds) => Task.FromResult(DeleteEvents(eventIds));
Expand Down
4 changes: 1 addition & 3 deletions src/WeihanLi.Common/Event/IEventStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace WeihanLi.Common.Event;

public interface IEventStore
{
int SaveEvents(ICollection<IEventBase> events);
Task<int> SaveEventsAsync(ICollection<IEventBase> events);
int DeleteEvents(ICollection<string> eventIds);
Task<int> SaveEventsAsync(ICollection<IEvent> events);

Check failure on line 8 in src/WeihanLi.Common/Event/IEventStore.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

Inconsistent accessibility: parameter type 'ICollection<IEvent>' is less accessible than method 'IEventStore.SaveEventsAsync(ICollection<IEvent>)'

Check failure on line 8 in src/WeihanLi.Common/Event/IEventStore.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

Inconsistent accessibility: parameter type 'ICollection<IEvent>' is less accessible than method 'IEventStore.SaveEventsAsync(ICollection<IEvent>)'

Check failure on line 8 in src/WeihanLi.Common/Event/IEventStore.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

Inconsistent accessibility: parameter type 'ICollection<IEvent>' is less accessible than method 'IEventStore.SaveEventsAsync(ICollection<IEvent>)'

Check failure on line 8 in src/WeihanLi.Common/Event/IEventStore.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

Inconsistent accessibility: parameter type 'ICollection<IEvent>' is less accessible than method 'IEventStore.SaveEventsAsync(ICollection<IEvent>)'

Check failure on line 8 in src/WeihanLi.Common/Event/IEventStore.cs

View workflow job for this annotation

GitHub Actions / Running tests on ubuntu-latest

Inconsistent accessibility: parameter type 'ICollection<IEvent>' is less accessible than method 'IEventStore.SaveEventsAsync(ICollection<IEvent>)'
Task<int> DeleteEventsAsync(ICollection<string> eventIds);
}
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Extensions/JsonSerializeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static class JsonSerializeExtension

public static JsonSerializerSettings SerializerSettingsWith(Action<JsonSerializerSettings>? action)
{
if (null == action)
if (action is null)
return DefaultSerializerSettings;

var serializerSettings = GetDefaultSerializerSettings();
Expand Down

0 comments on commit d458df3

Please sign in to comment.