Query & Mutation Builders now doesn't create a new instance

This commit is contained in:
Kingkor Roy Tirtho
2022-07-12 03:15:44 +06:00
parent 89272d5c00
commit 88841e71f8
10 changed files with 40 additions and 27 deletions
@@ -21,7 +21,6 @@ abstract class BaseOperation<Data, StatusType> extends ChangeNotifier {
int retryAttempts = 0;
DateTime updatedAt;
@protected
bool fetched = false;
/// used for keeping track of query activity. If the are no mounts &
+6
View File
@@ -168,4 +168,10 @@ class Mutation<T extends Object, V> extends BaseOperation<T, MutationStatus> {
bool get isLoading => status == MutationStatus.loading;
@override
bool get isSuccess => status == MutationStatus.success;
@override
bool operator ==(other) {
return (other is Mutation<T, V> && other.mutationKey == mutationKey) ||
identical(other, this);
}
}
@@ -51,7 +51,7 @@ class _MutationBuilderState<T extends Object, V>
void init([_]) {
queryBowl = QueryBowl.of(context);
mutation = queryBowl.addMutation<T, V>(
Mutation<T, V>.fromOptions(widget.job, queryBowl: queryBowl),
widget.job,
onData: widget.onData,
onError: widget.onError,
onMutate: widget.onMutate,
+6
View File
@@ -322,4 +322,10 @@ class Query<T extends Object, Outside> extends BaseOperation<T, QueryStatus> {
String toString() {
return debugLabel;
}
@override
bool operator ==(other) {
return (other is Query<T, Outside> && other.queryKey == queryKey) ||
identical(other, this);
}
}
+18 -7
View File
@@ -1,6 +1,7 @@
import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:fl_query/src/models/mutation_job.dart';
import 'package:fl_query/src/models/query_job.dart';
import 'package:fl_query/src/mutation.dart';
import 'package:fl_query/src/mutation_builder.dart';
@@ -334,23 +335,24 @@ class QueryBowl extends InheritedWidget {
/// !⚠️**Warning** only for internal library usage
@protected
Query<T, Outside> addQuery<T extends Object, Outside>(
Query<T, Outside> query, {
QueryJob<T, Outside> queryJob, {
required Outside externalData,
required ValueKey<String> key,
final QueryListener<T>? onData,
final QueryListener<dynamic>? onError,
}) {
final prevQuery =
_queries.firstWhereOrNull((q) => q.queryKey == query.queryKey);
_queries.firstWhereOrNull((q) => q.queryKey == queryJob.queryKey);
if (prevQuery is Query<T, Outside>) {
// run the query if its still not called or if externalData has
// changed
if (prevQuery.prevUsedExternalData != null &&
query.externalData != null &&
externalData != null &&
!isShallowEqual(
prevQuery.prevUsedExternalData!,
query.externalData!,
externalData,
)) {
prevQuery.setExternalData(query.externalData);
prevQuery.setExternalData(externalData);
}
prevQuery.mount(key);
if (onData != null) prevQuery.onDataListeners.add(onData);
@@ -358,6 +360,11 @@ class QueryBowl extends InheritedWidget {
// mounting the widget that is using the query in the prevQuery
return prevQuery;
}
final query = Query<T, Outside>.fromOptions(
queryJob,
externalData: externalData,
queryBowl: this,
);
if (onData != null) query.onDataListeners.add(onData);
if (onError != null) query.onErrorListeners.add(onError);
query.updateDefaultOptions(
@@ -375,14 +382,14 @@ class QueryBowl extends InheritedWidget {
/// !⚠️**Warning** only for internal library usage
@protected
Mutation<T, V> addMutation<T extends Object, V>(
Mutation<T, V> mutation, {
MutationJob<T, V> mutationJob, {
final MutationListener<T>? onData,
final MutationListener<dynamic>? onError,
final MutationListener<V>? onMutate,
required ValueKey<String> key,
}) {
final prevMutation = _mutations.firstWhereOrNull(
(prevMutation) => prevMutation.mutationKey == mutation.mutationKey);
(prevMutation) => prevMutation.mutationKey == mutationJob.mutationKey);
if (prevMutation != null && prevMutation is Mutation<T, V>) {
if (onData != null) prevMutation.onDataListeners.add(onData);
if (onError != null) prevMutation.onErrorListeners.add(onError);
@@ -390,6 +397,10 @@ class QueryBowl extends InheritedWidget {
prevMutation.mount(key);
return prevMutation;
} else {
final mutation = Mutation<T, V>.fromOptions(
mutationJob,
queryBowl: this,
);
if (onData != null) mutation.onDataListeners.add(onData);
if (onError != null) mutation.onErrorListeners.add(onError);
if (onMutate != null) mutation.onMutateListeners.add(onMutate);
+2 -5
View File
@@ -46,11 +46,8 @@ class _QueryBuilderState<T extends Object, Outside>
void init([QueryBowl? bowl]) async {
bowl ??= QueryBowl.of(context);
query = bowl.addQuery<T, Outside>(
Query<T, Outside>.fromOptions(
widget.job,
externalData: widget.externalData,
queryBowl: QueryBowl.of(context),
),
widget.job,
externalData: widget.externalData,
key: uKey,
onData: widget.onData,
onError: widget.onError,