platform/packages/websocket/lib/hooks.dart
2021-05-15 15:19:35 +08:00

31 lines
970 B
Dart

import 'package:angel3_framework/angel3_framework.dart';
/// Prevents a WebSocket event from being broadcasted, to any client from the given [provider].
///
/// [provider] can be a String, a [Provider], or an Iterable.
/// If [provider] is `null`, any provider will be blocked.
HookedServiceEventListener doNotBroadcast([provider]) {
return (HookedServiceEvent e) {
if (e.params != null && e.params!.containsKey('provider')) {
var deny = false;
var providers = provider is Iterable ? provider : [provider];
for (var p in providers) {
if (deny) break;
if (p is Providers) {
deny = deny ||
p == e.params!['provider'] ||
e.params!['provider'] == p.via;
} else if (p == null) {
deny = true;
} else {
deny =
deny || (e.params!['provider'] as Providers).via == p.toString();
}
}
e.params!['broadcast'] = false;
}
};
}