Skip to content

Commit

Permalink
feat(hive): create HiveObjectMixin (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
themisir committed Mar 26, 2021
1 parent 266fb5e commit 8aad686
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion hive/lib/hive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'package:hive/src/object/hive_object.dart';
import 'package:hive/src/util/extensions.dart';
import 'package:meta/meta.dart';

export 'src/object/hive_object.dart' show HiveObject;
export 'src/object/hive_object.dart' show HiveObject, HiveObjectMixin;

part 'src/annotations/hive_field.dart';
part 'src/annotations/hive_type.dart';
Expand Down
4 changes: 2 additions & 2 deletions hive/lib/src/object/hive_collection.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of hive;

/// List containing [HiveObject]s.
abstract class HiveCollection<E extends HiveObject> implements List<E> {
/// List containing [HiveObjectMixin]s.
abstract class HiveCollection<E extends HiveObjectMixin> implements List<E> {
/// The box which contains all the objects in this collection
BoxBase get box;

Expand Down
2 changes: 1 addition & 1 deletion hive/lib/src/object/hive_collection_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:hive/hive.dart';

/// Implemetation of [HiveCollection].
abstract class HiveCollectionMixin<E extends HiveObject>
abstract class HiveCollectionMixin<E extends HiveObjectMixin>
implements HiveCollection<E> {
@override
Iterable<dynamic> get keys sync* {
Expand Down
6 changes: 3 additions & 3 deletions hive/lib/src/object/hive_list.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
part of hive;

/// Allows defining references to other [HiveObject]s.
/// Allows defining references to other [HiveObjectMixin]s.
@experimental
abstract class HiveList<E extends HiveObject> extends HiveCollection<E>
abstract class HiveList<E extends HiveObjectMixin> extends HiveCollection<E>
implements List<E> {
/// Create a new HiveList which can contain HiveObjects from [box].
factory HiveList(Box box, {List<E>? objects}) =>
Expand All @@ -13,5 +13,5 @@ abstract class HiveList<E extends HiveObject> extends HiveCollection<E>
void dispose();

/// Casts the list to a new HiveList.
HiveList<T> castHiveList<T extends HiveObject>();
HiveList<T> castHiveList<T extends HiveObjectMixin>();
}
4 changes: 2 additions & 2 deletions hive/lib/src/object/hive_list_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:hive/src/util/delegating_list_view_mixin.dart';
import 'package:meta/meta.dart';

/// Not part of public API
class HiveListImpl<E extends HiveObject>
class HiveListImpl<E extends HiveObjectMixin>
with HiveCollectionMixin<E>, ListMixin<E>, DelegatingListViewMixin<E>
implements HiveList<E> {
/// Not part of public API
Expand Down Expand Up @@ -159,7 +159,7 @@ class HiveListImpl<E extends HiveObject>
}

@override
HiveList<T> castHiveList<T extends HiveObject>() {
HiveList<T> castHiveList<T extends HiveObjectMixin>() {
if (_delegate != null) {
return HiveListImpl(box, objects: _delegate!.cast());
} else {
Expand Down
4 changes: 3 additions & 1 deletion hive/lib/src/object/hive_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ part 'hive_object_internal.dart';

/// Extend `HiveObject` to add useful methods to the objects you want to store
/// in Hive
abstract class HiveObject {
mixin HiveObjectMixin {
BoxBase? _box;

dynamic _key;
Expand Down Expand Up @@ -57,3 +57,5 @@ abstract class HiveObject {
return false;
}
}

abstract class HiveObject with HiveObjectMixin {}
2 changes: 1 addition & 1 deletion hive/lib/src/object/hive_object_internal.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of hive_object_internal;

/// Not part of public API
extension HiveObjectInternal on HiveObject {
extension HiveObjectInternal on HiveObjectMixin {
/// Not part of public API
@pragma('vm:prefer-inline')
@pragma('dart2js:tryInline')
Expand Down
41 changes: 22 additions & 19 deletions hive/test/tests/mocks.mocks.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import 'package:hive/src/box/default_compaction_strategy.dart';
import 'package:hive/src/box/default_key_comparator.dart';
import 'package:mockito/mockito.dart' as _i1;
import 'dart:async' as _i7;
import 'dart:collection' as _i2;
import 'package:hive/hive.dart' as _i3;
import 'package:hive/src/object/hive_object.dart' as _i4;
import 'dart:typed_data' as _i5;
import 'dart:convert' as _i15;
import 'dart:io' as _i6;
import 'dart:async' as _i7;
import 'package:hive/src/box/change_notifier.dart' as _i8;
import 'package:hive/src/binary/frame.dart' as _i9;
import 'dart:typed_data' as _i5;

import 'package:hive/hive.dart' as _i3;
import 'package:hive/src/backend/storage_backend.dart' as _i10;
import 'package:hive/src/binary/frame.dart' as _i9;
import 'package:hive/src/box/change_notifier.dart' as _i8;
import 'package:hive/src/box/default_compaction_strategy.dart';
import 'package:hive/src/box/default_key_comparator.dart';
import 'package:hive/src/box/keystore.dart' as _i11;
import 'package:hive/src/hive_impl.dart' as _i12;
import 'package:hive/src/registry/type_registry_impl.dart' as _i13;
import 'package:hive/src/object/hive_list_impl.dart' as _i14;
import 'dart:convert' as _i15;
import 'package:hive/src/io/frame_io_helper.dart' as _i16;
import 'package:hive/src/object/hive_list_impl.dart' as _i14;
import 'package:hive/src/object/hive_object.dart' as _i4;
import 'package:hive/src/registry/type_registry_impl.dart' as _i13;
import 'package:mockito/mockito.dart' as _i1;

class _FakeType extends _i1.Fake implements Type {}

Expand All @@ -27,7 +28,7 @@ class _FakeLazyBox<E> extends _i1.Fake implements _i3.LazyBox<E> {}

class _FakeBoxBase<E> extends _i1.Fake implements _i3.BoxBase<E> {}

class _FakeHiveList<E extends _i4.HiveObject> extends _i1.Fake
class _FakeHiveList<E extends _i4.HiveObjectMixin> extends _i1.Fake
implements _i3.HiveList<E> {}

class _FakeIterator<E> extends _i1.Fake implements Iterator<E> {}
Expand Down Expand Up @@ -162,9 +163,11 @@ class MockKeystore<E> extends _i1.Mock implements _i11.Keystore<E> {
_i7.Stream<_i3.BoxEvent> watch({dynamic key}) => (super.noSuchMethod(
Invocation.method(#watch, [], {#key: key}),
Stream<_i3.BoxEvent>.empty()) as _i7.Stream<_i3.BoxEvent>);
_i9.Frame? insert(_i9.Frame? frame, {bool? notify = true, bool lazy = false}) =>
(super.noSuchMethod(Invocation.method(#insert, [frame], {#notify: notify}))
as _i9.Frame?);
_i9.Frame? insert(_i9.Frame? frame,
{bool? notify = true, bool lazy = false}) =>
(super.noSuchMethod(
Invocation.method(#insert, [frame], {#notify: notify}))
as _i9.Frame?);
bool beginTransaction(List<_i9.Frame>? newFrames) => (super.noSuchMethod(
Invocation.method(#beginTransaction, [newFrames]), false) as bool);
int clear() => (super.noSuchMethod(Invocation.method(#clear, []), 0) as int);
Expand Down Expand Up @@ -277,7 +280,7 @@ class MockHiveList<E extends _i4.HiveObject> extends _i1.Mock
as _i3.BoxBase<dynamic>);
Iterable<dynamic> get keys =>
(super.noSuchMethod(Invocation.getter(#keys), []) as Iterable<dynamic>);
_i3.HiveList<T> castHiveList<T extends _i4.HiveObject>() =>
_i3.HiveList<T> castHiveList<T extends _i4.HiveObjectMixin>() =>
(super.noSuchMethod(
Invocation.method(#castHiveList, []), _FakeHiveList<T>())
as _i3.HiveList<T>);
Expand Down Expand Up @@ -336,7 +339,7 @@ class MockHiveListImpl<E extends _i4.HiveObject> extends _i1.Mock
super.noSuchMethod(Invocation.method(#add, [element]));
void addAll(Iterable<E>? iterable) =>
super.noSuchMethod(Invocation.method(#addAll, [iterable]));
_i3.HiveList<T> castHiveList<T extends _i4.HiveObject>() =>
_i3.HiveList<T> castHiveList<T extends _i4.HiveObjectMixin>() =>
(super.noSuchMethod(
Invocation.method(#castHiveList, []), _FakeHiveList<T>())
as _i3.HiveList<T>);
Expand Down Expand Up @@ -672,7 +675,7 @@ class MockBinaryWriter extends _i1.Mock implements _i3.BinaryWriter {
void writeMap(Map<dynamic, dynamic>? map, {bool? writeLength = true}) =>
super.noSuchMethod(
Invocation.method(#writeMap, [map], {#writeLength: writeLength}));
void writeHiveList(_i3.HiveList<_i4.HiveObject>? list,
void writeHiveList(_i3.HiveList<_i4.HiveObjectMixin>? list,
{bool? writeLength = true}) =>
super.noSuchMethod(Invocation.method(
#writeHiveList, [list], {#writeLength: writeLength}));
Expand Down

0 comments on commit 8aad686

Please sign in to comment.