Test findHookedService, etc.

This commit is contained in:
Tobe O 2018-09-12 00:29:53 -04:00
parent fed8cf74f6
commit c63cb41dfe
3 changed files with 23 additions and 12 deletions

View file

@ -493,7 +493,7 @@ class HookedServiceEvent<Id, Data, T extends Service<Id, Data>> {
/// Resolves a service from the application. /// Resolves a service from the application.
/// ///
/// Shorthand for `e.service.app.service(...)`. /// Shorthand for `e.service.app.service(...)`.
Service getService(Pattern path) => service.app.service(path); Service getService(Pattern path) => service.app.findService(path);
bool _canceled = false; bool _canceled = false;
String _eventName; String _eventName;

View file

@ -44,6 +44,7 @@ RequestHandler chain(Iterable<RequestHandler> handlers) {
/// A routable server that can handle dynamic requests. /// A routable server that can handle dynamic requests.
class Routable extends Router<RequestHandler> { class Routable extends Router<RequestHandler> {
final Map<Pattern, Service> _services = {}; final Map<Pattern, Service> _services = {};
final Map<Pattern, Service> _serviceLookups = {};
final Map configuration = {}; final Map configuration = {};
final Container _container; final Container _container;
@ -73,9 +74,18 @@ class Routable extends Router<RequestHandler> {
Stream<Service> get onService => _onService.stream; Stream<Service> get onService => _onService.stream;
/// Retrieves the service assigned to the given path. /// Retrieves the service assigned to the given path.
Service service(Pattern path) => T findService<T extends Service>(Pattern path) {
_services[path] ?? return _serviceLookups.putIfAbsent(path, () {
_services[path.toString().replaceAll(_straySlashes, '')]; return _services[path] ??
_services[path.toString().replaceAll(_straySlashes, '')];
}) as T;
}
/// Shorthand for finding a [HookedService] in a statically-typed
HookedService<dynamic, dynamic, T> findHookedService<T extends Service>(
Pattern path) {
return findService(path) as HookedService<dynamic, dynamic, T>;
}
@override @override
Route<RequestHandler> addRoute( Route<RequestHandler> addRoute(

View file

@ -16,16 +16,17 @@ main() {
HttpServer server; HttpServer server;
String url; String url;
http.Client client; http.Client client;
HookedService Todos; HookedService todoService;
setUp(() async { setUp(() async {
app = new Angel(reflector: MirrorsReflector()); app = new Angel(reflector: MirrorsReflector());
client = new http.Client(); client = new http.Client();
app.use('/todos', new MapService()); app.use('/todos', new MapService());
app.use('/books', new BookService()); app.use('/books', new BookService());
Todos = app.service("todos") as HookedService;
Todos.beforeAllStream().listen((e) { todoService = app.findHookedService<MapService>('todos');
todoService.beforeAllStream().listen((e) {
print('Fired ${e.eventName}! Data: ${e.data}; Params: ${e.params}'); print('Fired ${e.eventName}! Data: ${e.data}; Params: ${e.params}');
}); });
@ -43,13 +44,13 @@ main() {
url = null; url = null;
client.close(); client.close();
client = null; client = null;
Todos = null; todoService = null;
}); });
test("listen before and after", () async { test("listen before and after", () async {
int count = 0; int count = 0;
Todos todoService
..beforeIndexed.listen((_) { ..beforeIndexed.listen((_) {
count++; count++;
}) })
@ -63,7 +64,7 @@ main() {
}); });
test("cancel before", () async { test("cancel before", () async {
Todos.beforeCreated todoService.beforeCreated
..listen((HookedServiceEvent event) { ..listen((HookedServiceEvent event) {
event.cancel({"hello": "hooked world"}); event.cancel({"hello": "hooked world"});
}) })
@ -80,7 +81,7 @@ main() {
}); });
test("cancel after", () async { test("cancel after", () async {
Todos.afterIndexed todoService.afterIndexed
..listen((HookedServiceEvent event) async { ..listen((HookedServiceEvent event) async {
// Hooks can be Futures ;) // Hooks can be Futures ;)
event.cancel([ event.cancel([
@ -106,7 +107,7 @@ main() {
}); });
test('inject request + response', () async { test('inject request + response', () async {
HookedService books = app.service('books'); HookedService books = app.findService('books');
books.beforeIndexed.listen((e) { books.beforeIndexed.listen((e) {
expect([e.request, e.response], everyElement(isNotNull)); expect([e.request, e.response], everyElement(isNotNull));