Make this event-based

This commit is contained in:
regiostech 2016-05-03 19:42:06 -04:00
parent 28be7342b9
commit 4b6ceec17e
2 changed files with 44 additions and 16 deletions

View file

@ -12,7 +12,7 @@
<entry key="angel_framework"> <entry key="angel_framework">
<value> <value>
<list> <list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/angel_framework-0.0.0-dev.8/lib" /> <option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/angel_framework-0.0.0-dev.14/lib" />
</list> </list>
</value> </value>
</entry> </entry>
@ -61,21 +61,21 @@
<entry key="collection"> <entry key="collection">
<value> <value>
<list> <list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.5.1/lib" /> <option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.6.0/lib" />
</list> </list>
</value> </value>
</entry> </entry>
<entry key="convert"> <entry key="convert">
<value> <value>
<list> <list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/convert-1.1.0/lib" /> <option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/convert-1.1.1/lib" />
</list> </list>
</value> </value>
</entry> </entry>
<entry key="crypto"> <entry key="crypto">
<value> <value>
<list> <list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/crypto-1.0.0/lib" /> <option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/crypto-1.1.1/lib" />
</list> </list>
</value> </value>
</entry> </entry>
@ -180,7 +180,7 @@
<entry key="pool"> <entry key="pool">
<value> <value>
<list> <list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pool-1.2.3/lib" /> <option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pool-1.2.4/lib" />
</list> </list>
</value> </value>
</entry> </entry>
@ -278,7 +278,7 @@
<entry key="watcher"> <entry key="watcher">
<value> <value>
<list> <list>
<option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/watcher-0.9.7/lib" /> <option value="$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/watcher-0.9.7+2/lib" />
</list> </list>
</value> </value>
</entry> </entry>
@ -300,16 +300,16 @@
</properties> </properties>
<CLASSES> <CLASSES>
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/analyzer-0.27.2/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/analyzer-0.27.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/angel_framework-0.0.0-dev.8/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/angel_framework-0.0.0-dev.14/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-0.13.4/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/args-0.13.4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/async-1.10.0/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/async-1.10.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/barback-0.15.2+7/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/barback-0.15.2+7/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/body_parser-1.0.0-dev/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/body_parser-1.0.0-dev/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/boolean_selector-1.0.1/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/boolean_selector-1.0.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/charcode-1.1.0/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/charcode-1.1.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.5.1/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/collection-1.6.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/convert-1.1.0/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/convert-1.1.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/crypto-1.0.0/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/crypto-1.1.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/csslib-0.13.1/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/csslib-0.13.1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/glob-1.1.2/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/glob-1.1.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/html-0.12.2+2/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/html-0.12.2+2/lib" />
@ -324,7 +324,7 @@
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/package_config-0.1.3/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/package_config-0.1.3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/path-1.3.9/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/path-1.3.9/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/plugin-0.1.0/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/plugin-0.1.0/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pool-1.2.3/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pool-1.2.4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pub_semver-1.2.4/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/pub_semver-1.2.4/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf-0.6.5/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf-0.6.5/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_static-0.2.3+3/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/shelf_static-0.2.3+3/lib" />
@ -338,7 +338,7 @@
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/test-0.12.13+1/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/test-0.12.13+1/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/typed_data-1.1.2/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/typed_data-1.1.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/utf-0.9.0+3/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/utf-0.9.0+3/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/watcher-0.9.7/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/watcher-0.9.7+2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/web_socket_channel-1.0.2/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/web_socket_channel-1.0.2/lib" />
<root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/yaml-2.1.8/lib" /> <root url="file://$USER_HOME$/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/yaml-2.1.8/lib" />
</CLASSES> </CLASSES>

View file

@ -5,6 +5,8 @@ import 'dart:io';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'shared.dart'; import 'shared.dart';
List<WebSocket> sockets = [];
_respond(AngelMessage message, Service service, Angel app) async { _respond(AngelMessage message, Service service, Angel app) async {
if (message.method == 'index') { if (message.method == 'index') {
return await service.index(message.body['query']); return await service.index(message.body['query']);
@ -20,12 +22,12 @@ _respond(AngelMessage message, Service service, Angel app) async {
} }
else if (message.method == 'update') { else if (message.method == 'update') {
return await service.update( await service.update(
message.body['id'], message.body['data'] ?? {}, message.body['query']); message.body['id'], message.body['data'] ?? {}, message.body['query']);
} }
else if (message.method == 'remove') { else if (message.method == 'remove') {
return await service.remove(message.body['id'], message.body['query']); await service.remove(message.body['id'], message.body['query']);
} }
else throw new AngelHttpException.NotImplemented( else throw new AngelHttpException.NotImplemented(
@ -66,7 +68,27 @@ _handleMsg(WebSocket socket, Angel app) {
}; };
} }
websocket({String endPoint: '/ws'}) { _wireHooks(bool hookAll) {
return (Angel app) async {
for (Pattern path in app.services.keys) {
Service _service = app.services[path];
// Hook any unhooked services
if (!(_service is HookedService) && hookAll) {
app.services[path] = new HookedService(_service);
}
Service service = app.services[path];
if (service is HookedService) {
service.onIndexed.listen((List items) {
});
}
}
};
}
websocket({String endPoint: '/ws', bool hookAll: true}) {
return (Angel app) async { return (Angel app) async {
app.get(endPoint, (RequestContext req, ResponseContext res) async { app.get(endPoint, (RequestContext req, ResponseContext res) async {
if (WebSocketTransformer.isUpgradeRequest(req.underlyingRequest)) { if (WebSocketTransformer.isUpgradeRequest(req.underlyingRequest)) {
@ -76,11 +98,17 @@ websocket({String endPoint: '/ws'}) {
WebSocket socket = await WebSocketTransformer.upgrade( WebSocket socket = await WebSocketTransformer.upgrade(
req.underlyingRequest); req.underlyingRequest);
socket.listen(_handleMsg(socket, app)); sockets.add(socket);
socket.listen(_handleMsg(socket, app), onDone: () {
// Remove from cache on disconnect
sockets.remove(socket);
});
} else { } else {
throw new AngelHttpException.BadRequest( throw new AngelHttpException.BadRequest(
message: 'This endpoint is only available via WebSockets.'); message: 'This endpoint is only available via WebSockets.');
} }
}); });
await app.configure(_wireHooks(hookAll));
}; };
} }