Erweiterungen
This commit is contained in:
1 parent
6af8cb63e6
commit
1f4234e381
3 files changed
+47
-83
No files matched your search
@@ -1,11 +1,15 @@
|
||||
typedef EventHandler<T> = void Function(T args);
|
||||
|
||||
/// The Custom Events Service that can publish data
|
||||
/// from one module/component to another.
|
||||
class MessageService {
|
||||
static final MessageService _singleton = new MessageService._internal();
|
||||
class _MapEntry {
|
||||
late dynamic owner;
|
||||
|
||||
Map<String, dynamic> _callbackMaps = Map<String, dynamic>();
|
||||
late dynamic handler;
|
||||
}
|
||||
|
||||
class MessageService {
|
||||
static final MessageService _singleton = MessageService._internal();
|
||||
|
||||
final Map<String, Set<_MapEntry>> _callbackMaps = <String, Set<_MapEntry>>{};
|
||||
|
||||
factory MessageService() {
|
||||
return _singleton;
|
||||
@@ -13,80 +17,39 @@ class MessageService {
|
||||
|
||||
MessageService._internal();
|
||||
|
||||
/// Subscribe to an event topic. Events that
|
||||
/// get posted to that topic will trigger
|
||||
/// the provided handler
|
||||
/// * [topic] The topic to subscribe to
|
||||
/// * [handler] The event handler
|
||||
subscribe<T>(String topic, EventHandler<T>? handler) {
|
||||
List<EventHandler<T>>? handlers = _callbackMaps[topic];
|
||||
if (handler == null) {
|
||||
_callbackMaps.remove(topic);
|
||||
return;
|
||||
}
|
||||
if (handlers == null) {
|
||||
handlers = [];
|
||||
_callbackMaps[topic] = handlers;
|
||||
}
|
||||
handlers.add(handler);
|
||||
}
|
||||
subscribe<T>(String topic, dynamic owner, EventHandler<T>? handler) {
|
||||
var entry = _MapEntry();
|
||||
entry.owner = owner;
|
||||
entry.handler = handler;
|
||||
|
||||
/// Unsubscribe from the given topic. Your handler
|
||||
/// will no longer recieve events published to this
|
||||
/// topic
|
||||
/// * [topic] The topic to unsubscribe from
|
||||
/// * [handler] The event handler
|
||||
unsubscribe<T>(String topic, {EventHandler<T>? handler}) {
|
||||
if (handler == null) {
|
||||
_callbackMaps.remove(topic);
|
||||
return;
|
||||
}
|
||||
|
||||
final handlers = _callbackMaps[topic];
|
||||
if (handlers == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We now need to remove specific handler
|
||||
final handlerIndex = handlers.indexOf(handler);
|
||||
|
||||
if (handlerIndex >= 0) {
|
||||
handlers.removeAt(handlerIndex);
|
||||
if (handlers.length == 0) {
|
||||
_callbackMaps.remove(topic);
|
||||
}
|
||||
if (_callbackMaps.containsKey(topic)) {
|
||||
_callbackMaps[topic]!.remove(entry);
|
||||
_callbackMaps[topic]!.add(entry);
|
||||
} else {
|
||||
_callbackMaps[topic] = <_MapEntry>{};
|
||||
_callbackMaps[topic]!.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
/// Publish an Event to given topic.
|
||||
/// * [topic] The topic to publish to
|
||||
/// * [arguments] The arguments to pass to
|
||||
/// the handlers. It's better if custom
|
||||
/// Structure is used.
|
||||
publish<T>(String topic, {T? arguments}) {
|
||||
List<EventHandler<T>>? handlers;
|
||||
try {
|
||||
handlers = _callbackMaps[topic];
|
||||
} catch (_) {
|
||||
throw FormatException(
|
||||
'Type mismatch between Publisher Arguments and what Subscriber Expects');
|
||||
}
|
||||
unsubscribe<T>(String topic, dynamic owner) {
|
||||
if (_callbackMaps.containsKey(topic)) {
|
||||
for (var entry in _callbackMaps[topic]!) {
|
||||
if (entry.owner == owner) {
|
||||
_callbackMaps[topic]!.remove(entry);
|
||||
|
||||
if (handlers == null || handlers.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
handlers.forEach((EventHandler<T> handler) {
|
||||
try {
|
||||
if (arguments != null) {
|
||||
handler(arguments);
|
||||
} else {
|
||||
throw 'arguments null';
|
||||
break;
|
||||
}
|
||||
} catch (_) {
|
||||
print('Error Occured while executing handler for topic $topic');
|
||||
print(_);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
publish<T>(String topic, {T? arguments}) {
|
||||
var entries = _callbackMaps[topic];
|
||||
|
||||
if (entries != null) {
|
||||
for (var entry in entries) {
|
||||
entry.handler(arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user