1.2.0
This commit is contained in:
parent
e5fee69cdc
commit
00f20b74b9
4 changed files with 63 additions and 29 deletions
|
@ -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.
|
||||||
|
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,7 +307,10 @@ class BaseAngelService extends Service {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (_invalid(response)) {
|
if (_invalid(response)) {
|
||||||
throw failure(response);
|
if (_onIndexed.hasListener)
|
||||||
|
_onIndexed.addError(failure(response));
|
||||||
|
else
|
||||||
|
throw failure(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
final json = JSON.decode(response.body);
|
final json = JSON.decode(response.body);
|
||||||
|
@ -322,7 +324,10 @@ class BaseAngelService extends Service {
|
||||||
_onIndexed.add(r);
|
_onIndexed.add(r);
|
||||||
return r;
|
return r;
|
||||||
} catch (e, st) {
|
} 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 {
|
try {
|
||||||
if (_invalid(response)) {
|
if (_invalid(response)) {
|
||||||
throw failure(response);
|
if (_onRead.hasListener)
|
||||||
|
_onRead.addError(failure(response));
|
||||||
|
else
|
||||||
|
throw failure(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
var r = deserialize(JSON.decode(response.body));
|
var r = deserialize(JSON.decode(response.body));
|
||||||
_onRead.add(r);
|
_onRead.add(r);
|
||||||
return r;
|
return r;
|
||||||
} catch (e, st) {
|
} 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 {
|
try {
|
||||||
if (_invalid(response)) {
|
if (_invalid(response)) {
|
||||||
throw failure(response);
|
if (_onCreated.hasListener)
|
||||||
|
_onCreated.addError(failure(response));
|
||||||
|
else
|
||||||
|
throw failure(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
var r = deserialize(JSON.decode(response.body));
|
var r = deserialize(JSON.decode(response.body));
|
||||||
_onCreated.add(r);
|
_onCreated.add(r);
|
||||||
return r;
|
return r;
|
||||||
} catch (e, st) {
|
} 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 {
|
try {
|
||||||
if (_invalid(response)) {
|
if (_invalid(response)) {
|
||||||
throw failure(response);
|
if (_onModified.hasListener)
|
||||||
|
_onModified.addError(failure(response));
|
||||||
|
else
|
||||||
|
throw failure(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
var r = deserialize(JSON.decode(response.body));
|
var r = deserialize(JSON.decode(response.body));
|
||||||
_onModified.add(r);
|
_onModified.add(r);
|
||||||
return r;
|
return r;
|
||||||
} catch (e, st) {
|
} 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 {
|
try {
|
||||||
if (_invalid(response)) {
|
if (_invalid(response)) {
|
||||||
throw failure(response);
|
if (_onUpdated.hasListener)
|
||||||
|
_onUpdated.addError(failure(response));
|
||||||
|
else
|
||||||
|
throw failure(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
var r = deserialize(JSON.decode(response.body));
|
var r = deserialize(JSON.decode(response.body));
|
||||||
_onUpdated.add(r);
|
_onUpdated.add(r);
|
||||||
return r;
|
return r;
|
||||||
} catch (e, st) {
|
} 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 {
|
try {
|
||||||
if (_invalid(response)) {
|
if (_invalid(response)) {
|
||||||
throw failure(response);
|
if (_onRemoved.hasListener)
|
||||||
|
_onRemoved.addError(failure(response));
|
||||||
|
else
|
||||||
|
throw failure(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
var r = deserialize(JSON.decode(response.body));
|
var r = deserialize(JSON.decode(response.body));
|
||||||
_onRemoved.add(r);
|
_onRemoved.add(r);
|
||||||
return r;
|
return r;
|
||||||
} catch (e, st) {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue