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/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 new file mode 100644 index 00000000..42ad8184 --- /dev/null +++ b/packages/signals_flutter/test/extension/untracked_value_test.dart @@ -0,0 +1,34 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:signals_flutter/extended.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); + }); + }); +}