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
* `ServiceList` no longer ignores empty `index` events.

View file

@ -144,8 +144,10 @@ class ServiceList extends DelegatingList {
/// See https://github.com/angel-dart/paginate.
final bool asPaginated;
/// A function used to compare two items for equality.
final bool Function(dynamic, dynamic) compare;
/// A function used to compare the ID's two items for equality.
///
/// Defaults to comparing the [idField] of `Map` instances.
final Equality _compare;
final Service service;
@ -153,8 +155,9 @@ class ServiceList extends DelegatingList {
final List<StreamSubscription> _subs = [];
ServiceList(this.service,
{this.idField, this.asPaginated: false, this.compare})
: super([]) {
{this.idField, this.asPaginated: false, Equality compare})
: _compare = compare ?? new EqualityBy((map) => map[idField ?? 'id']),
super([]) {
// Index
_subs.add(service.onIndexed.listen((data) {
var items = asPaginated == true ? data['data'] : data;
@ -175,7 +178,7 @@ class ServiceList extends DelegatingList {
var indices = <int>[];
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) {
@ -192,7 +195,7 @@ class ServiceList extends DelegatingList {
// Removed
_subs.add(service.onRemoved.listen((item) {
removeWhere((x) => compareItems(item, x));
removeWhere((x) => _compare.equals(item, x));
_onChange.add(this);
}));
}
@ -203,12 +206,4 @@ class ServiceList extends DelegatingList {
Future close() async {
_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
Service service(String path,
{Type type, AngelDeserializer deserializer}) {
Service service(String path, {Type type, AngelDeserializer deserializer}) {
String uri = path.toString().replaceAll(straySlashes, "");
var s = new BaseAngelService(client, this, '$basePath/$uri',
deserializer: deserializer);
@ -308,7 +307,10 @@ class BaseAngelService extends Service {
try {
if (_invalid(response)) {
throw failure(response);
if (_onIndexed.hasListener)
_onIndexed.addError(failure(response));
else
throw failure(response);
}
final json = JSON.decode(response.body);
@ -322,7 +324,10 @@ class BaseAngelService extends Service {
_onIndexed.add(r);
return r;
} catch (e, st) {
throw failure(response, error: e, stack: st);
if (_onIndexed.hasListener)
_onIndexed.addError(e, st);
else
throw failure(response, error: e, stack: st);
}
}
@ -333,14 +338,20 @@ class BaseAngelService extends Service {
try {
if (_invalid(response)) {
throw failure(response);
if (_onRead.hasListener)
_onRead.addError(failure(response));
else
throw failure(response);
}
var r = deserialize(JSON.decode(response.body));
_onRead.add(r);
return r;
} catch (e, st) {
throw failure(response, error: e, stack: st);
if (_onRead.hasListener)
_onRead.addError(e, st);
else
throw failure(response, error: e, stack: st);
}
}
@ -351,14 +362,20 @@ class BaseAngelService extends Service {
try {
if (_invalid(response)) {
throw failure(response);
if (_onCreated.hasListener)
_onCreated.addError(failure(response));
else
throw failure(response);
}
var r = deserialize(JSON.decode(response.body));
_onCreated.add(r);
return r;
} catch (e, st) {
throw failure(response, error: e, stack: st);
if (_onCreated.hasListener)
_onCreated.addError(e, st);
else
throw failure(response, error: e, stack: st);
}
}
@ -369,14 +386,20 @@ class BaseAngelService extends Service {
try {
if (_invalid(response)) {
throw failure(response);
if (_onModified.hasListener)
_onModified.addError(failure(response));
else
throw failure(response);
}
var r = deserialize(JSON.decode(response.body));
_onModified.add(r);
return r;
} catch (e, st) {
throw failure(response, error: e, stack: st);
if (_onModified.hasListener)
_onModified.addError(e, st);
else
throw failure(response, error: e, stack: st);
}
}
@ -387,14 +410,20 @@ class BaseAngelService extends Service {
try {
if (_invalid(response)) {
throw failure(response);
if (_onUpdated.hasListener)
_onUpdated.addError(failure(response));
else
throw failure(response);
}
var r = deserialize(JSON.decode(response.body));
_onUpdated.add(r);
return r;
} catch (e, st) {
throw failure(response, error: e, stack: st);
if (_onUpdated.hasListener)
_onUpdated.addError(e, st);
else
throw failure(response, error: e, stack: st);
}
}
@ -405,14 +434,20 @@ class BaseAngelService extends Service {
try {
if (_invalid(response)) {
throw failure(response);
if (_onRemoved.hasListener)
_onRemoved.addError(failure(response));
else
throw failure(response);
}
var r = deserialize(JSON.decode(response.body));
_onRemoved.add(r);
return r;
} catch (e, st) {
throw failure(response, error: e, stack: st);
if (_onRemoved.hasListener)
_onRemoved.addError(e, st);
else
throw failure(response, error: e, stack: st);
}
}
}

View file

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