Skip to content

Commit

Permalink
feat: Add notifyDocumentChanges to the query.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Mar 29, 2024
1 parent 98a06d9 commit ee34caa
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/katana_model/lib/src/collection_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ abstract class CollectionBase<TModel extends DocumentBase>
}
if (val != found.value) {
found.notifyListeners();
if (modelQuery.filters.any(
(e) => e.type == ModelQueryFilterType.notifyDocumentChanges)) {
notify = true;
}
}
if (update.newIndex != update.oldIndex) {
notify = true;
Expand Down
32 changes: 30 additions & 2 deletions packages/katana_model/lib/src/model_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class CollectionModelQuery extends ModelQuery {
///
/// Execute [create] to create a [DocumentModelQuery] with the specified ID.
///
/// The following elements can be specified by chaining [equal], [notEqual], [lessThanOrEqual], [greaterThanOrEqual], [contains], [containsAny], [where], [notWhere], [isNull], [isNotNull], [like ], [geo], [orderByAsc], [orderByDesc], and [limitTo] can be specified in a chain to filter elements.
/// The following elements can be specified by chaining [equal], [notEqual], [lessThanOrEqual], [greaterThanOrEqual], [contains], [containsAny], [where], [notWhere], [isNull], [isNotNull], [like ], [geo], [orderByAsc], [orderByDesc], [notifyDocumentChanges]and [limitTo] can be specified in a chain to filter elements.
///
/// Modelを定義するためのクエリクラス。
///
Expand All @@ -149,7 +149,7 @@ class CollectionModelQuery extends ModelQuery {
///
/// [create]を実行すると指定したIDを持つ[DocumentModelQuery]を作成することができます。
///
/// [equal][notEqual][lessThanOrEqual][greaterThanOrEqual][contains][containsAny][where][notWhere][isNull][isNotNull][like][geo][orderByAsc][orderByDesc][limitTo]をチェインして指定していくことにより要素のフィルタリングが可能です。
/// [equal][notEqual][lessThanOrEqual][greaterThanOrEqual][contains][containsAny][where][notWhere][isNull][isNotNull][like][geo][orderByAsc][orderByDesc][limitTo][notifyDocumentChanges]をチェインして指定していくことにより要素のフィルタリングが可能です。
const CollectionModelQuery(
super.path, {
super.adapter,
Expand Down Expand Up @@ -479,6 +479,19 @@ class CollectionModelQuery extends ModelQuery {
]);
}

/// Ensure that the collection is notified of changes to internal documents.
///
/// 内部のドキュメントの変更をコレクションに通知するようにします。
CollectionModelQuery notifyDocumentChanges() {
return _copyWithAddingFilter(filters: [
...filters
.where((e) => e.type != ModelQueryFilterType.notifyDocumentChanges),
const ModelQueryFilter._(
type: ModelQueryFilterType.notifyDocumentChanges,
)
]);
}

/// Reset all [filters].
///
/// すべての[filters]をリセットします。
Expand Down Expand Up @@ -956,6 +969,11 @@ enum ModelQueryFilterType {
///
/// データの数に制限をかけるフィルター。
limit,

/// Filters to notify the collection of changes in internal documents.
///
/// 内部のドキュメントの変更をコレクションに通知するためのフィルター。
notifyDocumentChanges;
}

/// {@template model_query_filter}
Expand Down Expand Up @@ -1237,6 +1255,16 @@ class ModelQueryFilter {
value: value,
);

/// Ensure that the collection is notified of changes to internal documents.
///
/// 内部のドキュメントの変更をコレクションに通知するようにします。
///
/// {@macro model_query_filter}
const ModelQueryFilter.notifyDocumentChanges()
: this._(
type: ModelQueryFilterType.notifyDocumentChanges,
);

static const String _kTypeKey = "type";
static const String _kKeyKey = "key";
static const String _kValueKey = "value";
Expand Down
7 changes: 7 additions & 0 deletions packages/masamune/lib/model/model_query_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ abstract class ModelQuerySelector<T, TQuery extends ModelQueryBase> {
return _toQuery(_modelQuery.limitTo(value));
}

/// Ensure that the collection is notified of changes to internal documents.
///
/// 内部のドキュメントの変更をコレクションに通知するようにします。
TQuery notifyDocumentChanges() {
return _toQuery(_modelQuery.notifyDocumentChanges());
}

/// Reset all conditions.
///
/// すべての条件をリセットします。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ enum CollectionQueryType {
/// `limitTo`のメソッド。
limitTo,

/// The method for `notifyDocumentChanges`.
///
/// `notifyDocumentChanges`のメソッド。
notifyDocumentChanges,

/// The method of `reset`.
///
/// `reset`のメソッド。
Expand Down Expand Up @@ -177,6 +182,7 @@ enum CollectionQueryType {
),
];
case CollectionQueryType.reset:
case CollectionQueryType.notifyDocumentChanges:
return [];
}
}
Expand Down Expand Up @@ -208,6 +214,7 @@ enum CollectionQueryType {
case CollectionQueryType.limitTo:
return "$name(value)";
case CollectionQueryType.reset:
case CollectionQueryType.notifyDocumentChanges:
return "$name()";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,14 @@ List<Spec> collectionModelQueryClass(
..body =
Code("_\$_${model.name}CollectionQuery(modelQuery.reset())"),
),
Method(
(m) => m
..name = "notifyDocumentChanges"
..lambda = true
..returns = Reference("_\$_${model.name}CollectionQuery")
..body = Code(
"_\$_${model.name}CollectionQuery(modelQuery.notifyDocumentChanges())"),
),
Method(
(m) => m
..name = "uid"
Expand Down Expand Up @@ -924,6 +932,14 @@ List<Spec> collectionModelQueryClass(
..body = Code(
"_\$_${model.name}MirrorCollectionQuery(modelQuery.limitTo(value))"),
),
Method(
(m) => m
..name = "notifyDocumentChanges"
..lambda = true
..returns = Reference("_\$_${model.name}MirrorCollectionQuery")
..body = Code(
"_\$_${model.name}MirrorCollectionQuery(modelQuery.notifyDocumentChanges())"),
),
Method(
(m) => m
..name = "reset"
Expand Down

0 comments on commit ee34caa

Please sign in to comment.