@@ -104,8 +104,12 @@ def on_commit(self, uow):
104104
105105from functools import wraps
106106
107+ from flask import current_app
107108from invenio_db import db
108109
110+ from .events import EventBus
111+ from .events .queue import MemoryQueue
112+
109113
110114#
111115# Unit of work operations
@@ -215,6 +219,10 @@ def __init__(self, session=None):
215219 """Initialize unit of work context."""
216220 self ._session = session or db .session
217221 self ._operations = []
222+ self ._event_bus = EventBus (
223+ handlers = current_app .config ["RECORDS_RESOURCES_EVENT_HANDLERS" ],
224+ queue = MemoryQueue ()
225+ )
218226 self ._dirty = False
219227
220228 def __enter__ (self ):
@@ -267,6 +275,14 @@ def register(self, op):
267275 # Append to list of operations.
268276 self ._operations .append (op )
269277
278+ def add_event (self , event ):
279+ """Adds an event."""
280+ self ._event_bus .publish (event )
281+
282+ def handle_events (self ):
283+ """Triggers the handling of all stored events."""
284+ self ._event_bus .handle_events (uow = self )
285+
270286
271287def unit_of_work (** kwargs ):
272288 """Decorator to auto-inject a unit of work if not provided.
@@ -291,6 +307,7 @@ def inner(self, *args, **kwargs):
291307 kwargs ['uow' ] = uow
292308 res = f (self , * args , ** kwargs )
293309 uow .commit ()
310+ uow .handle_events ()
294311 return res
295312 else :
296313 return f (self , * args , ** kwargs )
0 commit comments