From 9523062a216a18bfaf91ce365dac73514d666de6 Mon Sep 17 00:00:00 2001 From: Nabila Irfandi Date: Thu, 21 Nov 2024 17:45:11 +0100 Subject: [PATCH 1/4] added untrackedValue --- .../signals_core/lib/src/core/signal.dart | 15 ++++++++++ .../signals_core/lib/src/core/wrappers.dart | 3 ++ .../test/core/untracked_test.dart | 28 +++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/packages/signals_core/lib/src/core/signal.dart b/packages/signals_core/lib/src/core/signal.dart index 3a3da799..0dfa7a21 100644 --- a/packages/signals_core/lib/src/core/signal.dart +++ b/packages/signals_core/lib/src/core/signal.dart @@ -548,6 +548,21 @@ class Signal extends ReadonlySignal { return _value; } + /// Run a callback function that returns the signal value without + /// subscribing to the signal updates. + T get untrackedValue { + if (_lazy) { + throw LazySignalInitializationError(this); + } + if (disposed) { + if (kDebugMode) { + print( + 'signal warning: [$globalId|$debugLabel] has been read after disposed: ${StackTrace.current}'); + } + } + return untracked(() => _value); + } + /// Returns a readonly signal ReadonlySignal readonly() => this; diff --git a/packages/signals_core/lib/src/core/wrappers.dart b/packages/signals_core/lib/src/core/wrappers.dart index 5816cef3..3047d371 100644 --- a/packages/signals_core/lib/src/core/wrappers.dart +++ b/packages/signals_core/lib/src/core/wrappers.dart @@ -100,4 +100,7 @@ class WrappedSignal extends WrappedReadonlySignal> @override set value(T val) => source.value = val; + + @override + get untrackedValue => untracked(() => source.value); } diff --git a/packages/signals_core/test/core/untracked_test.dart b/packages/signals_core/test/core/untracked_test.dart index d093d11b..ab7900cd 100644 --- a/packages/signals_core/test/core/untracked_test.dart +++ b/packages/signals_core/test/core/untracked_test.dart @@ -30,4 +30,32 @@ void main() { expect(calls, 1); }); }); + + group('untrackedValue', () { + test('single', () { + final s = signal(0); + int calls = 0; + final dis = effect(() { + s.value; + s.untrackedValue + 1; + calls++; + }); + dis(); + + expect(calls, 1); + }); + + test('nested', () { + final s = signal(0); + int calls = 0; + final dis = effect(() { + s.value; + untracked(() => s.untrackedValue + 1); + calls++; + }); + dis(); + + expect(calls, 1); + }); + }); } From 36c552b8d973303d896da70723c0aa521a24bf5d Mon Sep 17 00:00:00 2001 From: Nabila Irfandi Date: Thu, 21 Nov 2024 19:27:30 +0100 Subject: [PATCH 2/4] Revert "added untrackedValue" This reverts commit 9523062a216a18bfaf91ce365dac73514d666de6. --- .../signals_core/lib/src/core/signal.dart | 15 ---------- .../signals_core/lib/src/core/wrappers.dart | 3 -- .../test/core/untracked_test.dart | 28 ------------------- 3 files changed, 46 deletions(-) diff --git a/packages/signals_core/lib/src/core/signal.dart b/packages/signals_core/lib/src/core/signal.dart index 0dfa7a21..3a3da799 100644 --- a/packages/signals_core/lib/src/core/signal.dart +++ b/packages/signals_core/lib/src/core/signal.dart @@ -548,21 +548,6 @@ class Signal extends ReadonlySignal { return _value; } - /// Run a callback function that returns the signal value without - /// subscribing to the signal updates. - T get untrackedValue { - if (_lazy) { - throw LazySignalInitializationError(this); - } - if (disposed) { - if (kDebugMode) { - print( - 'signal warning: [$globalId|$debugLabel] has been read after disposed: ${StackTrace.current}'); - } - } - return untracked(() => _value); - } - /// Returns a readonly signal ReadonlySignal readonly() => this; diff --git a/packages/signals_core/lib/src/core/wrappers.dart b/packages/signals_core/lib/src/core/wrappers.dart index 3047d371..5816cef3 100644 --- a/packages/signals_core/lib/src/core/wrappers.dart +++ b/packages/signals_core/lib/src/core/wrappers.dart @@ -100,7 +100,4 @@ class WrappedSignal extends WrappedReadonlySignal> @override set value(T val) => source.value = val; - - @override - get untrackedValue => untracked(() => source.value); } diff --git a/packages/signals_core/test/core/untracked_test.dart b/packages/signals_core/test/core/untracked_test.dart index ab7900cd..d093d11b 100644 --- a/packages/signals_core/test/core/untracked_test.dart +++ b/packages/signals_core/test/core/untracked_test.dart @@ -30,32 +30,4 @@ void main() { expect(calls, 1); }); }); - - group('untrackedValue', () { - test('single', () { - final s = signal(0); - int calls = 0; - final dis = effect(() { - s.value; - s.untrackedValue + 1; - calls++; - }); - dis(); - - expect(calls, 1); - }); - - test('nested', () { - final s = signal(0); - int calls = 0; - final dis = effect(() { - s.value; - untracked(() => s.untrackedValue + 1); - calls++; - }); - dis(); - - expect(calls, 1); - }); - }); } From f94017ff0a526f18e358278ad1724ee96d7d4840 Mon Sep 17 00:00:00 2001 From: Nabila Irfandi Date: Thu, 21 Nov 2024 19:41:32 +0100 Subject: [PATCH 3/4] added .untrackedValue as an extension --- .../lib/src/extensions/signal.dart | 9 +++++ .../test/extension/untracked_value_test.dart | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 packages/signals_flutter/test/extension/untracked_value_test.dart diff --git a/packages/signals_flutter/lib/src/extensions/signal.dart b/packages/signals_flutter/lib/src/extensions/signal.dart index 2ae03e0c..0cc923cc 100644 --- a/packages/signals_flutter/lib/src/extensions/signal.dart +++ b/packages/signals_flutter/lib/src/extensions/signal.dart @@ -70,6 +70,15 @@ extension FlutterReadonlySignalUtils on ReadonlySignal { return SignalValueListenable, ReadonlySignal>.fromSignal(this); } + + /// Runs a callback function that returns the signal value without + /// subscribing to the signal updates. + /// + /// ```dart + /// final counter = signal(0); + /// final untrackedValue = counter.untrackedValue; + /// ``` + T get untrackedValue => untracked(() => value); } /// Mutable signal utils diff --git a/packages/signals_flutter/test/extension/untracked_value_test.dart b/packages/signals_flutter/test/extension/untracked_value_test.dart new file mode 100644 index 00000000..67d62faf --- /dev/null +++ b/packages/signals_flutter/test/extension/untracked_value_test.dart @@ -0,0 +1,33 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:signals_flutter/signals_flutter.dart'; + +void main() { + SignalsObserver.instance = null; + group('untrackedValue', () { + test('single', () { + final s = signal(0); + int calls = 0; + final dis = effect(() { + s.value; + s.untrackedValue + 1; + calls++; + }); + dis(); + + expect(calls, 1); + }); + + test('nested', () { + final s = signal(0); + int calls = 0; + final dis = effect(() { + s.value; + untracked(() => s.untrackedValue + 1); + calls++; + }); + dis(); + + expect(calls, 1); + }); + }); +} From c7045bff434c077d6eca2a687e6ba3c0c19a9f12 Mon Sep 17 00:00:00 2001 From: Nabila Irfandi Date: Thu, 21 Nov 2024 21:38:31 +0100 Subject: [PATCH 4/4] isolate the .untrackedValue extension --- packages/signals_flutter/lib/extended.dart | 3 ++- .../signals_flutter/lib/src/extensions/signal.dart | 9 --------- .../lib/src/extensions/untracked_value.dart | 13 +++++++++++++ .../test/extension/untracked_value_test.dart | 1 + 4 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 packages/signals_flutter/lib/src/extensions/untracked_value.dart diff --git a/packages/signals_flutter/lib/extended.dart b/packages/signals_flutter/lib/extended.dart index 265890f9..03df2828 100644 --- a/packages/signals_flutter/lib/extended.dart +++ b/packages/signals_flutter/lib/extended.dart @@ -8,4 +8,5 @@ export 'src/flutter/scheduler_binding.dart'; export 'src/flutter/inherited_widget/signal.dart'; export 'src/flutter/inherited_widget/computed.dart'; export 'src/flutter/inherited_widget/readonly.dart'; -export 'src/flutter/inherited_widget/provider.dart'; \ No newline at end of file +export 'src/flutter/inherited_widget/provider.dart'; +export 'src/extensions/untracked_value.dart'; diff --git a/packages/signals_flutter/lib/src/extensions/signal.dart b/packages/signals_flutter/lib/src/extensions/signal.dart index 0cc923cc..2ae03e0c 100644 --- a/packages/signals_flutter/lib/src/extensions/signal.dart +++ b/packages/signals_flutter/lib/src/extensions/signal.dart @@ -70,15 +70,6 @@ extension FlutterReadonlySignalUtils on ReadonlySignal { return SignalValueListenable, ReadonlySignal>.fromSignal(this); } - - /// Runs a callback function that returns the signal value without - /// subscribing to the signal updates. - /// - /// ```dart - /// final counter = signal(0); - /// final untrackedValue = counter.untrackedValue; - /// ``` - T get untrackedValue => untracked(() => value); } /// Mutable signal utils diff --git a/packages/signals_flutter/lib/src/extensions/untracked_value.dart b/packages/signals_flutter/lib/src/extensions/untracked_value.dart new file mode 100644 index 00000000..84eecd97 --- /dev/null +++ b/packages/signals_flutter/lib/src/extensions/untracked_value.dart @@ -0,0 +1,13 @@ +import 'package:signals_core/signals_core.dart'; + +/// Signal extensions +extension ReadonlySignalUntrackedValueUtils on ReadonlySignal { + /// Runs a callback function that returns the signal value without + /// subscribing to the signal updates. + /// + /// ```dart + /// final counter = signal(0); + /// final untrackedValue = counter.untrackedValue; + /// ``` + T get untrackedValue => untracked(() => value); +} diff --git a/packages/signals_flutter/test/extension/untracked_value_test.dart b/packages/signals_flutter/test/extension/untracked_value_test.dart index 67d62faf..42ad8184 100644 --- a/packages/signals_flutter/test/extension/untracked_value_test.dart +++ b/packages/signals_flutter/test/extension/untracked_value_test.dart @@ -1,4 +1,5 @@ import 'package:flutter_test/flutter_test.dart'; +import 'package:signals_flutter/extended.dart'; import 'package:signals_flutter/signals_flutter.dart'; void main() {