cake_wallet/lib/entities/record.dart

37 lines
699 B
Dart
Raw Normal View History

2020-09-26 19:17:31 +00:00
import 'dart:async';
import 'package:cake_wallet/utils/mobx.dart';
import 'package:hive/hive.dart';
abstract class Record<T extends HiveObject> with Keyable {
Record(this._source, this.original) {
2020-09-28 15:47:43 +00:00
key = original.key;
2020-09-26 19:17:31 +00:00
_listener?.cancel();
_listener = _source.watch(key: original.key).listen((event) {
if (!event.deleted) {
fromBind(event.value as T);
}
});
fromBind(original);
toBind(original);
}
2020-09-28 15:47:43 +00:00
dynamic key;
2020-09-26 19:17:31 +00:00
@override
dynamic get keyIndex => key;
final T original;
final Box<T> _source;
StreamSubscription<BoxEvent> _listener;
void fromBind(T original);
void toBind(T original);
Future<void> save() => original.save();
}