This commit is contained in:
thosakwe 2017-01-29 14:49:28 -05:00
parent 6b7d9e884f
commit 0696705809
3 changed files with 83 additions and 30 deletions

View file

@ -1,6 +1,6 @@
# angel_framework # angel_framework
[![pub 1.0.0-dev.52](https://img.shields.io/badge/pub-1.0.0--dev.52-red.svg)](https://pub.dartlang.org/packages/angel_framework) [![pub 1.0.0-dev.53](https://img.shields.io/badge/pub-1.0.0--dev.53-red.svg)](https://pub.dartlang.org/packages/angel_framework)
[![build status](https://travis-ci.org/angel-dart/framework.svg)](https://travis-ci.org/angel-dart/framework) [![build status](https://travis-ci.org/angel-dart/framework.svg)](https://travis-ci.org/angel-dart/framework)
Core libraries for the Angel Framework. Core libraries for the Angel Framework.

View file

@ -2,27 +2,62 @@
library angel_framework.hooks; library angel_framework.hooks;
import 'dart:async'; import 'dart:async';
import 'dart:mirrors';
import 'package:json_god/json_god.dart' as god; import 'package:json_god/json_god.dart' as god;
import 'angel_framework.dart'; import 'angel_framework.dart';
/// Transforms `e.data` into JSON-friendly data, i.e. a Map. /// Sequentially runs a set of [listeners].
HookedServiceEventListener toJson() { HookedServiceEventListener chainListeners(
return (HookedServiceEvent e) { Iterable<HookedServiceEventListener> listeners) {
if (e.data != null && e.data is! Map) e.data = god.serializeObject(e.data); return (HookedServiceEvent e) async {
for (HookedServiceEventListener listener in listeners) await listener(e);
}; };
} }
/// Transforms `e.data` into an instance of the given [type], /// 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.
HookedServiceEventListener toJson() {
return (HookedServiceEvent e) {
normalize(obj) {
if (obj != null && obj is! Map) obj = god.serializeObject(obj);
}
normalize(e.isBefore ? e.data : e.result);
};
}
/// Transforms `e.data` or `e.result` into an instance of the given [type],
/// if it is not already. /// if it is not already.
HookedServiceEventListener toType(Type type) { HookedServiceEventListener toType(Type type) {
return (HookedServiceEvent e) { return (HookedServiceEvent e) {
if (e.data != null && e.data.runtimeType != type) normalize(obj) {
e.data = god.deserializeDatum(e.data, outputType: type); if (obj != null && obj.runtimeType != type)
obj = god.deserializeDatum(obj, outputType: type);
}
normalize(e.isBefore ? e.data : e.result);
}; };
} }
/// Removes one or more [key]s from service results. /// Removes one or more [key]s from `e.data` or `e.result`.
/// Works on single results, and iterable results. /// Works on single objects and iterables.
HookedServiceEventListener remove(key, remover(key, obj)) { HookedServiceEventListener remove(key, remover(key, obj)) {
return (HookedServiceEvent e) async { return (HookedServiceEvent e) async {
if (!e.isAfter) throw new StateError("'remove' only works on after hooks."); if (!e.isAfter) throw new StateError("'remove' only works on after hooks.");
@ -38,8 +73,14 @@ HookedServiceEventListener remove(key, remover(key, obj)) {
return obj..remove(key); return obj..remove(key);
else if (obj is Extensible) else if (obj is Extensible)
return obj..properties.remove(key); return obj..properties.remove(key);
else else {
throw new ArgumentError("Cannot remove key 'key' from $obj."); try {
reflect(obj).setField(new Symbol(key), null);
return obj;
} catch (e) {
throw new ArgumentError("Cannot remove key 'key' from $obj.");
}
}
} }
var keys = key is Iterable ? key : [key]; var keys = key is Iterable ? key : [key];
@ -54,33 +95,45 @@ HookedServiceEventListener remove(key, remover(key, obj)) {
return r; return r;
} }
if (e.result is Iterable) { normalize(obj) async {
var r = await Future.wait(e.result.map(_removeAll)); if (obj != null) {
e.result = e.result is List ? r.toList() : r; if (obj is Iterable) {
} else var r = await Future.wait(obj.map(_removeAll));
e.result = await _removeAll(e.result); obj = obj is List ? r.toList() : r;
} else
obj = await _removeAll(obj);
}
}
await normalize(e.isBefore ? e.data : e.result);
}; };
} }
/// Disables a service method for access from a provider. /// Disables a service method for client access from a provider.
/// ///
/// [provider] can be either a String, [Providers], an Iterable of String, or a /// [provider] can be either a String, [Providers], an Iterable of String, or a
/// function that takes a [HookedServiceEvent] and returns a bool. /// function that takes a [HookedServiceEvent] and returns a bool.
/// Futures are allowed. /// Futures are allowed.
///
/// If [provider] is `null`, then it will be disabled to all clients.
HookedServiceEventListener disable([provider]) { HookedServiceEventListener disable([provider]) {
return (HookedServiceEvent e) async { return (HookedServiceEvent e) async {
if (provider is Function) { if (e.params.containsKey('provider')) {
var r = await provider(e); if (provider == null)
if (r != true) throw new AngelHttpException.methodNotAllowed();
} else {
_provide(p) => p is Providers ? p : new Providers(p.toString());
var providers =
provider is Iterable ? provider.map(_provide) : [_provide(provider)];
if (providers.any((Providers p) => p == e.params['provider'])) {
throw new AngelHttpException.methodNotAllowed(); 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());
var providers = provider is Iterable
? provider.map(_provide)
: [_provide(provider)];
if (providers.any((Providers p) => p == e.params['provider'])) {
throw new AngelHttpException.methodNotAllowed();
}
} }
} }
}; };

View file

@ -1,5 +1,5 @@
name: angel_framework name: angel_framework
version: 1.0.0-dev.52 version: 1.0.0-dev.53
description: Core libraries for the Angel framework. description: Core libraries for the Angel framework.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_framework homepage: https://github.com/angel-dart/angel_framework