diff --git a/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityHook.kt b/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityHook.kt index 16bd9c94..984b6d48 100644 --- a/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityHook.kt +++ b/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityHook.kt @@ -38,16 +38,22 @@ object EntityHook { } fun Transaction.registerChange(entityClass: EntityClass<*, Entity<*>>, entityId: EntityID<*>, changeType: EntityChangeType) { - val event = EntityChange(entityClass, entityId, changeType, id) - - if (entityEvents.lastOrNull() != event) { - entityEvents.add(event) - entitySubscribers.forEach { - it(event) + EntityChange(entityClass, entityId, changeType, id).let { + if (entityEvents.lastOrNull() != it) { + entityEvents.add(it) } } } +fun Transaction.alertSubscribers() { + entityEvents.forEach { e -> + entitySubscribers.forEach { + it(e) + } + } + entityEvents.clear() +} + fun Transaction.registeredChanges() = entityEvents.toList() fun withHook(action: (EntityChange) -> Unit, body: ()->T): T { diff --git a/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityLifecycleInterceptor.kt b/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityLifecycleInterceptor.kt index 69861f91..db346052 100644 --- a/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityLifecycleInterceptor.kt +++ b/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityLifecycleInterceptor.kt @@ -38,6 +38,7 @@ class EntityLifecycleInterceptor : GlobalStatementInterceptor { override fun beforeCommit(transaction: Transaction) { val created = transaction.flushCache() + transaction.alertSubscribers() val createdByHooks = transaction.flushCache() EntityCache.invalidateGlobalCaches(created + createdByHooks) }