feat(event-manager): Extend EventListener with getEventMetadata.

This allows specialized event listeners to get event metadata before processing certain event.
For example onResetAll does not carry information if refresh was requested from BE or is it because of internal event manager state.
This commit is contained in:
Damir Mihaljinec
2023-04-05 11:38:10 +02:00
parent e74b06201b
commit 5ed6e14a7e
3 changed files with 24 additions and 11 deletions
@@ -184,7 +184,7 @@ class EventManagerImpl @AssistedInject constructor(
eventMetadataRepository.updateNextEventId(config, metadata.eventId, nextEventId)
// Fully sequential and ordered.
eventListenersByOrder.values.flatten().forEach {
it.notifyResetAll(config)
it.notifyResetAll(config, metadata)
}
}.onFailure {
CoreLogger.e(LogTag.NOTIFY_ERROR, it)
@@ -2,13 +2,14 @@ public abstract class me/proton/core/eventmanager/domain/EventListener : me/prot
public fun <init> ()V
public abstract fun deserializeEvents (Lme/proton/core/eventmanager/domain/EventManagerConfig;Lme/proton/core/eventmanager/domain/entity/EventsResponse;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getActionMap (Lme/proton/core/eventmanager/domain/EventManagerConfig;)Ljava/util/Map;
public final fun getEventMetadata (Lme/proton/core/eventmanager/domain/EventManagerConfig;)Lme/proton/core/eventmanager/domain/entity/EventMetadata;
public abstract fun getOrder ()I
public abstract fun getType ()Lme/proton/core/eventmanager/domain/EventListener$Type;
public final fun notifyComplete (Lme/proton/core/eventmanager/domain/EventManagerConfig;Lme/proton/core/eventmanager/domain/entity/EventMetadata;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun notifyEvents (Lme/proton/core/eventmanager/domain/EventManagerConfig;Lme/proton/core/eventmanager/domain/entity/EventMetadata;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun notifyFailure (Lme/proton/core/eventmanager/domain/EventManagerConfig;Lme/proton/core/eventmanager/domain/entity/EventMetadata;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun notifyPrepare (Lme/proton/core/eventmanager/domain/EventManagerConfig;Lme/proton/core/eventmanager/domain/entity/EventMetadata;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun notifyResetAll (Lme/proton/core/eventmanager/domain/EventManagerConfig;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun notifyResetAll (Lme/proton/core/eventmanager/domain/EventManagerConfig;Lme/proton/core/eventmanager/domain/entity/EventMetadata;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun notifySuccess (Lme/proton/core/eventmanager/domain/EventManagerConfig;Lme/proton/core/eventmanager/domain/entity/EventMetadata;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun onComplete (Lme/proton/core/eventmanager/domain/EventManagerConfig;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun onCreate (Lme/proton/core/eventmanager/domain/EventManagerConfig;Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
@@ -28,6 +28,7 @@ import me.proton.core.util.kotlin.takeIfNotEmpty
abstract class EventListener<K : Any, T : Any> : TransactionHandler {
private val actionMapByConfig = mutableMapOf<EventManagerConfig, Map<Action, List<Event<K, T>>>>()
private val eventMetadataByConfig = mutableMapOf<EventManagerConfig, EventMetadata>()
/**
* Type of Event loop.
@@ -71,13 +72,15 @@ abstract class EventListener<K : Any, T : Any> : TransactionHandler {
*/
abstract val order: Int
private suspend fun setActionMap(config: EventManagerConfig, metadata: EventMetadata) {
private suspend fun setMetadata(config: EventManagerConfig, metadata: EventMetadata) {
val events = metadata.response?.let { deserializeEvents(config, it) }.orEmpty()
actionMapByConfig[config] = events.groupByAction()
eventMetadataByConfig[config] = metadata
}
private fun clearActionMap(config: EventManagerConfig) {
private fun clearMetadata(config: EventManagerConfig) {
actionMapByConfig.remove(config)
eventMetadataByConfig.remove(config)
}
/**
@@ -88,6 +91,14 @@ abstract class EventListener<K : Any, T : Any> : TransactionHandler {
fun getActionMap(config: EventManagerConfig): Map<Action, List<Event<K, T>>> =
actionMapByConfig.getOrPut(config) { emptyMap() }
/**
* Get [EventMetadata] part of the current set of modifications.
*
* Note: The map is created in first place just before [onPrepare], and cleared after [onComplete].
*/
fun getEventMetadata(config: EventManagerConfig): EventMetadata =
eventMetadataByConfig.getValue(config)
/**
* Notify to prepare any additional data (e.g. foreign key).
*
@@ -95,7 +106,7 @@ abstract class EventListener<K : Any, T : Any> : TransactionHandler {
*/
suspend fun notifyPrepare(config: EventManagerConfig, metadata: EventMetadata) {
requireNotNull(metadata.response)
setActionMap(config, metadata)
setMetadata(config, metadata)
val actions = getActionMap(config)
val entities = actions[Action.Create].orEmpty() + actions[Action.Update].orEmpty()
entities.takeIfNotEmpty()?.let { list -> onPrepare(config, list.mapNotNull { it.entity }) }
@@ -108,7 +119,7 @@ abstract class EventListener<K : Any, T : Any> : TransactionHandler {
*/
suspend fun notifyEvents(config: EventManagerConfig, metadata: EventMetadata) {
requireNotNull(metadata.response)
setActionMap(config, metadata)
setMetadata(config, metadata)
val actions = getActionMap(config)
actions[Action.Create]?.takeIfNotEmpty()?.let { list -> onCreate(config, list.mapNotNull { it.entity }) }
actions[Action.Update]?.takeIfNotEmpty()?.let { list -> onUpdate(config, list.mapNotNull { it.entity }) }
@@ -121,7 +132,8 @@ abstract class EventListener<K : Any, T : Any> : TransactionHandler {
*
* Note: No transaction wraps this function.
*/
suspend fun notifyResetAll(config: EventManagerConfig) {
suspend fun notifyResetAll(config: EventManagerConfig, metadata: EventMetadata) {
setMetadata(config, metadata)
onResetAll(config)
}
@@ -132,7 +144,7 @@ abstract class EventListener<K : Any, T : Any> : TransactionHandler {
*/
suspend fun notifySuccess(config: EventManagerConfig, metadata: EventMetadata) {
requireNotNull(metadata.response)
setActionMap(config, metadata)
setMetadata(config, metadata)
onSuccess(config)
}
@@ -142,7 +154,7 @@ abstract class EventListener<K : Any, T : Any> : TransactionHandler {
* Note: No transaction wraps this function.
*/
suspend fun notifyFailure(config: EventManagerConfig, metadata: EventMetadata) {
setActionMap(config, metadata)
setMetadata(config, metadata)
onFailure(config)
}
@@ -152,9 +164,9 @@ abstract class EventListener<K : Any, T : Any> : TransactionHandler {
* Note: No transaction wraps this function.
*/
suspend fun notifyComplete(config: EventManagerConfig, metadata: EventMetadata) {
setActionMap(config, metadata)
setMetadata(config, metadata)
onComplete(config)
clearActionMap(config)
clearMetadata(config)
}
/**