platform/lib/src/core/anonymous_service.dart

54 lines
1.6 KiB
Dart
Raw Normal View History

2017-02-01 21:43:18 +00:00
import 'dart:async';
import 'service.dart';
/// An easy helper class to create one-off services without having to create an entire class.
///
/// Well-suited for testing.
class AnonymousService extends Service {
2018-07-12 15:19:25 +00:00
FutureOr Function([Map]) _index;
FutureOr Function(Object, [Map]) _read, _create, _remove;
FutureOr Function(Object, Object, [Map]) _modify, _update;
2017-02-01 21:43:18 +00:00
AnonymousService(
2018-07-12 15:19:25 +00:00
{FutureOr index([Map params]),
FutureOr read(id, [Map params]),
FutureOr create(data, [Map params]),
FutureOr modify(id, data, [Map params]),
FutureOr update(id, data, [Map params]),
FutureOr remove(id, [Map params])})
2017-02-01 21:43:18 +00:00
: super() {
_index = index;
_read = read;
_create = create;
_modify = modify;
_update = update;
_remove = remove;
}
@override
2018-07-12 15:19:25 +00:00
index([Map params]) => new Future.sync(
() => _index != null ? _index(params) : super.index(params));
2017-02-01 21:43:18 +00:00
@override
2018-07-12 15:19:25 +00:00
read(id, [Map params]) => new Future.sync(
() => _read != null ? _read(id, params) : super.read(id, params));
2017-02-01 21:43:18 +00:00
@override
2018-07-12 15:19:25 +00:00
create(data, [Map params]) => new Future.sync(() =>
_create != null ? _create(data, params) : super.create(data, params));
2017-02-01 21:43:18 +00:00
@override
2018-07-12 15:19:25 +00:00
modify(id, data, [Map params]) => new Future.sync(() => _modify != null
2017-02-01 21:43:18 +00:00
? _modify(id, data, params)
2018-07-12 15:19:25 +00:00
: super.modify(id, data, params));
2017-02-01 21:43:18 +00:00
@override
2018-07-12 15:19:25 +00:00
update(id, data, [Map params]) => new Future.sync(() => _update != null
2017-02-01 21:43:18 +00:00
? _update(id, data, params)
2018-07-12 15:19:25 +00:00
: super.update(id, data, params));
2017-02-01 21:43:18 +00:00
@override
2018-07-12 15:19:25 +00:00
remove(id, [Map params]) => new Future.sync(
() => _remove != null ? _remove(id, params) : super.remove(id, params));
2017-02-01 21:43:18 +00:00
}