2016-05-02 22:28:14 +00:00
|
|
|
part of angel_framework.http;
|
|
|
|
|
2016-06-19 05:02:41 +00:00
|
|
|
/// Wraps another service in a service that broadcasts events on actions.
|
2016-05-02 22:28:14 +00:00
|
|
|
class HookedService extends Service {
|
2016-06-19 05:02:41 +00:00
|
|
|
StreamController<HookedServiceEvent> _beforeIndexed = new StreamController<HookedServiceEvent>.broadcast();
|
|
|
|
StreamController<HookedServiceEvent> _beforeRead = new StreamController.broadcast();
|
|
|
|
StreamController<HookedServiceEvent> _beforeCreated = new StreamController.broadcast();
|
|
|
|
StreamController<HookedServiceEvent> _beforeModified = new StreamController.broadcast();
|
|
|
|
StreamController<HookedServiceEvent> _beforeUpdated = new StreamController.broadcast();
|
|
|
|
StreamController<HookedServiceEvent> _beforeRemoved = new StreamController.broadcast();
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-06-19 05:02:41 +00:00
|
|
|
Stream<HookedServiceEvent> get beforeIndexed => _beforeIndexed.stream;
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-06-19 05:02:41 +00:00
|
|
|
Stream<HookedServiceEvent> get beforeRead => _beforeRead.stream;
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-06-19 05:02:41 +00:00
|
|
|
Stream<HookedServiceEvent> get beforeCreated => _beforeCreated.stream;
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-06-19 05:02:41 +00:00
|
|
|
Stream<HookedServiceEvent> get beforeModified => _beforeModified.stream;
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-06-19 05:02:41 +00:00
|
|
|
Stream<HookedServiceEvent> get beforeUpdated => _beforeUpdated.stream;
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-06-19 05:02:41 +00:00
|
|
|
Stream<HookedServiceEvent> get beforeRemoved => _beforeRemoved.stream;
|
|
|
|
|
|
|
|
StreamController<HookedServiceEvent> _afterIndexed = new StreamController<HookedServiceEvent>.broadcast();
|
|
|
|
StreamController<HookedServiceEvent> _afterRead = new StreamController<HookedServiceEvent>.broadcast();
|
|
|
|
StreamController<HookedServiceEvent> _afterCreated = new StreamController<HookedServiceEvent>.broadcast();
|
|
|
|
StreamController<HookedServiceEvent> _afterModified = new StreamController<HookedServiceEvent>.broadcast();
|
|
|
|
StreamController<HookedServiceEvent> _afterUpdated = new StreamController<HookedServiceEvent>.broadcast();
|
|
|
|
StreamController<HookedServiceEvent> _afterRemoved = new StreamController<HookedServiceEvent>.broadcast();
|
|
|
|
|
|
|
|
Stream<HookedServiceEvent> get afterIndexed => _afterIndexed.stream;
|
|
|
|
|
|
|
|
Stream<HookedServiceEvent> get afterRead => _afterRead.stream;
|
|
|
|
|
|
|
|
Stream<HookedServiceEvent> get afterCreated => _afterCreated.stream;
|
|
|
|
|
|
|
|
Stream<HookedServiceEvent> get afterModified => _afterModified.stream;
|
|
|
|
|
|
|
|
Stream<HookedServiceEvent> get afterUpdated => _afterUpdated.stream;
|
|
|
|
|
|
|
|
Stream<HookedServiceEvent> get afterRemoved => _afterRemoved.stream;
|
2016-05-02 22:28:14 +00:00
|
|
|
|
|
|
|
final Service inner;
|
|
|
|
|
|
|
|
HookedService(Service this.inner);
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<List> index([Map params]) async {
|
2016-06-19 05:02:41 +00:00
|
|
|
HookedServiceEvent before = new HookedServiceEvent._base(inner, params: params);
|
|
|
|
_beforeIndexed.add(before);
|
|
|
|
|
|
|
|
if (before._canceled) {
|
|
|
|
HookedServiceEvent after = new HookedServiceEvent._base(inner, params: params, result: before.result);
|
|
|
|
_afterIndexed.add(after);
|
|
|
|
return before.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
List result = await inner.index(params);
|
|
|
|
HookedServiceEvent after = new HookedServiceEvent._base(inner, params: params, result: result);
|
|
|
|
_afterIndexed.add(after);
|
|
|
|
return result;
|
2016-05-02 22:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future read(id, [Map params]) async {
|
|
|
|
var retrieved = await inner.read(id, params);
|
2016-06-19 05:02:41 +00:00
|
|
|
_afterRead.add(retrieved);
|
2016-05-02 22:28:14 +00:00
|
|
|
return retrieved;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
2016-05-04 00:55:34 +00:00
|
|
|
Future create(data, [Map params]) async {
|
2016-05-02 22:28:14 +00:00
|
|
|
var created = await inner.create(data, params);
|
2016-06-19 05:02:41 +00:00
|
|
|
_afterCreated.add(created);
|
2016-05-02 22:28:14 +00:00
|
|
|
return created;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2016-05-04 00:55:34 +00:00
|
|
|
Future modify(id, data, [Map params]) async {
|
2016-05-02 22:28:14 +00:00
|
|
|
var modified = await inner.modify(id, data, params);
|
2016-06-19 05:02:41 +00:00
|
|
|
_afterUpdated.add(modified);
|
2016-05-02 22:28:14 +00:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
2016-05-04 00:55:34 +00:00
|
|
|
Future update(id, data, [Map params]) async {
|
2016-05-02 22:28:14 +00:00
|
|
|
var updated = await inner.update(id, data, params);
|
2016-06-19 05:02:41 +00:00
|
|
|
_afterUpdated.add(updated);
|
2016-05-02 22:28:14 +00:00
|
|
|
return updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future remove(id, [Map params]) async {
|
|
|
|
var removed = await inner.remove(id, params);
|
2016-06-19 05:02:41 +00:00
|
|
|
_afterRemoved.add(removed);
|
2016-05-02 22:28:14 +00:00
|
|
|
return removed;
|
|
|
|
}
|
2016-06-19 05:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fired when a hooked service is invoked.
|
|
|
|
class HookedServiceEvent {
|
|
|
|
/// Use this to end processing of an event.
|
|
|
|
void cancel(result) {
|
|
|
|
_canceled = true;
|
|
|
|
_result = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _canceled = false;
|
|
|
|
var id;
|
|
|
|
var data;
|
|
|
|
Map params;
|
|
|
|
var _result;
|
|
|
|
get result => _result;
|
|
|
|
/// The inner service whose method was hooked.
|
|
|
|
Service service;
|
|
|
|
|
|
|
|
HookedServiceEvent._base(Service this.service, {this.id, this.data, Map this.params: const{}, result}) {
|
|
|
|
_result = result;
|
|
|
|
}
|
2016-05-02 22:28:14 +00:00
|
|
|
}
|