From 3da0a4337465ffe18d54de7fc070a6615798a8f6 Mon Sep 17 00:00:00 2001 From: Mad Sheogorath Date: Wed, 15 Dec 2021 23:23:37 +0300 Subject: [PATCH] Bugfix: concurrent modifications during iteration occurred in some cases while processing events. --- CHANGELOG.md | 3 +++ lib/core/events.dart | 19 ++++++++++++------- pubspec.yaml | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b5f178..06e3dc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v1.1.3 +* Bugfix: concurrent modifications during iteration occurred in some cases while processing events. + ## v1.1.2 * Bugfix: expireAfterSeconds option for IndexDescriptor of CollectionDescriptor was ignored. diff --git a/lib/core/events.dart b/lib/core/events.dart index bcf4a6c..8779945 100644 --- a/lib/core/events.dart +++ b/lib/core/events.dart @@ -60,18 +60,23 @@ class Events { _eventHandlers[T]?.remove(handler); } + Future _tryExecute(Function handler, Event event) async { + try { + await handler(event); + } + catch (err, stack) { + _server._logger.error('Event handler execution error: $err', stack); + } + } + Future dispatch(Event event) async { - if (event is! Event) throw Exception('$event is not an Event class instance'); + final List> futures = >[]; if (_eventHandlers[event.runtimeType] != null) { for (final Function handler in _eventHandlers[event.runtimeType]!) { - try { - await handler(event); - } - catch (err, stack) { - _server._logger.error('Event handler execution error: $err', stack); - } + futures.add(_tryExecute(handler, event)); } } + await Future.wait(futures); } void dispose() { diff --git a/pubspec.yaml b/pubspec.yaml index 4caaae7..102c6c1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: serveme description: Backend server framework designed for a quick development of modular WebSocket based server applications with MongoDB integration. -version: 1.1.2 +version: 1.1.3 homepage: https://github.com/sourcecaster/serveme repository: https://github.com/sourcecaster/serveme issue_tracker: https://github.com/sourcecaster/serveme/issues