From 9a7deb48f7ddaaa5b23e221663ec64af4ffd1c73 Mon Sep 17 00:00:00 2001 From: miyoyo Date: Sat, 6 Jul 2019 22:51:40 +0200 Subject: [PATCH 1/4] Use streams because this is literally just a stream recreation --- lib/src/hermes_base.dart | 45 +++++++++------------------------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/lib/src/hermes_base.dart b/lib/src/hermes_base.dart index 8fb1a5b..7cbc377 100644 --- a/lib/src/hermes_base.dart +++ b/lib/src/hermes_base.dart @@ -33,51 +33,26 @@ * */ -import 'dart:collection'; +import 'dart:async'; -/// [Hermes]. The messenger. class Hermes { - final Map> _callbacks = - >{}; - - static Map _instances = {}; - + static final _instances = {}; + Hermes._(); - static Hermes _get() { - if (!_instances.containsKey(T.hashCode)) { - _instances[T.hashCode] = Hermes._(); - } - return _instances[T.hashCode] as Hermes; - } - - /// [send] allows to send a message. + /// [send] will send a message to the stream linked to the type. + /// + /// Returns true if the target exists, false otherwise static bool send(T message) { - var i = _get(); - - if (!i._callbacks.containsKey(T.hashCode)) { - return false; - } - - for (var handler in i._callbacks[T.hashCode]) { - Future.microtask(() { - handler(message); - }); - } - ; - - return true; + _instances[T]?.add(message); + return _instances[T] != null ? true : false; } /// [fetch] registers callbacks for messages. /// /// whenever a message of type [T] is received, the [callback] /// is called. - static fetch(Function(T arg) callback) { - var i = _get(); - if (!i._callbacks.containsKey(T.hashCode)) { - i._callbacks[T.hashCode] = Queue(); - } - i._callbacks[T.hashCode].add(callback); + static void fetch(Function(T arg) callback) { + _instances[T] ??= StreamController.broadcast()..stream.listen(callback); } } From b6f246f527fb59baf11cb0acd883174bc24f1c12 Mon Sep 17 00:00:00 2001 From: miyoyo Date: Sat, 6 Jul 2019 23:31:20 +0200 Subject: [PATCH 2/4] Fix multiple calls to fetch --- lib/src/hermes_base.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/src/hermes_base.dart b/lib/src/hermes_base.dart index 7cbc377..f614a3e 100644 --- a/lib/src/hermes_base.dart +++ b/lib/src/hermes_base.dart @@ -53,6 +53,7 @@ class Hermes { /// whenever a message of type [T] is received, the [callback] /// is called. static void fetch(Function(T arg) callback) { - _instances[T] ??= StreamController.broadcast()..stream.listen(callback); + final target = _instances[T] ??= StreamController.broadcast(); + target.stream.listen(callback); } } From d424d34891817e1abe1bd24e88cecf1bd0847819 Mon Sep 17 00:00:00 2001 From: miyoyo Date: Tue, 9 Jul 2019 14:56:59 +0200 Subject: [PATCH 3/4] Don't fetch the stream twice, but grab a reference instead. Boosts speed significantly --- lib/src/hermes_base.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/src/hermes_base.dart b/lib/src/hermes_base.dart index f614a3e..15c5b8d 100644 --- a/lib/src/hermes_base.dart +++ b/lib/src/hermes_base.dart @@ -44,8 +44,9 @@ class Hermes { /// /// Returns true if the target exists, false otherwise static bool send(T message) { - _instances[T]?.add(message); - return _instances[T] != null ? true : false; + final target = _instances[T]; + final?.add(message); + return final != null; } /// [fetch] registers callbacks for messages. From 72bedf5fdb103145448694cb99f5f539ecee408e Mon Sep 17 00:00:00 2001 From: miyoyo Date: Tue, 9 Jul 2019 14:58:08 +0200 Subject: [PATCH 4/4] Cast the target to avoid a runtime type error. Vastly faster than wrapping the callback, and, since we create the streamcontroller right above, the `as` is safe. --- lib/src/hermes_base.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/hermes_base.dart b/lib/src/hermes_base.dart index 15c5b8d..2fdc333 100644 --- a/lib/src/hermes_base.dart +++ b/lib/src/hermes_base.dart @@ -55,6 +55,6 @@ class Hermes { /// is called. static void fetch(Function(T arg) callback) { final target = _instances[T] ??= StreamController.broadcast(); - target.stream.listen(callback); + (target as StreamController).stream.listen(callback); } }