Skip to content

Commit

Permalink
fix: retrieval of scope state
Browse files Browse the repository at this point in the history
  • Loading branch information
nank1ro committed Feb 3, 2025
1 parent 619b2ad commit 3ee596a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
13 changes: 4 additions & 9 deletions packages/disco/lib/src/models/providers/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ class Provider<T extends Object> extends InstantiableProvider {

/// {@macro Provider.lazy}
bool? lazy,
}) : _disposeValue = dispose,
}) : _createValue = create,
_disposeValue = dispose,
_lazy = lazy ?? DiscoConfig.lazy,
super._() {
_createValue = (context, scopeState) {
_scopeState = scopeState;
return create(context);
};
}
super._();

/// {@macro arg-provider}
static ArgProvider<T, A> withArgument<T extends Object, A>(
Expand All @@ -65,8 +61,7 @@ class Provider<T extends Object> extends InstantiableProvider {
/// {@template Provider.create}
/// The function called to create the element.
/// {@endtemplate}
late final T Function(BuildContext context, ProviderScopeState scopeState)
_createValue;
late final CreateProviderValueFn<T> _createValue;

/// {@template Provider.dispose}
/// An optional dispose function called when the [ProviderScope] that created
Expand Down
36 changes: 18 additions & 18 deletions packages/disco/lib/src/widgets/provider_scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ class ProviderScopeState extends State<ProviderScope> {
// NB: even though `id` and `provider` point to the same reference,
// two different variables are used to simplify understanding how
// providers are saved.
final id = provider;
final id = provider.._scopeState = this;

// In this case, the provider put in scope can be the ID itself.
allProvidersInScope[id] = provider;

// create non lazy providers.
if (!provider._lazy) {
// create and store the provider
createdProviderValues[id] = provider._createValue(context, this);
createdProviderValues[id] = provider._createValue(context);
}
}

Expand Down Expand Up @@ -205,10 +205,11 @@ class ProviderScopeState extends State<ProviderScope> {
if (!instantiableArgProvider._argProvider._lazy) {
// the intermediate ID is a reference to the associated generated
// intermediate provider
final intermediateId = allArgProvidersInScope[id]!;
final intermediateId = allArgProvidersInScope[id]!
.._scopeState = this;
// create and store the provider
createdProviderValues[intermediateId] =
allArgProvidersInScope[id]!._createValue(context, this);
allArgProvidersInScope[id]!._createValue(context);
}
}
} else if (widget.overrides != null) {
Expand All @@ -234,15 +235,15 @@ class ProviderScopeState extends State<ProviderScope> {
);

for (final override in providerOverrides) {
final id = override._provider;
final id = override._provider.._scopeState = this;

allProvidersInScope[id] = override._generateIntermediateProvider();

// create providers (they are never lazy in the case of overrides)
{
// create and store the provider
createdProviderValues[id] =
allProvidersInScope[id]!._createValue(context, this);
allProvidersInScope[id]!._createValue(context);
}
}

Expand Down Expand Up @@ -272,14 +273,12 @@ class ProviderScopeState extends State<ProviderScope> {
allArgProvidersInScope[id] = override._generateIntermediateProvider();

// create providers (they are never lazy in the case of overrides)
{
// the intermediate ID is a reference to the associated generated
// intermediate provider
final intermediateId = allArgProvidersInScope[id]!;
// create and store the provider
createdProviderValues[intermediateId] =
allArgProvidersInScope[id]!._createValue(context, this);
}
// the intermediate ID is a reference to the associated generated
// intermediate provider
final intermediateId = allArgProvidersInScope[id]!.._scopeState = this;
// create and store the provider
createdProviderValues[intermediateId] =
allArgProvidersInScope[id]!._createValue(context);
}
}
}
Expand Down Expand Up @@ -319,9 +318,9 @@ class ProviderScopeState extends State<ProviderScope> {
/// Creates a provider value and stores it to [createdProviderValues].
dynamic createProviderValue(Provider id) {
// find the intermediate provider in the list
final provider = getIntermediateProvider(id)!;
final provider = getIntermediateProvider(id)!.._scopeState = this;
// create and return its value
final value = provider._createValue(context, this);
final value = provider._createValue(context);
// store the created provider value
createdProviderValues[id] = value;
return value;
Expand All @@ -348,9 +347,10 @@ class ProviderScopeState extends State<ProviderScope> {
ArgProvider id,
) {
// find the intermediate provider in the list
final provider = getIntermediateProviderForArgProvider(id)!;
final provider = getIntermediateProviderForArgProvider(id)!
.._scopeState = this;
// create and return its value
final value = provider._createValue(context, this);
final value = provider._createValue(context);
// store the created provider value
createdProviderValues[allArgProvidersInScope[id]!] = value;
return value;
Expand Down
6 changes: 2 additions & 4 deletions packages/disco/test/disco_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,15 @@ void main() {
providers: [numberProvider, doubleNumberProvider],
child: Builder(
builder: (context) {
final number = numberProvider.of(context);
final doubleNumber = doubleNumberProvider.of(context);
return Text('$number $doubleNumber');
return Text('$doubleNumber');
},
),
),
),
),
);
Finder numberFinder(int value1, int value2) => find.text('$value1 $value2');
expect(numberFinder(5, 10), findsOneWidget);
expect(find.text('10'), findsOneWidget);
});

testWidgets('Test ProviderScope throws an error for a not found provider',
Expand Down

0 comments on commit 3ee596a

Please sign in to comment.