platform/packages/websocket/lib/hooks.dart

31 lines
957 B
Dart
Raw Normal View History

2017-04-10 01:45:45 +00:00
import 'package:angel_framework/angel_framework.dart';
2017-04-10 01:50:35 +00:00
/// Prevents a WebSocket event from being broadcasted, to any client from the given [provider].
///
/// [provider] can be a String, a [Provider], or an Iterable.
2017-04-23 18:40:30 +00:00
/// If [provider] is `null`, any provider will be blocked.
HookedServiceEventListener doNotBroadcast([provider]) {
2017-04-10 01:45:45 +00:00
return (HookedServiceEvent e) {
2017-04-10 01:50:35 +00:00
if (e.params != null && e.params.containsKey('provider')) {
bool deny = false;
Iterable 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;
2017-04-23 18:40:30 +00:00
} else if (p == null) {
deny = true;
2017-04-10 01:50:35 +00:00
} else
deny =
deny || (e.params['provider'] as Providers).via == p.toString();
}
e.params['broadcast'] = false;
}
2017-04-10 01:45:45 +00:00
};
}