platform/packages/poll/lib/angel_poll.dart

264 lines
6.6 KiB
Dart
Raw Normal View History

2017-12-21 17:38:30 +00:00
import 'dart:async';
2017-12-21 19:24:49 +00:00
import 'package:collection/collection.dart';
2017-12-21 17:38:30 +00:00
import 'package:angel_client/angel_client.dart';
2017-12-21 19:24:49 +00:00
/// A [Service] that facilitates real-time updates via the long polling of an [inner] service.
///
/// Works well with [ServiceList].
class PollingService extends Service {
/// The underlying [Service] that does the actual communication with the server.
2017-12-21 17:38:30 +00:00
final Service inner;
2017-12-21 19:24:49 +00:00
/// Perform computations after polling to discern whether new items were created.
final bool checkForCreated;
/// Perform computations after polling to discern whether items were modified.
final bool checkForModified;
/// Perform computations after polling to discern whether items were removed.
final bool checkForRemoved;
/// An [EqualityBy] used to compare the ID's of two items.
///
/// Defaults to comparing the [idField] of two `Map` instances.
final EqualityBy compareId;
/// An [Equality] used to discern whether two items, with the same [idField], are the same item.
///
/// Defaults to [MapEquality], which deep-compares `Map` instances.
final Equality compareItems;
/// A [String] used as an index through which to compare `Map` instances.
///
/// Defaults to `id`.
2017-12-21 17:38:30 +00:00
final String idField;
2017-12-21 19:24:49 +00:00
/// If `true` (default: `false`), then `index` events will be handled as a [Map] containing a `data` field.
///
/// See https://github.com/angel-dart/paginate.
2017-12-21 17:38:30 +00:00
final bool asPaginated;
final List _items = [];
2017-12-21 19:24:49 +00:00
final List<StreamSubscription> _subs = [];
2021-06-20 12:37:20 +00:00
final StreamController<List<dynamic>> _onIndexed = StreamController(),
_onRead = StreamController(),
_onCreated = StreamController(),
_onModified = StreamController(),
_onUpdated = StreamController(),
_onRemoved = StreamController();
2017-12-21 17:38:30 +00:00
2021-06-20 12:37:20 +00:00
late Timer _timer;
2017-12-21 17:38:30 +00:00
@override
Angel get app => inner.app;
@override
2021-06-20 12:37:20 +00:00
Stream<List<dynamic>> get onIndexed => _onIndexed.stream;
2017-12-21 17:38:30 +00:00
@override
Stream get onRead => _onRead.stream;
@override
Stream get onCreated => _onCreated.stream;
@override
Stream get onModified => _onModified.stream;
@override
Stream get onUpdated => _onUpdated.stream;
@override
Stream get onRemoved => _onRemoved.stream;
2017-12-21 19:24:49 +00:00
PollingService(this.inner, Duration interval,
2021-06-20 12:37:20 +00:00
{this.checkForCreated = true,
this.checkForModified = true,
this.checkForRemoved = true,
this.idField = 'id',
this.asPaginated = false,
EqualityBy? compareId,
this.compareItems = const MapEquality()})
: compareId = compareId ?? EqualityBy((map) => map[idField]) {
_timer = Timer.periodic(interval, (_) {
index().catchError((error) {
_onIndexed.addError(error as Object);
});
2017-12-21 19:24:49 +00:00
});
var streams = <Stream, StreamController>{
inner.onRead: _onRead,
inner.onCreated: _onCreated,
inner.onModified: _onModified,
inner.onUpdated: _onUpdated,
inner.onRemoved: _onRemoved,
};
streams.forEach((stream, ctrl) {
_subs.add(stream.listen(ctrl.add, onError: ctrl.addError));
});
_subs.add(
inner.onIndexed.listen(
_handleIndexed,
onError: _onIndexed.addError,
),
);
2017-12-21 17:38:30 +00:00
}
@override
Future close() async {
_timer.cancel();
2017-12-21 19:24:49 +00:00
_subs.forEach((s) => s.cancel());
2021-06-20 12:37:20 +00:00
await _onIndexed.close();
await _onRead.close();
await _onCreated.close();
await _onModified.close();
await _onUpdated.close();
await _onRemoved.close();
2017-12-21 17:38:30 +00:00
}
2021-02-14 05:22:25 +00:00
// TODO: To revisit this logic
2017-12-21 17:38:30 +00:00
@override
2021-06-20 12:37:20 +00:00
Future<List<dynamic>> index([Map? params]) {
2017-12-21 17:38:30 +00:00
return inner.index().then((data) {
2021-02-14 05:22:25 +00:00
//return asPaginated == true ? data['data'] : data;
2021-06-20 12:37:20 +00:00
//return asPaginated == true ? data[0] : data;
return data!;
2017-12-21 17:38:30 +00:00
});
}
2021-02-14 05:22:25 +00:00
/*
@override
Future index([Map params]) {
}
*/
2017-12-21 17:38:30 +00:00
@override
2021-06-20 12:37:20 +00:00
Future remove(id, [Map<String, dynamic>? params]) {
2017-12-21 19:24:49 +00:00
return inner.remove(id, params).then((result) {
_items.remove(result);
return result;
}).catchError(_onRemoved.addError);
}
2021-06-20 12:37:20 +00:00
dynamic _handleUpdate(result) {
var index = -1;
2017-12-21 19:24:49 +00:00
2021-06-20 12:37:20 +00:00
for (var i = 0; i < _items.length; i++) {
2017-12-21 19:24:49 +00:00
if (compareId.equals(_items[i], result)) {
index = i;
break;
}
}
if (index > -1) {
_items[index] = result;
}
return result;
}
2017-12-21 17:38:30 +00:00
@override
2021-06-20 12:37:20 +00:00
Future update(id, data, [Map<String, dynamic>? params]) {
2017-12-21 19:24:49 +00:00
return inner
.update(id, data, params)
.then(_handleUpdate)
.catchError(_onUpdated.addError);
}
2017-12-21 17:38:30 +00:00
@override
2021-06-20 12:37:20 +00:00
Future modify(id, data, [Map<String, dynamic>? params]) {
2017-12-21 19:24:49 +00:00
return inner
.modify(id, data, params)
.then(_handleUpdate)
.catchError(_onModified.addError);
}
2017-12-21 17:38:30 +00:00
@override
2021-06-20 12:37:20 +00:00
Future create(data, [Map<String, dynamic>? params]) {
2017-12-21 19:24:49 +00:00
return inner.create(data, params).then((result) {
_items.add(result);
return result;
}).catchError(_onCreated.addError);
}
2017-12-21 17:38:30 +00:00
@override
2021-06-20 12:37:20 +00:00
Future read(id, [Map<String, dynamic>? params]) {
2017-12-21 19:24:49 +00:00
return inner.read(id, params);
}
2021-06-20 12:37:20 +00:00
void _handleIndexed(List<dynamic> data) {
//var items = asPaginated == true ? data['data'] : data;
var items = data;
var changesComputed = false;
2017-12-21 19:24:49 +00:00
if (checkForCreated != false) {
var newItems = <int, dynamic>{};
2021-06-20 12:37:20 +00:00
for (var i = 0; i < items.length; i++) {
2017-12-21 19:24:49 +00:00
var item = items[i];
if (!_items.any((i) => compareId.equals(i, item))) {
newItems[i] = item;
}
}
newItems.forEach((index, item) {
_items.insert(index, item);
2021-06-20 12:37:20 +00:00
_onCreated.add([item]);
2017-12-21 19:24:49 +00:00
});
changesComputed = newItems.isNotEmpty;
}
if (checkForRemoved != false) {
var removedItems = <int, dynamic>{};
2017-12-21 17:38:30 +00:00
2021-06-20 12:37:20 +00:00
for (var i = 0; i < _items.length; i++) {
2017-12-21 19:24:49 +00:00
var item = _items[i];
2017-12-21 17:38:30 +00:00
2017-12-21 19:24:49 +00:00
if (!items.any((i) => compareId.equals(i, item))) {
removedItems[i] = item;
}
}
2017-12-21 17:38:30 +00:00
2017-12-21 19:24:49 +00:00
removedItems.forEach((index, item) {
_items.removeAt(index);
2021-06-20 12:37:20 +00:00
_onRemoved.add([item]);
2017-12-21 19:24:49 +00:00
});
changesComputed = changesComputed || removedItems.isNotEmpty;
}
if (checkForModified != false) {
var modifiedItems = <int, dynamic>{};
for (var item in items) {
2021-06-20 12:37:20 +00:00
for (var i = 0; i < _items.length; i++) {
2017-12-21 19:24:49 +00:00
var localItem = _items[i];
if (compareId.equals(item, localItem)) {
if (!compareItems.equals(item, localItem)) {
modifiedItems[i] = item;
}
break;
}
}
}
modifiedItems.forEach((index, item) {
2021-06-20 12:37:20 +00:00
_onModified.add([_items[index] = item]);
2017-12-21 19:24:49 +00:00
});
changesComputed = changesComputed || modifiedItems.isNotEmpty;
}
if (!changesComputed) {
_items
..clear()
2021-06-20 12:37:20 +00:00
..add(items);
_onIndexed.add([items]);
2017-12-21 19:24:49 +00:00
}
2017-12-21 17:38:30 +00:00
}
}