This commit is contained in:
Tobe O 2017-12-10 00:31:34 -05:00
parent d72870a2f1
commit caf3dda1b7
5 changed files with 99 additions and 7 deletions

2
CHANGELOG.md Normal file
View file

@ -0,0 +1,2 @@
# 1.1.0+1
* Added `unwrap`.

View file

@ -163,3 +163,13 @@ main() async {
app.authenticateViaJwt('<some-jwt>'); app.authenticateViaJwt('<some-jwt>');
} }
``` ```
**Unwrapping Events**
In several cases, it may be cumbersome or inconsistent to handle events
as `WebSocketEvent`s. Call `unwrap` to receive a `Service` that returns the underlying `data`
objects.
```dart
foo() async {
var unwrapped = app.service('api/todos').unwrap();
}
```

View file

@ -136,8 +136,7 @@ abstract class BaseWebSocketClient extends BaseAngelClient {
WebSocketsService service(String path, WebSocketsService service(String path,
{Type type, AngelDeserializer deserializer}) { {Type type, AngelDeserializer deserializer}) {
String uri = path.toString().replaceAll(_straySlashes, ''); String uri = path.toString().replaceAll(_straySlashes, '');
return new WebSocketsService(socket, this, uri, return new WebSocketsService(socket, this, uri, deserializer: deserializer);
deserializer: deserializer);
} }
/// Starts listening for data. /// Starts listening for data.
@ -389,6 +388,9 @@ class WebSocketsService extends Service {
eventName: '$path::${ACTION_REMOVE}', id: id, params: params ?? {})); eventName: '$path::${ACTION_REMOVE}', id: id, params: params ?? {}));
return null; return null;
} }
/// Returns a wrapper that queries this service, but fires the `data` of `WebSocketEvent`s, rather than the events themselves.
Service unwrap() => new _WebSocketsDataService(this);
} }
/// Contains a dynamic Map of [WebSocketEvent] streams. /// Contains a dynamic Map of [WebSocketEvent] streams.
@ -409,3 +411,81 @@ class WebSocketExtraneousEventHandler {
return _events[index].stream; return _events[index].stream;
} }
} }
class _WebSocketsDataService extends Service {
final WebSocketsService service;
Stream _onIndexed, _onRead, _onCreated, _onModified, _onUpdated, _onRemoved;
_WebSocketsDataService(this.service);
getData(WebSocketEvent e) => e.data;
@override
Future remove(id, [Map params]) {
return service.remove(id, params);
}
@override
Future update(id, data, [Map params]) {
return service.update(id, data, params);
}
@override
Future modify(id, data, [Map params]) {
return service.modify(id, data, params);
}
@override
Future create(data, [Map params]) {
return service.create(data, params);
}
@override
Future read(id, [Map params]) {
return service.read(id, params);
}
@override
Future index([Map params]) {
return service.index(params);
}
@override
Future close() async {}
@override
Angel get app {
return service.app;
}
@override
Stream get onRemoved {
return _onRemoved ??= service.onRemoved.map(getData);
}
@override
Stream get onUpdated {
return _onUpdated ??= service.onUpdated.map(getData);
}
@override
Stream get onModified {
return _onModified ??= service.onModified.map(getData);
}
@override
Stream get onCreated {
return _onCreated ??= service.onCreated.map(getData);
}
@override
Stream get onRead {
return _onRead ??= service.onRead.map(getData);
}
@override
Stream get onIndexed {
return _onIndexed ??= service.onIndexed.map(getData);
}
}

View file

@ -2,7 +2,7 @@ name: angel_websocket
description: WebSocket plugin for Angel. description: WebSocket plugin for Angel.
environment: environment:
sdk: ">=1.19.0" sdk: ">=1.19.0"
version: 1.1.0-alpha+4 version: 1.1.0+1
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_websocket homepage: https://github.com/angel-dart/angel_websocket
dependencies: dependencies:

View file

@ -21,12 +21,12 @@ class TodoService extends TypedService<Todo> {
} }
testIndex(BaseWebSocketClient client) async { testIndex(BaseWebSocketClient client) async {
var Todos = client.service('api/todos'); var Todos = client.service('api/todos').unwrap();
Todos.index(); Todos.index();
var indexed = await Todos.onIndexed.first; var indexed = await Todos.onIndexed.first;
print('indexed: ${indexed.toJson()}'); print('indexed: $indexed');
expect(indexed.data, isList); expect(indexed, isList);
expect(indexed.data, isEmpty); expect(indexed, isEmpty);
} }