From 4810760ad16906b6b6a64438005b137fce337f6e Mon Sep 17 00:00:00 2001 From: Remi Rousselet Date: Wed, 28 Feb 2024 21:11:18 +0100 Subject: [PATCH] Rename generics --- .../lib/src/core/consumer.dart | 3 +- .../lib/src/core/widget_ref.dart | 3 + .../riverpod/lib/src/core/async_value.dart | 118 +++++++++--------- packages/riverpod/lib/src/core/element.dart | 38 +++--- .../riverpod/lib/src/core/foundation.dart | 12 +- .../lib/src/core/modifiers/select.dart | 42 +++---- .../lib/src/core/modifiers/select_async.dart | 28 ++--- .../lib/src/core/override_with_value.dart | 12 +- .../lib/src/core/provider/provider.dart | 5 +- .../lib/src/core/provider_subscription.dart | 26 ++-- .../src/core/proxy_provider_listenable.dart | 26 ++-- packages/riverpod/lib/src/core/ref.dart | 8 +- .../src/providers/async_notifier/family.dart | 9 +- .../src/providers/async_notifier/orphan.dart | 6 +- .../providers/legacy/state_controller.dart | 8 +- .../src/providers/legacy/state_provider.dart | 4 +- .../lib/src/providers/notifier/orphan.dart | 6 +- .../src/providers/stream_notifier/family.dart | 9 +- .../src/providers/stream_notifier/orphan.dart | 6 +- packages/riverpod/test/meta_test.dart | 1 - 20 files changed, 188 insertions(+), 182 deletions(-) diff --git a/packages/flutter_riverpod/lib/src/core/consumer.dart b/packages/flutter_riverpod/lib/src/core/consumer.dart index 4740b95fd..fc5b107cc 100644 --- a/packages/flutter_riverpod/lib/src/core/consumer.dart +++ b/packages/flutter_riverpod/lib/src/core/consumer.dart @@ -340,8 +340,7 @@ abstract class ConsumerStatefulWidget extends StatefulWidget { /// ``` abstract class ConsumerState extends State { - // TODO change this doc - /// An object that allows widgets to interact with providers. + /// {@macro flutter_riverpod.widget_ref} late final WidgetRef ref = context as WidgetRef; } diff --git a/packages/flutter_riverpod/lib/src/core/widget_ref.dart b/packages/flutter_riverpod/lib/src/core/widget_ref.dart index a0c6b7079..b46f9b45e 100644 --- a/packages/flutter_riverpod/lib/src/core/widget_ref.dart +++ b/packages/flutter_riverpod/lib/src/core/widget_ref.dart @@ -1,6 +1,9 @@ part of '../core.dart'; +// TODO change this doc +/// {@template flutter_riverpod.widget_ref} /// An object that allows widgets to interact with providers. +/// {@endtemplate} abstract class WidgetRef { /// The [BuildContext] of the widget associated to this [WidgetRef]. /// diff --git a/packages/riverpod/lib/src/core/async_value.dart b/packages/riverpod/lib/src/core/async_value.dart index bee1a0e2e..82530a552 100644 --- a/packages/riverpod/lib/src/core/async_value.dart +++ b/packages/riverpod/lib/src/core/async_value.dart @@ -8,14 +8,14 @@ import '../providers/stream_provider.dart' show StreamProvider; /// An extension for [asyncTransition]. @internal -extension AsyncTransition on ProviderElementBase> { +extension AsyncTransition on ProviderElementBase> { /// Internal utility for transitioning an [AsyncValue] after a provider refresh. /// /// [seamless] controls how the previous state is preserved: /// - seamless:true => import previous state and skip loading /// - seamless:false => import previous state and prefer loading void asyncTransition( - AsyncValue newState, { + AsyncValue newState, { required bool seamless, }) { // ignore: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member @@ -28,7 +28,9 @@ extension AsyncTransition on ProviderElementBase> { // ignore: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member setStateResult( ResultData( - newState._cast().copyWithPrevious(previous, isRefresh: seamless), + newState + ._cast() + .copyWithPrevious(previous, isRefresh: seamless), ), ); } @@ -86,14 +88,14 @@ extension AsyncTransition on ProviderElementBase> { /// - [AsyncValue.guard], to simplify transforming a [Future] into an [AsyncValue]. @sealed @immutable -sealed class AsyncValue { +sealed class AsyncValue { const AsyncValue._(); /// {@template async_value.data} /// Creates an [AsyncValue] with a data. /// {@endtemplate} // coverage:ignore-start - const factory AsyncValue.data(T value) = AsyncData; + const factory AsyncValue.data(StateT value) = AsyncData; // coverage:ignore-end /// {@template async_value.loading} @@ -102,7 +104,7 @@ sealed class AsyncValue { /// Prefer always using this constructor with the `const` keyword. /// {@endtemplate} // coverage:ignore-start - const factory AsyncValue.loading() = AsyncLoading; + const factory AsyncValue.loading() = AsyncLoading; // coverage:ignore-end /// {@template async_value.error_ctor} @@ -117,7 +119,7 @@ sealed class AsyncValue { /// {@endtemplate} // coverage:ignore-start const factory AsyncValue.error(Object error, StackTrace stackTrace) = - AsyncError; + AsyncError; // coverage:ignore-end /// Transforms a [Future] that may fail into something that is safe to read. @@ -212,7 +214,7 @@ sealed class AsyncValue { /// ```dart /// ref.watch(provider).unwrapPrevious().value; /// ``` - T? get value; + StateT? get value; /// The [error]. Object? get error; @@ -227,8 +229,8 @@ sealed class AsyncValue { /// Finally if in loading state, throws a [StateError]. /// /// This is typically used for when the UI assumes that [value] is always present. - T get requireValue { - if (hasValue) return value as T; + StateT get requireValue { + if (hasValue) return value as StateT; if (hasError) { throwErrorWithCombinedStackTrace(error!, stackTrace!); } @@ -273,7 +275,7 @@ sealed class AsyncValue { /// /// Note that an [AsyncData] may still be in loading/error state, such /// as during a pull-to-refresh. - AsyncData? get asData { + AsyncData? get asData { return map( data: (d) => d, error: (e) => null, @@ -286,7 +288,7 @@ sealed class AsyncValue { /// /// Note that an [AsyncError] may still be in loading state, such /// as during a pull-to-refresh. - AsyncError? get asError => map( + AsyncError? get asError => map( data: (_) => null, error: (e) => e, loading: (_) => null, @@ -297,9 +299,9 @@ sealed class AsyncValue { /// This allows reading the content of an [AsyncValue] in a type-safe way, /// without potentially ignoring to handle a case. R map({ - required R Function(AsyncData data) data, - required R Function(AsyncError error) error, - required R Function(AsyncLoading loading) loading, + required R Function(AsyncData data) data, + required R Function(AsyncError error) error, + required R Function(AsyncLoading loading) loading, }); /// Casts the [AsyncValue] to a different type. @@ -309,7 +311,7 @@ sealed class AsyncValue { /// /// For loading/error cases, creates a new [AsyncValue] with the corresponding /// generic type while preserving the error/stacktrace. - AsyncValue whenData(R Function(T value) cb) { + AsyncValue whenData(R Function(StateT value) cb) { return map( data: (d) { try { @@ -350,7 +352,7 @@ sealed class AsyncValue { bool skipLoadingOnReload = false, bool skipLoadingOnRefresh = true, bool skipError = false, - R Function(T data)? data, + R Function(StateT data)? data, R Function(Object error, StackTrace stackTrace)? error, R Function()? loading, required R Function() orElse, @@ -396,7 +398,7 @@ sealed class AsyncValue { bool skipLoadingOnReload = false, bool skipLoadingOnRefresh = true, bool skipError = false, - required R Function(T data) data, + required R Function(StateT data) data, required R Function(Object error, StackTrace stackTrace) error, required R Function() loading, }) { @@ -429,7 +431,7 @@ sealed class AsyncValue { bool skipLoadingOnReload = false, bool skipLoadingOnRefresh = true, bool skipError = false, - R? Function(T data)? data, + R? Function(StateT data)? data, R? Function(Object error, StackTrace stackTrace)? error, R? Function()? loading, }) { @@ -446,9 +448,9 @@ sealed class AsyncValue { /// Perform some actions based on the state of the [AsyncValue], or call orElse /// if the current state was not tested. R maybeMap({ - R Function(AsyncData data)? data, - R Function(AsyncError error)? error, - R Function(AsyncLoading loading)? loading, + R Function(AsyncData data)? data, + R Function(AsyncError error)? error, + R Function(AsyncLoading loading)? loading, required R Function() orElse, }) { return map( @@ -470,9 +472,9 @@ sealed class AsyncValue { /// Perform some actions based on the state of the [AsyncValue], or return null /// if the current state wasn't tested. R? mapOrNull({ - R? Function(AsyncData data)? data, - R? Function(AsyncError error)? error, - R? Function(AsyncLoading loading)? loading, + R? Function(AsyncData data)? data, + R? Function(AsyncError error)? error, + R? Function(AsyncLoading loading)? loading, }) { return map( data: (d) => data?.call(d), @@ -493,24 +495,24 @@ sealed class AsyncValue { /// or instead by [Ref.watch] (if false). /// This changes the default behavior of [when] and sets the [isReloading]/ /// [isRefreshing] flags accordingly. - AsyncValue copyWithPrevious( - AsyncValue previous, { + AsyncValue copyWithPrevious( + AsyncValue previous, { bool isRefresh = true, }); /// The opposite of [copyWithPrevious], reverting to the raw [AsyncValue] /// with no information on the previous state. - AsyncValue unwrapPrevious() { + AsyncValue unwrapPrevious() { return map( data: (d) { - if (d.isLoading) return AsyncLoading(); + if (d.isLoading) return AsyncLoading(); return AsyncData(d.value); }, error: (e) { - if (e.isLoading) return AsyncLoading(); + if (e.isLoading) return AsyncLoading(); return AsyncError(e.error, e.stackTrace); }, - loading: (l) => AsyncLoading(), + loading: (l) => AsyncLoading(), ); } @@ -525,13 +527,13 @@ sealed class AsyncValue { ], ].join(', '); - return '$_displayString<$T>($content)'; + return '$_displayString<$StateT>($content)'; } @override bool operator ==(Object other) { return runtimeType == other.runtimeType && - other is AsyncValue && + other is AsyncValue && other.isLoading == isLoading && other.hasValue == hasValue && other.error == error && @@ -551,9 +553,9 @@ sealed class AsyncValue { } /// {@macro async_value.data} -final class AsyncData extends AsyncValue { +final class AsyncData extends AsyncValue { /// {@macro async_value.data} - const AsyncData(T value) + const AsyncData(StateT value) : this._( value, isLoading: false, @@ -575,7 +577,7 @@ final class AsyncData extends AsyncValue { bool get hasValue => true; @override - final T value; + final StateT value; @override @override @@ -589,16 +591,16 @@ final class AsyncData extends AsyncValue { @override R map({ - required R Function(AsyncData data) data, - required R Function(AsyncError error) error, - required R Function(AsyncLoading loading) loading, + required R Function(AsyncData data) data, + required R Function(AsyncError error) error, + required R Function(AsyncLoading loading) loading, }) { return data(this); } @override - AsyncData copyWithPrevious( - AsyncValue previous, { + AsyncData copyWithPrevious( + AsyncValue previous, { bool isRefresh = true, }) { return this; @@ -606,7 +608,7 @@ final class AsyncData extends AsyncValue { @override AsyncValue _cast() { - if (T == R) return this as AsyncValue; + if (StateT == R) return this as AsyncValue; return AsyncData._( value as R, isLoading: isLoading, @@ -617,7 +619,7 @@ final class AsyncData extends AsyncValue { } /// {@macro async_value.loading} -final class AsyncLoading extends AsyncValue { +final class AsyncLoading extends AsyncValue { /// {@macro async_value.loading} const AsyncLoading() : hasValue = false, @@ -643,7 +645,7 @@ final class AsyncLoading extends AsyncValue { final bool hasValue; @override - final T? value; + final StateT? value; @override final Object? error; @@ -653,7 +655,7 @@ final class AsyncLoading extends AsyncValue { @override AsyncValue _cast() { - if (T == R) return this as AsyncValue; + if (StateT == R) return this as AsyncValue; return AsyncLoading._( hasValue: hasValue, value: value as R?, @@ -664,16 +666,16 @@ final class AsyncLoading extends AsyncValue { @override R map({ - required R Function(AsyncData data) data, - required R Function(AsyncError error) error, - required R Function(AsyncLoading loading) loading, + required R Function(AsyncData data) data, + required R Function(AsyncError error) error, + required R Function(AsyncLoading loading) loading, }) { return loading(this); } @override - AsyncValue copyWithPrevious( - AsyncValue previous, { + AsyncValue copyWithPrevious( + AsyncValue previous, { bool isRefresh = true, }) { if (isRefresh) { @@ -714,7 +716,7 @@ final class AsyncLoading extends AsyncValue { } /// {@macro async_value.error_ctor} -final class AsyncError extends AsyncValue { +final class AsyncError extends AsyncValue { /// {@macro async_value.error_ctor} const AsyncError(Object error, StackTrace stackTrace) : this._( @@ -743,7 +745,7 @@ final class AsyncError extends AsyncValue { final bool hasValue; @override - final T? value; + final StateT? value; @override final Object error; @@ -753,7 +755,7 @@ final class AsyncError extends AsyncValue { @override AsyncValue _cast() { - if (T == R) return this as AsyncValue; + if (StateT == R) return this as AsyncValue; return AsyncError._( error, stackTrace: stackTrace, @@ -765,16 +767,16 @@ final class AsyncError extends AsyncValue { @override R map({ - required R Function(AsyncData data) data, - required R Function(AsyncError error) error, - required R Function(AsyncLoading loading) loading, + required R Function(AsyncData data) data, + required R Function(AsyncError error) error, + required R Function(AsyncLoading loading) loading, }) { return error(this); } @override - AsyncError copyWithPrevious( - AsyncValue previous, { + AsyncError copyWithPrevious( + AsyncValue previous, { bool isRefresh = true, }) { return AsyncError._( diff --git a/packages/riverpod/lib/src/core/element.dart b/packages/riverpod/lib/src/core/element.dart index 2a17ccd67..f38bdc3fb 100644 --- a/packages/riverpod/lib/src/core/element.dart +++ b/packages/riverpod/lib/src/core/element.dart @@ -16,9 +16,9 @@ part of '../framework.dart'; /// ref.watch(provider.select((value) => value)); /// ``` /// {@endtemplate} -sealed class Refreshable implements ProviderListenable {} +sealed class Refreshable implements ProviderListenable {} -mixin _ProviderRefreshable implements Refreshable { +mixin _ProviderRefreshable implements Refreshable { ProviderBase get provider; } @@ -42,7 +42,7 @@ void Function()? debugCanModifyProviders; /// {@endtemplate} // TODO rename to ProviderElement @internal -abstract class ProviderElementBase implements Ref, Node { +abstract class ProviderElementBase implements Ref, Node { /// {@macro riverpod.provider_element_base} // TODO changelog: ProviderElement no-longer takes a provider as parameter but takes a ProviderContainer ProviderElementBase(this.container); @@ -50,10 +50,10 @@ abstract class ProviderElementBase implements Ref, Node { static ProviderElementBase? _debugCurrentlyBuildingElement; @override - State get state => readSelf(); + StateT get state => readSelf(); @override - set state(State newState) => setStateResult(ResultData(newState)); + set state(StateT newState) => setStateResult(ResultData(newState)); /// The last result of [ProviderBase.debugGetCreateSourceHash]. /// @@ -68,7 +68,7 @@ abstract class ProviderElementBase implements Ref, Node { /// The provider associated with this [ProviderElementBase], after applying overrides. // TODO changelog ProviderElement.provider is now abstract - ProviderBase get provider; + ProviderBase get provider; /// The [ProviderContainer] that owns this [ProviderElementBase]. @override @@ -89,7 +89,7 @@ abstract class ProviderElementBase implements Ref, Node { /// This is typically Flutter widgets or manual calls to [ProviderContainer.listen] /// with this provider as target. // TODO(rrousselGit) refactor to match ChangeNotifier - final _externalDependents = <_ExternalProviderSubscription>[]; + final _externalDependents = <_ExternalProviderSubscription>[]; /// The [ProviderSubscription]s associated to the providers that this /// [ProviderElementBase] listens to. @@ -114,7 +114,7 @@ abstract class ProviderElementBase implements Ref, Node { List? _onCancelListeners; List? _onAddListeners; List? _onRemoveListeners; - List? _onChangeSelfListeners; + List? _onChangeSelfListeners; List? _onErrorSelfListeners; bool _mustRecomputeState = false; @@ -139,7 +139,7 @@ abstract class ProviderElementBase implements Ref, Node { List? _keepAliveLinks; /* STATE */ - Result? _stateResult; + Result? _stateResult; /// The current state of the provider. /// @@ -153,7 +153,7 @@ abstract class ProviderElementBase implements Ref, Node { /// This is not meant for public consumption. Instead, public API should use /// [readSelf]. @internal - Result? get stateResult => _stateResult; + Result? get stateResult => _stateResult; /// Update the exposed value of a provider and notify its listeners. /// @@ -163,7 +163,7 @@ abstract class ProviderElementBase implements Ref, Node { /// This API is not meant for public consumption. Instead if a [Ref] needs /// to expose a way to update the state, the practice is to expose a getter/setter. @internal - void setStateResult(Result newState) { + void setStateResult(Result newState) { if (kDebugMode) _debugDidSetState = true; final previousResult = stateResult; @@ -182,7 +182,7 @@ abstract class ProviderElementBase implements Ref, Node { /// This is not meant for public consumption. Instead, public API should use /// [readSelf]. @internal - State get requireState { + StateT get requireState { const uninitializedError = ''' Tried to read the state of an uninitialized provider. This could mean a few things: @@ -208,7 +208,7 @@ This could mean a few things: /// Called when a provider is rebuilt. Used for providers to not notify their /// listeners if the exposed value did not change. @internal - bool updateShouldNotify(State previous, State next); + bool updateShouldNotify(StateT previous, StateT next); /* /STATE */ @@ -298,7 +298,7 @@ This could mean a few things: /// - `overrideWithValue`, which relies on [update] to handle /// the scenario where the value changed. @visibleForOverriding - void update(ProviderBase newProvider) {} + void update(ProviderBase newProvider) {} @override void invalidate(ProviderOrFamily providerOrFamily) { @@ -470,8 +470,8 @@ The provider ${_debugCurrentlyBuildingElement!.origin} modified $origin while bu } void _notifyListeners( - Result newState, - Result? previousStateResult, { + Result newState, + Result? previousStateResult, { bool checkUpdateShouldNotify = true, }) { if (kDebugMode) _debugAssertNotificationAllowed(); @@ -511,7 +511,7 @@ The provider ${_debugCurrentlyBuildingElement!.origin} modified $origin while bu previousStateResult.hasState && newState.hasState && !updateShouldNotify( - previousState as State, + previousState as StateT, newState.requireState, )) { return; @@ -774,7 +774,7 @@ final = Provider(dependencies: []); @override void listenSelf( - void Function(State? previous, State next) listener, { + void Function(StateT? previous, StateT next) listener, { void Function(Object error, StackTrace stackTrace)? onError, }) { // TODO do we want to expose a way to close the subscription? @@ -807,7 +807,7 @@ final = Provider(dependencies: []); /// Returns the currently exposed by a provider /// /// May throw if the provider threw when creating the exposed value. - State readSelf() { + StateT readSelf() { flush(); return requireState; diff --git a/packages/riverpod/lib/src/core/foundation.dart b/packages/riverpod/lib/src/core/foundation.dart index a2e5107f5..e578e1941 100644 --- a/packages/riverpod/lib/src/core/foundation.dart +++ b/packages/riverpod/lib/src/core/foundation.dart @@ -172,12 +172,12 @@ String shortHash(Object? object) { /// /// Should override ==/hashCode when possible @immutable -mixin ProviderListenable implements ProviderListenableOrFamily { +mixin ProviderListenable implements ProviderListenableOrFamily { /// Starts listening to this transformer - ProviderSubscription addListener( + ProviderSubscription addListener( // TODO remove Node and pass directly the listened Element Node node, - void Function(State? previous, State next) listener, { + void Function(StateT? previous, StateT next) listener, { required void Function(Object error, StackTrace stackTrace)? onError, // TODO remove required void Function()? onDependencyMayHaveChanged, @@ -185,7 +185,7 @@ mixin ProviderListenable implements ProviderListenableOrFamily { }); /// Obtains the result of this provider expression without adding listener. - State read(Node node); + StateT read(Node node); /// Partially listen to a provider. /// @@ -253,9 +253,9 @@ mixin ProviderListenable implements ProviderListenableOrFamily { /// This will further optimize our widget by rebuilding it only when "isAdult" /// changed instead of whenever the age changes. ProviderListenable select( - Selected Function(State value) selector, + Selected Function(StateT value) selector, ) { - return _ProviderSelector( + return _ProviderSelector( provider: this, selector: selector, ); diff --git a/packages/riverpod/lib/src/core/modifiers/select.dart b/packages/riverpod/lib/src/core/modifiers/select.dart index 3c65bc63b..9aa3fa6cc 100644 --- a/packages/riverpod/lib/src/core/modifiers/select.dart +++ b/packages/riverpod/lib/src/core/modifiers/select.dart @@ -5,9 +5,9 @@ part of '../../framework.dart'; @internal abstract class Node { /// Starts listening to this transformer - ProviderSubscription listen( - ProviderListenable listenable, - void Function(State? previous, State next) listener, { + ProviderSubscription listen( + ProviderListenable listenable, + void Function(StateT? previous, StateT next) listener, { void Function(Object error, StackTrace stackTrace)? onError, bool fireImmediately = false, }); @@ -18,21 +18,21 @@ abstract class Node { /// /// Do not use this in production code. This is exposed only for testing /// and devtools, to be able to test if a provider has listeners or similar. - ProviderElementBase readProviderElement( - ProviderBase provider, + ProviderElementBase readProviderElement( + ProviderBase provider, ); /// Subscribes to a [ProviderElementBase]. - ProviderSubscription _listenElement( - ProviderElementBase element, { - required void Function(State? previous, State next) listener, + ProviderSubscription _listenElement( + ProviderElementBase element, { + required void Function(StateT? previous, StateT next) listener, required void Function(Object error, StackTrace stackTrace) onError, }); } /// An internal class for `ProviderBase.select`. @sealed -class _ProviderSelector with ProviderListenable { +class _ProviderSelector with ProviderListenable { /// An internal class for `ProviderBase.select`. _ProviderSelector({ required this.provider, @@ -40,12 +40,12 @@ class _ProviderSelector with ProviderListenable { }); /// The provider that was selected - final ProviderListenable provider; + final ProviderListenable provider; /// The selector applied - final Output Function(Input) selector; + final OutputT Function(InputT) selector; - Result _select(Result value) { + Result _select(Result value) { if (kDebugMode) _debugIsRunningSelector = true; try { @@ -63,11 +63,11 @@ class _ProviderSelector with ProviderListenable { } void _selectOnChange({ - required Input newState, - required Result lastSelectedValue, + required InputT newState, + required Result lastSelectedValue, required void Function(Object error, StackTrace stackTrace) onError, - required void Function(Output? prev, Output next) listener, - required void Function(Result newState) onChange, + required void Function(OutputT? prev, OutputT next) listener, + required void Function(Result newState) onChange, }) { final newSelectedValue = _select(Result.data(newState)); if (!lastSelectedValue.hasState || @@ -91,18 +91,18 @@ class _ProviderSelector with ProviderListenable { } @override - _SelectorSubscription addListener( + _SelectorSubscription addListener( Node node, - void Function(Output? previous, Output next) listener, { + void Function(OutputT? previous, OutputT next) listener, { required void Function(Object error, StackTrace stackTrace)? onError, required void Function()? onDependencyMayHaveChanged, required bool fireImmediately, }) { onError ??= Zone.current.handleUncaughtError; - late Result lastSelectedValue; + late Result lastSelectedValue; - final sub = node.listen( + final sub = node.listen( provider, (prev, input) { _selectOnChange( @@ -141,7 +141,7 @@ class _ProviderSelector with ProviderListenable { } @override - Output read(Node node) { + OutputT read(Node node) { final input = provider.read(node); return selector(input); } diff --git a/packages/riverpod/lib/src/core/modifiers/select_async.dart b/packages/riverpod/lib/src/core/modifiers/select_async.dart index b2b7f1a65..21d48c2b6 100644 --- a/packages/riverpod/lib/src/core/modifiers/select_async.dart +++ b/packages/riverpod/lib/src/core/modifiers/select_async.dart @@ -2,7 +2,7 @@ part of '../../framework.dart'; /// An internal class for `ProviderBase.selectAsync`. @sealed -class _AsyncSelector with ProviderListenable> { +class _AsyncSelector with ProviderListenable> { /// An internal class for `ProviderBase.select`. _AsyncSelector({ required this.provider, @@ -11,15 +11,15 @@ class _AsyncSelector with ProviderListenable> { }); /// The provider that was selected - final ProviderListenable> provider; + final ProviderListenable> provider; /// The future associated to the listened provider - final ProviderListenable> future; + final ProviderListenable> future; /// The selector applied - final Output Function(Input) selector; + final OutputT Function(InputT) selector; - Result _select(Input value) { + Result _select(InputT value) { if (kDebugMode) _debugIsRunningSelector = true; try { @@ -32,18 +32,18 @@ class _AsyncSelector with ProviderListenable> { } @override - _SelectorSubscription, Future> addListener( + _SelectorSubscription, Future> addListener( Node node, - void Function(Future? previous, Future next) listener, { + void Function(Future? previous, Future next) listener, { required void Function(Object error, StackTrace stackTrace)? onError, required void Function()? onDependencyMayHaveChanged, required bool fireImmediately, }) { - Result? lastSelectedValue; - Completer? selectedCompleter; - Future? selectedFuture; + Result? lastSelectedValue; + Completer? selectedCompleter; + Future? selectedFuture; - void emitData(Output data, {required bool callListeners}) { + void emitData(OutputT data, {required bool callListeners}) { final previousFuture = selectedFuture; if (selectedCompleter != null) { selectedCompleter!.complete(data); @@ -70,7 +70,7 @@ class _AsyncSelector with ProviderListenable> { } void playValue( - AsyncValue value, { + AsyncValue value, { bool callListeners = true, }) { void onLoading(AsyncValue loading) { @@ -134,7 +134,7 @@ class _AsyncSelector with ProviderListenable> { ); } - final sub = node.listen>( + final sub = node.listen>( provider, (prev, input) => playValue(input), onError: onError, @@ -153,5 +153,5 @@ class _AsyncSelector with ProviderListenable> { } @override - Future read(Node node) => future.read(node).then(selector); + Future read(Node node) => future.read(node).then(selector); } diff --git a/packages/riverpod/lib/src/core/override_with_value.dart b/packages/riverpod/lib/src/core/override_with_value.dart index e7e2c186d..9b92a6f7e 100644 --- a/packages/riverpod/lib/src/core/override_with_value.dart +++ b/packages/riverpod/lib/src/core/override_with_value.dart @@ -33,25 +33,25 @@ final class $ValueProvider extends ProviderBase } /// The [ProviderElementBase] of a [$ValueProvider] -class _ValueProviderElement extends ProviderElementBase { +class _ValueProviderElement extends ProviderElementBase { /// The [ProviderElementBase] of a [$ValueProvider] _ValueProviderElement(this.provider, super.container); /// A custom listener called when `overrideWithValue` changes /// with a different value. - void Function(State value)? onChange; + void Function(StateT value)? onChange; @override - $ValueProvider provider; + $ValueProvider provider; @override - void update(covariant $ValueProvider newProvider) { + void update(covariant $ValueProvider newProvider) { super.update(newProvider); provider = newProvider; final newValue = provider._value; // `getState` will never be in error/loading state since there is no "create" - final previousState = stateResult! as ResultData; + final previousState = stateResult! as ResultData; if (newValue != previousState.state) { // Asserts would otherwise prevent a provider rebuild from updating @@ -74,7 +74,7 @@ class _ValueProviderElement extends ProviderElementBase { } @override - bool updateShouldNotify(State previous, State next) { + bool updateShouldNotify(StateT previous, StateT next) { return true; } } diff --git a/packages/riverpod/lib/src/core/provider/provider.dart b/packages/riverpod/lib/src/core/provider/provider.dart index e2935be06..528b69b0d 100644 --- a/packages/riverpod/lib/src/core/provider/provider.dart +++ b/packages/riverpod/lib/src/core/provider/provider.dart @@ -13,7 +13,9 @@ part of '../../framework.dart'; /// - [Ref], which exposes the methods to read other providers. /// - [Provider], a provider that uses [Create] to expose an immutable value. @internal -typedef Create> = T Function(R ref); +typedef Create> = CreatedT Function( + RefT ref, +); /// A callback used to catches errors @internal @@ -21,7 +23,6 @@ typedef OnError = void Function(Object, StackTrace); /// A base class for _all_ providers. @immutable -// TODO rename all generics to // Marked as "base" because linters/generators rely on fields on const provider instances. abstract base class ProviderBase extends ProviderOrFamily with ProviderListenable diff --git a/packages/riverpod/lib/src/core/provider_subscription.dart b/packages/riverpod/lib/src/core/provider_subscription.dart index 03a386ae9..8bfcb1399 100644 --- a/packages/riverpod/lib/src/core/provider_subscription.dart +++ b/packages/riverpod/lib/src/core/provider_subscription.dart @@ -1,17 +1,17 @@ part of '../framework.dart'; /// Represents the subscription to a [ProviderListenable] -abstract class ProviderSubscription { +abstract class ProviderSubscription { /// Stops listening to the provider @mustCallSuper void close(); /// Obtain the latest value emitted by the provider - State read(); + StateT read(); } /// When a provider listens to another provider using `listen` -class _ProviderListener implements ProviderSubscription { +class _ProviderListener implements ProviderSubscription { _ProviderListener._({ required this.listenedElement, required this.dependentElement, @@ -22,7 +22,7 @@ class _ProviderListener implements ProviderSubscription { // TODO can't we type it properly? final void Function(Object? prev, Object? state) listener; final ProviderElementBase dependentElement; - final ProviderElementBase listenedElement; + final ProviderElementBase listenedElement; final OnError onError; @override @@ -34,21 +34,21 @@ class _ProviderListener implements ProviderSubscription { } @override - State read() => listenedElement.readSelf(); + StateT read() => listenedElement.readSelf(); } var _debugIsRunningSelector = false; -class _ExternalProviderSubscription - implements ProviderSubscription { +class _ExternalProviderSubscription + implements ProviderSubscription { _ExternalProviderSubscription._( this._listenedElement, this._listener, { required this.onError, }); - final void Function(State? previous, State next) _listener; - final ProviderElementBase _listenedElement; + final void Function(StateT? previous, StateT next) _listener; + final ProviderElementBase _listenedElement; final void Function(Object error, StackTrace stackTrace) onError; var _closed = false; @@ -60,7 +60,7 @@ class _ExternalProviderSubscription } @override - State read() { + StateT read() { if (_closed) { throw StateError( 'called ProviderSubscription.read on a subscription that was closed', @@ -72,9 +72,9 @@ class _ExternalProviderSubscription /// Deals with the internals of synchronously calling the listeners /// when using `fireImmediately: true` -void _handleFireImmediately( - Result currentState, { - required void Function(State? previous, State current) listener, +void _handleFireImmediately( + Result currentState, { + required void Function(StateT? previous, StateT current) listener, required void Function(Object error, StackTrace stackTrace) onError, }) { currentState.map( diff --git a/packages/riverpod/lib/src/core/proxy_provider_listenable.dart b/packages/riverpod/lib/src/core/proxy_provider_listenable.dart index dcb0f1d2f..b3ca927b2 100644 --- a/packages/riverpod/lib/src/core/proxy_provider_listenable.dart +++ b/packages/riverpod/lib/src/core/proxy_provider_listenable.dart @@ -1,6 +1,6 @@ part of '../framework.dart'; -class _ProxySubscription extends ProviderSubscription { +class _ProxySubscription extends ProviderSubscription { _ProxySubscription( this._removeListeners, this._read, { @@ -9,7 +9,7 @@ class _ProxySubscription extends ProviderSubscription { final ProviderSubscription innerSubscription; final void Function() _removeListeners; - final T Function() _read; + final StateT Function() _read; @override void close() { @@ -18,7 +18,7 @@ class _ProxySubscription extends ProviderSubscription { } @override - T read() => _read(); + StateT read() => _read(); } /// An internal utility for reading alternate values of a provider. @@ -37,8 +37,8 @@ class _ProxySubscription extends ProviderSubscription { /// /// This API is not meant for public consumption. @internal -class ProviderElementProxy - with ProviderListenable, _ProviderRefreshable { +class ProviderElementProxy + with ProviderListenable, _ProviderRefreshable { /// An internal utility for reading alternate values of a provider. /// /// For example, this is used by [FutureProvider] to differentiate: @@ -57,15 +57,15 @@ class ProviderElementProxy const ProviderElementProxy(this.provider, this._lense); @override - final ProviderBase provider; - final ProxyElementValueListenable Function( - ProviderElementBase element, + final ProviderBase provider; + final ProxyElementValueListenable Function( + ProviderElementBase element, ) _lense; @override - ProviderSubscription addListener( + ProviderSubscription addListener( Node node, - void Function(Output? previous, Output next) listener, { + void Function(OutputT? previous, OutputT next) listener, { required void Function(Object error, StackTrace stackTrace)? onError, required void Function()? onDependencyMayHaveChanged, required bool fireImmediately, @@ -100,7 +100,7 @@ class ProviderElementProxy () => read(node), // While we don't care about changes to the element, calling _listenElement // is necessary to tell the listened element that it is being listened. - innerSubscription: node._listenElement( + innerSubscription: node._listenElement( element, listener: (prev, next) {}, onError: (err, stack) {}, @@ -109,7 +109,7 @@ class ProviderElementProxy } @override - Output read(Node node) { + OutputT read(Node node) { final element = node.readProviderElement(provider); element.flush(); element._mayNeedDispose(); @@ -118,7 +118,7 @@ class ProviderElementProxy @override bool operator ==(Object other) => - other is ProviderElementProxy && + other is ProviderElementProxy && other.provider == provider; @override diff --git a/packages/riverpod/lib/src/core/ref.dart b/packages/riverpod/lib/src/core/ref.dart index 357bfcad4..4a00d2725 100644 --- a/packages/riverpod/lib/src/core/ref.dart +++ b/packages/riverpod/lib/src/core/ref.dart @@ -9,7 +9,7 @@ part of '../framework.dart'; /// - [read] and [watch], two methods that allow a provider to consume other providers. /// - [onDispose], a method that allows performing a task when the provider is destroyed. /// {@endtemplate} -abstract class Ref { +abstract class Ref { // TODO changelog breaking: AutoDisposeRef and related interfaces are removed. // Use the non-autodispose variant instead. They now have the same API. @@ -22,8 +22,8 @@ abstract class Ref { /// - on asynchronous providers, this will return an [AsyncLoading]. /// /// Will throw if the provider threw during creation. - State get state; - set state(State newState); + StateT get state; + set state(StateT newState); /// The [ProviderContainer] that this provider is associated with. ProviderContainer get container; @@ -107,7 +107,7 @@ abstract class Ref { /// [ProviderElementBase.updateShouldNotify] returns false, meaning that the previous /// and new value can potentially be identical. void listenSelf( - void Function(State? previous, State next) listener, { + void Function(StateT? previous, StateT next) listener, { void Function(Object error, StackTrace stackTrace)? onError, }); diff --git a/packages/riverpod/lib/src/providers/async_notifier/family.dart b/packages/riverpod/lib/src/providers/async_notifier/family.dart index b6a354f09..36879719d 100644 --- a/packages/riverpod/lib/src/providers/async_notifier/family.dart +++ b/packages/riverpod/lib/src/providers/async_notifier/family.dart @@ -3,7 +3,8 @@ part of '../async_notifier.dart'; /// {@macro riverpod.async_notifier} /// /// {@macro riverpod.async_notifier_provider_modifier} -abstract class FamilyAsyncNotifier extends $AsyncNotifier { +abstract class FamilyAsyncNotifier + extends $AsyncNotifier { /// {@template riverpod.notifier.family_arg} /// The argument that was passed to this family. /// @@ -15,15 +16,15 @@ abstract class FamilyAsyncNotifier extends $AsyncNotifier { /// /// then [arg] will be `0`. /// {@endtemplate} - late final Arg arg = (ref as ProviderElementBase).origin.argument as Arg; + late final ArgT arg = (ref as ProviderElementBase).origin.argument as ArgT; /// {@macro riverpod.async_notifier.build} @visibleForOverriding - FutureOr build(Arg arg); + FutureOr build(ArgT arg); @internal @override - FutureOr runBuild() => build(arg); + FutureOr runBuild() => build(arg); } /// The [Family] of [AsyncNotifierProvider]. diff --git a/packages/riverpod/lib/src/providers/async_notifier/orphan.dart b/packages/riverpod/lib/src/providers/async_notifier/orphan.dart index 2145bca99..7cd802786 100644 --- a/packages/riverpod/lib/src/providers/async_notifier/orphan.dart +++ b/packages/riverpod/lib/src/providers/async_notifier/orphan.dart @@ -16,7 +16,7 @@ part of '../async_notifier.dart'; /// {@endtemplate} /// /// {@macro riverpod.async_notifier_provider_modifier} -abstract class AsyncNotifier extends $AsyncNotifier { +abstract class AsyncNotifier extends $AsyncNotifier { /// {@template riverpod.async_notifier.build} /// Initialize an [AsyncNotifier]. /// @@ -31,11 +31,11 @@ abstract class AsyncNotifier extends $AsyncNotifier { /// will be caught and an [AsyncError] will be emitted. /// {@endtemplate} @visibleForOverriding - FutureOr build(); + FutureOr build(); @internal @override - FutureOr runBuild() => build(); + FutureOr runBuild() => build(); } /// {@template riverpod.async_notifier_provider} diff --git a/packages/riverpod/lib/src/providers/legacy/state_controller.dart b/packages/riverpod/lib/src/providers/legacy/state_controller.dart index 32ada9304..7e8c3a8e6 100644 --- a/packages/riverpod/lib/src/providers/legacy/state_controller.dart +++ b/packages/riverpod/lib/src/providers/legacy/state_controller.dart @@ -3,16 +3,16 @@ import 'package:state_notifier/state_notifier.dart'; /// A [StateNotifier] that allows modifying its [state] from outside. /// /// This avoids having to make a [StateNotifier] subclass for simple scenarios. -class StateController extends StateNotifier { +class StateController extends StateNotifier { /// Initialize the state of [StateController]. StateController(super._state); // Remove the protected status @override - T get state => super.state; + StateT get state => super.state; @override - set state(T value) => super.state = value; + set state(StateT value) => super.state = value; /// Calls a function with the current [state] and assigns the result as the /// new state. @@ -29,5 +29,5 @@ class StateController extends StateNotifier { /// ```dart /// ref.read(provider.notifier).update((state) => state + 1); /// ``` - T update(T Function(T state) cb) => state = cb(state); + StateT update(StateT Function(StateT state) cb) => state = cb(state); } diff --git a/packages/riverpod/lib/src/providers/legacy/state_provider.dart b/packages/riverpod/lib/src/providers/legacy/state_provider.dart index 93bcee12c..11aff9606 100644 --- a/packages/riverpod/lib/src/providers/legacy/state_provider.dart +++ b/packages/riverpod/lib/src/providers/legacy/state_provider.dart @@ -17,11 +17,11 @@ ProviderElementProxy> _notifier( /// {@macro riverpod.provider_ref_base} /// - [controller], the [StateController] currently exposed by this provider. -abstract class StateProviderRef implements Ref { +abstract class StateProviderRef implements Ref { /// The [StateController] currently exposed by this provider. /// /// Cannot be accessed while creating the provider. - StateController get controller; + StateController get controller; } /// {@template riverpod.state_provider} diff --git a/packages/riverpod/lib/src/providers/notifier/orphan.dart b/packages/riverpod/lib/src/providers/notifier/orphan.dart index a60c242c7..d0d1df078 100644 --- a/packages/riverpod/lib/src/providers/notifier/orphan.dart +++ b/packages/riverpod/lib/src/providers/notifier/orphan.dart @@ -52,7 +52,7 @@ part of '../notifier.dart'; /// When using `family`, your notifier type changes. /// Instead of extending [Notifier], you should extend [FamilyNotifier]. /// {@endtemplate} -abstract class Notifier extends $Notifier { +abstract class Notifier extends $Notifier { /// {@template riverpod.notifier.build} /// Initialize a [Notifier]. /// @@ -66,11 +66,11 @@ abstract class Notifier extends $Notifier { /// If this method throws, reading this provider will rethrow the error. /// {@endtemplate} @visibleForOverriding - State build(); + StateT build(); @internal @override - State runBuild() => build(); + StateT runBuild() => build(); } final class NotifierProvider, StateT> diff --git a/packages/riverpod/lib/src/providers/stream_notifier/family.dart b/packages/riverpod/lib/src/providers/stream_notifier/family.dart index 481e56e75..6366073e8 100644 --- a/packages/riverpod/lib/src/providers/stream_notifier/family.dart +++ b/packages/riverpod/lib/src/providers/stream_notifier/family.dart @@ -3,7 +3,8 @@ part of '../stream_notifier.dart'; /// {@macro riverpod.async_notifier} /// /// {@macro riverpod.async_notifier_provider_modifier} -abstract class FamilyStreamNotifier extends $StreamNotifier { +abstract class FamilyStreamNotifier + extends $StreamNotifier { /// {@template riverpod.notifier.family_arg} /// The argument that was passed to this family. /// @@ -15,15 +16,15 @@ abstract class FamilyStreamNotifier extends $StreamNotifier { /// /// then [arg] will be `0`. /// {@endtemplate} - late final Arg arg = (ref as ProviderElementBase).origin.argument as Arg; + late final ArgT arg = (ref as ProviderElementBase).origin.argument as ArgT; /// {@macro riverpod.async_notifier.build} @visibleForOverriding - Stream build(Arg arg); + Stream build(ArgT arg); @internal @override - Stream runBuild() => build(arg); + Stream runBuild() => build(arg); } final class FamilyStreamNotifierProvider< // diff --git a/packages/riverpod/lib/src/providers/stream_notifier/orphan.dart b/packages/riverpod/lib/src/providers/stream_notifier/orphan.dart index 49b4b2451..3e612185c 100644 --- a/packages/riverpod/lib/src/providers/stream_notifier/orphan.dart +++ b/packages/riverpod/lib/src/providers/stream_notifier/orphan.dart @@ -15,14 +15,14 @@ part of '../stream_notifier.dart'; /// When using `family`, your notifier type changes. Instead of extending /// [StreamNotifier], you should extend [FamilyStreamNotifier]. /// {@endtemplate} -abstract class StreamNotifier extends $StreamNotifier { +abstract class StreamNotifier extends $StreamNotifier { /// {@macro riverpod.async_notifier.build} @visibleForOverriding - Stream build(); + Stream build(); @internal @override - Stream runBuild() => build(); + Stream runBuild() => build(); } /// {@template riverpod.async_notifier_provider} diff --git a/packages/riverpod/test/meta_test.dart b/packages/riverpod/test/meta_test.dart index 723664e44..423bf0797 100644 --- a/packages/riverpod/test/meta_test.dart +++ b/packages/riverpod/test/meta_test.dart @@ -45,7 +45,6 @@ void main() { 'AsyncData', 'AsyncLoading', 'AsyncError', - // TODO rename 'ProviderBase', 'NotifierBase', 'Refreshable',