Skip to content

Commit

Permalink
Query & Mutation Builders now doesn't create a new instance
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Jul 11, 2022
1 parent 89272d5 commit 88841e7
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode/dryrun.log
.vscode/configurationCache.log
1 change: 0 additions & 1 deletion packages/fl_query/lib/src/base_operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down
6 changes: 6 additions & 0 deletions packages/fl_query/lib/src/mutation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion packages/fl_query/lib/src/mutation_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions packages/fl_query/lib/src/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
25 changes: 18 additions & 7 deletions packages/fl_query/lib/src/query_bowl.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -334,30 +335,36 @@ 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);
if (onError != null) prevQuery.onErrorListeners.add(onError);
// 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(
Expand All @@ -375,21 +382,25 @@ 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);
if (onMutate != null) prevMutation.onMutateListeners.add(onMutate);
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);
Expand Down
7 changes: 2 additions & 5 deletions packages/fl_query/lib/src/query_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/fl_query_hooks/.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.5/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.5/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_linux-1.3.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus_web","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.2/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":["connectivity_plus_linux","connectivity_plus_macos","connectivity_plus_web","connectivity_plus_windows"]},{"name":"connectivity_plus_linux","dependencies":[]},{"name":"connectivity_plus_macos","dependencies":[]},{"name":"connectivity_plus_web","dependencies":[]},{"name":"connectivity_plus_windows","dependencies":[]}],"date_created":"2022-07-08 04:37:56.711440","version":"3.0.1"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.5/","native_build":true,"dependencies":[]}],"android":[{"name":"connectivity_plus","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus-2.3.5/","native_build":true,"dependencies":[]}],"macos":[{"name":"connectivity_plus_macos","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_macos-1.2.4/","native_build":true,"dependencies":[]}],"linux":[{"name":"connectivity_plus_linux","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_linux-1.3.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"connectivity_plus_windows","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_windows-1.2.2/","native_build":true,"dependencies":[]}],"web":[{"name":"connectivity_plus_web","path":"/home/krtirtho/.pub-cache/hosted/pub.dartlang.org/connectivity_plus_web-1.2.2/","dependencies":[]}]},"dependencyGraph":[{"name":"connectivity_plus","dependencies":["connectivity_plus_linux","connectivity_plus_macos","connectivity_plus_web","connectivity_plus_windows"]},{"name":"connectivity_plus_linux","dependencies":[]},{"name":"connectivity_plus_macos","dependencies":[]},{"name":"connectivity_plus_web","dependencies":[]},{"name":"connectivity_plus_windows","dependencies":[]}],"date_created":"2022-07-12 03:10:11.860962","version":"3.0.1"}
6 changes: 1 addition & 5 deletions packages/fl_query_hooks/lib/src/use_mutation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Mutation<T, V> useMutation<T extends Object, V>({

final init = useCallback(() {
mutation.value = queryBowl.addMutation<T, V>(
mutation.value,
job,
onData: onData,
onError: onError,
onMutate: onMutate,
Expand Down Expand Up @@ -55,10 +55,6 @@ Mutation<T, V> useMutation<T extends Object, V>({
useEffect(() {
if (oldJob != null && oldJob.mutationKey != job.mutationKey) {
disposeMutation();
mutation.value = Mutation<T, V>.fromOptions(
job,
queryBowl: queryBowl,
);
init();
} else {
if (oldOnData != onData && oldOnData != null) {
Expand Down
10 changes: 3 additions & 7 deletions packages/fl_query_hooks/lib/src/use_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Query<T, Outside> useQuery<T extends Object, Outside>({

final init = useCallback(() {
query.value = queryBowl.addQuery<T, Outside>(
query.value,
job,
externalData: externalData,
key: uKey,
onData: onData,
onError: onError,
Expand All @@ -46,7 +47,7 @@ Query<T, Outside> useQuery<T extends Object, Outside>({
hasExternalDataChanged
? query.value.refetch()
: query.value.fetch();
}, [queryBowl, query.value, uKey, onData, onError, job]);
}, [queryBowl, query.value, uKey, onData, onError, job, externalData]);

final disposeQuery = useCallback(() {
query.value.unmount(uKey);
Expand All @@ -64,11 +65,6 @@ Query<T, Outside> useQuery<T extends Object, Outside>({
final hasOnDataChanged = oldOnData != onData && oldOnData != null;
if (oldJob != null && oldJob.queryKey != job.queryKey) {
disposeQuery();
query.value = Query.fromOptions(
job,
externalData: externalData,
queryBowl: queryBowl,
);
init();
} else if (oldExternalData != null &&
externalData != null &&
Expand Down

0 comments on commit 88841e7

Please sign in to comment.