Skip to content

Commit

Permalink
fix: Fixed a bug that prevented ScopedQuery from detecting Ref updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Jul 14, 2023
1 parent 2620119 commit a7f9b88
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/katana_scoped/lib/value/cache.dart
Expand Up @@ -104,7 +104,7 @@ class _CacheValueState<T> extends ScopedValueState<T, _CacheValue<T>> {
_value = value.callback();
final ref = value.ref;
if (ref is ListenableRef) {
ref.addListener(_handledOnUpdateRef);
ref.addListener(_handledOnUpdateByRef);
}
}

Expand All @@ -116,7 +116,7 @@ class _CacheValueState<T> extends ScopedValueState<T, _CacheValue<T>> {
}
}

void _handledOnUpdateRef() {
void _handledOnUpdateByRef() {
_value = value.callback();
setState(() {});
}
Expand All @@ -126,7 +126,7 @@ class _CacheValueState<T> extends ScopedValueState<T, _CacheValue<T>> {
super.dispose();
final ref = value.ref;
if (ref is ListenableRef) {
ref.removeListener(_handledOnUpdateRef);
ref.removeListener(_handledOnUpdateByRef);
}
}

Expand Down
21 changes: 21 additions & 0 deletions packages/katana_scoped/lib/value/query.dart
Expand Up @@ -36,6 +36,8 @@ extension RefQueryExtensions on Ref {
return getScopedValue<T, _QueryValue<T>>(
(ref) => _QueryValue<T>(
callback: query.call(ref),
ref: ref,
listen: query.listen,
autoDisposeWhenUnreferenced: query.autoDisposeWhenUnreferenced,
),
listen: query.listen,
Expand Down Expand Up @@ -85,10 +87,14 @@ extension RefHasAppQueryExtensions on RefHasApp {
class _QueryValue<T> extends ScopedValue<T> {
const _QueryValue({
required this.callback,
required this.ref,
this.listen = false,
this.autoDisposeWhenUnreferenced = false,
});

final T Function() callback;
final bool listen;
final Ref ref;
final bool autoDisposeWhenUnreferenced;

@override
Expand All @@ -111,12 +117,23 @@ class _QueryValueState<T> extends ScopedValueState<T, _QueryValue<T>> {
if (val is Listenable) {
val.addListener(_handledOnUpdate);
}
if (!value.listen) {
final ref = value.ref;
if (ref is ListenableRef) {
ref.addListener(_handledOnUpdateByRef);
}
}
}

void _handledOnUpdate() {
setState(() {});
}

void _handledOnUpdateByRef() {
_value = value.callback.call();
setState(() {});
}

@override
void dispose() {
super.dispose();
Expand All @@ -127,6 +144,10 @@ class _QueryValueState<T> extends ScopedValueState<T, _QueryValue<T>> {
val.dispose();
}
}
final ref = value.ref;
if (ref is ListenableRef) {
ref.removeListener(_handledOnUpdateByRef);
}
}

@override
Expand Down

0 comments on commit a7f9b88

Please sign in to comment.