This commit is contained in:
Tobe O 2017-12-21 15:08:45 -05:00
parent e5fee69cdc
commit 00f20b74b9
4 changed files with 63 additions and 29 deletions

View file

@ -1,3 +1,7 @@
# 1.2.0
* `ServiceList` now uses `Equality` from `package:collection` to compare items.
* `Service`s will now add service errors to corresponding streams if there is a listener.
# 1.1.0+3 # 1.1.0+3
* `ServiceList` no longer ignores empty `index` events. * `ServiceList` no longer ignores empty `index` events.

View file

@ -144,8 +144,10 @@ class ServiceList extends DelegatingList {
/// See https://github.com/angel-dart/paginate. /// See https://github.com/angel-dart/paginate.
final bool asPaginated; final bool asPaginated;
/// A function used to compare two items for equality. /// A function used to compare the ID's two items for equality.
final bool Function(dynamic, dynamic) compare; ///
/// Defaults to comparing the [idField] of `Map` instances.
final Equality _compare;
final Service service; final Service service;
@ -153,8 +155,9 @@ class ServiceList extends DelegatingList {
final List<StreamSubscription> _subs = []; final List<StreamSubscription> _subs = [];
ServiceList(this.service, ServiceList(this.service,
{this.idField, this.asPaginated: false, this.compare}) {this.idField, this.asPaginated: false, Equality compare})
: super([]) { : _compare = compare ?? new EqualityBy((map) => map[idField ?? 'id']),
super([]) {
// Index // Index
_subs.add(service.onIndexed.listen((data) { _subs.add(service.onIndexed.listen((data) {
var items = asPaginated == true ? data['data'] : data; var items = asPaginated == true ? data['data'] : data;
@ -175,7 +178,7 @@ class ServiceList extends DelegatingList {
var indices = <int>[]; var indices = <int>[];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
if (compareItems(item, this[i])) indices.add(i); if (_compare.equals(item, this[i])) indices.add(i);
} }
if (indices.isNotEmpty) { if (indices.isNotEmpty) {
@ -192,7 +195,7 @@ class ServiceList extends DelegatingList {
// Removed // Removed
_subs.add(service.onRemoved.listen((item) { _subs.add(service.onRemoved.listen((item) {
removeWhere((x) => compareItems(item, x)); removeWhere((x) => _compare.equals(item, x));
_onChange.add(this); _onChange.add(this);
})); }));
} }
@ -203,12 +206,4 @@ class ServiceList extends DelegatingList {
Future close() async { Future close() async {
_onChange.close(); _onChange.close();
} }
bool compareItems(a, b) {
if (compare != null) return compare(a, b);
if (a is Map)
return a[idField ?? 'id'] == b[idField ?? 'id'];
else
return a == b;
}
} }

View file

@ -191,8 +191,7 @@ abstract class BaseAngelClient extends Angel {
} }
@override @override
Service service(String path, Service service(String path, {Type type, AngelDeserializer deserializer}) {
{Type type, AngelDeserializer deserializer}) {
String uri = path.toString().replaceAll(straySlashes, ""); String uri = path.toString().replaceAll(straySlashes, "");
var s = new BaseAngelService(client, this, '$basePath/$uri', var s = new BaseAngelService(client, this, '$basePath/$uri',
deserializer: deserializer); deserializer: deserializer);
@ -308,6 +307,9 @@ class BaseAngelService extends Service {
try { try {
if (_invalid(response)) { if (_invalid(response)) {
if (_onIndexed.hasListener)
_onIndexed.addError(failure(response));
else
throw failure(response); throw failure(response);
} }
@ -322,6 +324,9 @@ class BaseAngelService extends Service {
_onIndexed.add(r); _onIndexed.add(r);
return r; return r;
} catch (e, st) { } catch (e, st) {
if (_onIndexed.hasListener)
_onIndexed.addError(e, st);
else
throw failure(response, error: e, stack: st); throw failure(response, error: e, stack: st);
} }
} }
@ -333,6 +338,9 @@ class BaseAngelService extends Service {
try { try {
if (_invalid(response)) { if (_invalid(response)) {
if (_onRead.hasListener)
_onRead.addError(failure(response));
else
throw failure(response); throw failure(response);
} }
@ -340,6 +348,9 @@ class BaseAngelService extends Service {
_onRead.add(r); _onRead.add(r);
return r; return r;
} catch (e, st) { } catch (e, st) {
if (_onRead.hasListener)
_onRead.addError(e, st);
else
throw failure(response, error: e, stack: st); throw failure(response, error: e, stack: st);
} }
} }
@ -351,6 +362,9 @@ class BaseAngelService extends Service {
try { try {
if (_invalid(response)) { if (_invalid(response)) {
if (_onCreated.hasListener)
_onCreated.addError(failure(response));
else
throw failure(response); throw failure(response);
} }
@ -358,6 +372,9 @@ class BaseAngelService extends Service {
_onCreated.add(r); _onCreated.add(r);
return r; return r;
} catch (e, st) { } catch (e, st) {
if (_onCreated.hasListener)
_onCreated.addError(e, st);
else
throw failure(response, error: e, stack: st); throw failure(response, error: e, stack: st);
} }
} }
@ -369,6 +386,9 @@ class BaseAngelService extends Service {
try { try {
if (_invalid(response)) { if (_invalid(response)) {
if (_onModified.hasListener)
_onModified.addError(failure(response));
else
throw failure(response); throw failure(response);
} }
@ -376,6 +396,9 @@ class BaseAngelService extends Service {
_onModified.add(r); _onModified.add(r);
return r; return r;
} catch (e, st) { } catch (e, st) {
if (_onModified.hasListener)
_onModified.addError(e, st);
else
throw failure(response, error: e, stack: st); throw failure(response, error: e, stack: st);
} }
} }
@ -387,6 +410,9 @@ class BaseAngelService extends Service {
try { try {
if (_invalid(response)) { if (_invalid(response)) {
if (_onUpdated.hasListener)
_onUpdated.addError(failure(response));
else
throw failure(response); throw failure(response);
} }
@ -394,6 +420,9 @@ class BaseAngelService extends Service {
_onUpdated.add(r); _onUpdated.add(r);
return r; return r;
} catch (e, st) { } catch (e, st) {
if (_onUpdated.hasListener)
_onUpdated.addError(e, st);
else
throw failure(response, error: e, stack: st); throw failure(response, error: e, stack: st);
} }
} }
@ -405,6 +434,9 @@ class BaseAngelService extends Service {
try { try {
if (_invalid(response)) { if (_invalid(response)) {
if (_onRemoved.hasListener)
_onRemoved.addError(failure(response));
else
throw failure(response); throw failure(response);
} }
@ -412,6 +444,9 @@ class BaseAngelService extends Service {
_onRemoved.add(r); _onRemoved.add(r);
return r; return r;
} catch (e, st) { } catch (e, st) {
if (_onRemoved.hasListener)
_onRemoved.addError(e, st);
else
throw failure(response, error: e, stack: st); throw failure(response, error: e, stack: st);
} }
} }

View file

@ -1,5 +1,5 @@
name: angel_client name: angel_client
version: 1.1.0+3 version: 1.2.0
description: Client library for the Angel framework. description: Client library for the Angel framework.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_client homepage: https://github.com/angel-dart/angel_client