Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eventbus handler ref #469

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions Sources/Common/Communication/EventBusHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Foundation
// sourcery: InjectSingleton
public class EventBusHandler {
private let eventBus: EventBus
private let eventCache: EventCache
private let eventStorage: EventStorage
private let memoryStorage: MemoryStorage = .init()
private let logger: Logger

/// Initializes the EventBusHandler with dependencies for event bus and storage.
Expand All @@ -17,20 +17,26 @@ public class EventBusHandler {
/// - eventStorage: An instance of EventStorage to manage event persistence.
/// - logger: A logger for logging information and errors.
/// Automatically loads events from file-based storage into in-memory storage upon initialization.
public init(eventBus: EventBus, eventStorage: EventStorage, logger: Logger) {
public init(
eventBus: EventBus,
eventCache: EventCache,
eventStorage: EventStorage,
logger: Logger
) {
self.eventBus = eventBus
self.eventCache = eventCache
self.eventStorage = eventStorage
self.logger = logger
Task { await loadEventsFromStorage() }
}

/// Loads events from persistent storage into in-memory storage for quick access and event replay.
private func loadEventsFromStorage() async {
public func loadEventsFromStorage() async {
for eventType in EventTypesRegistry.allEventTypes() {
do {
let key = eventType.key
let events: [AnyEventRepresentable] = try await eventStorage.loadEvents(ofType: key)
await memoryStorage.storeEvents(events, forKey: key)
await eventCache.storeEvents(events, forKey: key)
} catch {
logger.debug("Error loading events for \(eventType): \(error)")
}
Expand Down Expand Up @@ -68,9 +74,11 @@ public class EventBusHandler {
/// - Parameter eventType: The event type for which to replay events.
private func replayEvents<E: EventRepresentable>(forType eventType: E.Type, action: @escaping (AnyEventRepresentable) -> Void) async {
let key = eventType.key
let storedEvents = await memoryStorage.eventsForKey(key)
logger.debug("Replaying events for key: \(key)")
let storedEvents = await eventCache.getEvent(key)

for event in storedEvents {
logger.debug("Found stored events for key: \(key)")
if let specificEvent = event as? E {
logger.debug("EventBusHandler: Replaying event type - \(specificEvent)")
action(specificEvent)
Expand All @@ -85,7 +93,7 @@ public class EventBusHandler {
logger.debug("EventBusHandler: Posting event - \(event)")
Task {
let hasObservers = await eventBus.post(event)
await memoryStorage.appendEvent(event)
await eventCache.addEvent(event: event)
if !hasObservers {
logger.debug("EventBusHandler: Storing event in memory - \(event)")
await storeEvent(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ extension DIGraphShared {
}

private func _get_eventBusHandler() -> EventBusHandler {
EventBusHandler(eventBus: eventBus, eventStorage: eventStorage, logger: logger)
EventBusHandler(eventBus: eventBus, eventCache: eventCache, eventStorage: eventStorage, logger: logger)
}

// EventCache (singleton)
Expand Down