platform/lib/hooks.dart

152 lines
4.2 KiB
Dart
Raw Normal View History

2017-01-28 21:08:07 +00:00
/// Easy helper hooks.
library angel_framework.hooks;
2017-01-28 21:25:02 +00:00
import 'dart:async';
2017-01-29 19:49:28 +00:00
import 'dart:mirrors';
2017-01-28 21:08:07 +00:00
import 'package:json_god/json_god.dart' as god;
import 'angel_framework.dart';
2017-01-29 19:49:28 +00:00
/// Sequentially runs a set of [listeners].
HookedServiceEventListener chainListeners(
Iterable<HookedServiceEventListener> listeners) {
return (HookedServiceEvent e) async {
for (HookedServiceEventListener listener in listeners) await listener(e);
};
}
/// Runs a [callback] on every service, and listens for future services to run it again.
AngelConfigurer hookAllServices(callback(Service service)) {
return (Angel app) async {
List<Service> touched = [];
for (var service in app.services.values) {
if (!touched.contains(service)) {
await callback(service);
touched.add(service);
}
}
app.onService.listen((service) {
if (!touched.contains(service)) return callback(service);
});
};
}
/// Transforms `e.data` or `e.result` into JSON-friendly data, i.e. a Map.
2017-01-28 21:08:07 +00:00
HookedServiceEventListener toJson() {
return (HookedServiceEvent e) {
2017-01-29 19:49:28 +00:00
normalize(obj) {
2017-02-13 00:38:33 +00:00
if (obj != null && obj is! Map) return god.serializeObject(obj);
return obj;
2017-01-29 19:49:28 +00:00
}
2017-02-13 00:38:33 +00:00
if (e.isBefore) {
return e.data = normalize(e.data);
} else
e.result = normalize(e.result);
2017-01-28 21:08:07 +00:00
};
}
2017-01-29 19:49:28 +00:00
/// Transforms `e.data` or `e.result` into an instance of the given [type],
2017-01-28 21:08:07 +00:00
/// if it is not already.
HookedServiceEventListener toType(Type type) {
return (HookedServiceEvent e) {
2017-01-29 19:49:28 +00:00
normalize(obj) {
if (obj != null && obj.runtimeType != type)
2017-02-13 00:38:33 +00:00
return god.deserializeDatum(obj, outputType: type);
return obj;
2017-01-29 19:49:28 +00:00
}
2017-02-13 00:38:33 +00:00
if (e.isBefore) {
return e.data = normalize(e.data);
} else
e.result = normalize(e.result);
2017-01-28 21:08:07 +00:00
};
}
2017-01-28 21:25:02 +00:00
2017-01-29 19:49:28 +00:00
/// Removes one or more [key]s from `e.data` or `e.result`.
/// Works on single objects and iterables.
2017-03-06 04:55:13 +00:00
///
/// Only applies to the client-side.
2017-03-04 21:12:39 +00:00
HookedServiceEventListener remove(key, [remover(key, obj)]) {
2017-01-28 21:25:02 +00:00
return (HookedServiceEvent e) async {
if (!e.isAfter) throw new StateError("'remove' only works on after hooks.");
_remover(key, obj) {
if (remover != null)
return remover(key, obj);
else if (obj is List)
return obj..remove(key);
else if (obj is Iterable)
return obj.where((k) => !key);
else if (obj is Map)
return obj..remove(key);
else if (obj is Extensible)
return obj..properties.remove(key);
2017-01-29 19:49:28 +00:00
else {
try {
reflect(obj).setField(new Symbol(key), null);
return obj;
} catch (e) {
throw new ArgumentError("Cannot remove key 'key' from $obj.");
}
}
2017-01-28 21:25:02 +00:00
}
var keys = key is Iterable ? key : [key];
_removeAll(obj) async {
var r = obj;
for (var key in keys) {
r = await _remover(key, r);
}
return r;
}
2017-01-29 19:49:28 +00:00
normalize(obj) async {
if (obj != null) {
if (obj is Iterable) {
var r = await Future.wait(obj.map(_removeAll));
obj = obj is List ? r.toList() : r;
} else
obj = await _removeAll(obj);
}
}
2017-03-06 04:55:13 +00:00
if (e.params?.containsKey('provider') == true)
await normalize(e.isBefore ? e.data : e.result);
2017-01-28 21:25:02 +00:00
};
}
2017-01-29 19:49:28 +00:00
/// Disables a service method for client access from a provider.
2017-01-28 21:25:02 +00:00
///
/// [provider] can be either a String, [Providers], an Iterable of String, or a
/// function that takes a [HookedServiceEvent] and returns a bool.
/// Futures are allowed.
2017-01-29 19:49:28 +00:00
///
/// If [provider] is `null`, then it will be disabled to all clients.
2017-01-28 21:25:02 +00:00
HookedServiceEventListener disable([provider]) {
return (HookedServiceEvent e) async {
2017-01-29 19:49:28 +00:00
if (e.params.containsKey('provider')) {
if (provider == null)
throw new AngelHttpException.methodNotAllowed();
else if (provider is Function) {
var r = await provider(e);
if (r != true) throw new AngelHttpException.methodNotAllowed();
} else {
_provide(p) => p is Providers ? p : new Providers(p.toString());
2017-01-28 21:25:02 +00:00
2017-01-29 19:49:28 +00:00
var providers = provider is Iterable
? provider.map(_provide)
: [_provide(provider)];
2017-01-28 21:25:02 +00:00
2017-01-29 19:49:28 +00:00
if (providers.any((Providers p) => p == e.params['provider'])) {
throw new AngelHttpException.methodNotAllowed();
}
2017-01-28 21:25:02 +00:00
}
}
};
}