Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
🐛 fixed issue with auto disposed use case provider
  • Loading branch information
sarbagyastha committed Jan 17, 2023
commit 510b07b16500ed71c25574e4d1c4816e32db00e7
4 changes: 2 additions & 2 deletions packages/clean_framework/example/lib/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import 'package:clean_framework_example/features/profile/domain/profile_use_case
import 'package:clean_framework_example/features/profile/external_interface/pokemon_profile_gateway.dart';
import 'package:clean_framework_example/features/profile/external_interface/pokemon_species_gateway.dart';

final homeUseCaseProvider = UseCaseProvider.autoDispose(HomeUseCase.new);
final homeUseCaseProvider = UseCaseProvider(HomeUseCase.new);

final profileUseCaseProvider = UseCaseProvider(ProfileUseCase.new);
final profileUseCaseProvider = UseCaseProvider.autoDispose(ProfileUseCase.new);

final pokemonCollectionGateway = GatewayProvider(
PokemonCollectionGateway.new,
Expand Down
4 changes: 2 additions & 2 deletions packages/clean_framework/lib/src/core/gateway/gateway.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class Gateway<O extends Output, R extends Request,
_useCaseProviders = providers;

for (final useCaseProvider in providers) {
useCaseProvider.notifier.then(
useCaseProvider.notifier.listen(
(notifier) {
ref
.read(notifier)
Expand Down Expand Up @@ -77,7 +77,7 @@ abstract class WatcherGateway<
@nonVirtual
void yieldResponse(P response) {
for (final useCaseProvider in _useCaseProviders) {
useCaseProvider.notifier.then(
useCaseProvider.notifier.listen(
(notifier) {
_ref.read(notifier).setInput(onSuccess(response));
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ typedef RequestSubscription<I extends Input> = Result<I> Function(dynamic);
extension RequestSubscriptionMapExtension<I extends Input>
on RequestSubscriptionMap<I> {
void add<O extends Output>(RequestSubscription<I> subscription) {
if (this[O] != null) {
throw StateError('A subscription for $O already exists.');
}

this[O] = subscription;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ abstract class UseCaseProviderBase<E extends Entity, U extends UseCase<E>,
N extends ProviderBase<E>> extends CleanFrameworkProvider<N> {
UseCaseProviderBase({required super.provider});

Future<Refreshable<U>> get notifier => _initCompleter.future;
final StreamController<Refreshable<U>> _notifierController =
StreamController.broadcast();

Stream<Refreshable<U>> get notifier => _notifierController.stream;

@visibleForOverriding
Refreshable<U> buildNotifier();

void init() {
if (!_initCompleter.isCompleted) {
_initCompleter.complete(buildNotifier());
}
_notifierController.add(buildNotifier());
}

final Completer<Refreshable<U>> _initCompleter = Completer();

O subscribe<O extends Output>(WidgetRef ref) {
return ref.watch(_listenForOutputChange(ref));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ class _PresenterState<V extends ViewModel, O extends Output, U extends UseCase>
void initState() {
super.initState();
widget._provider
..init()
..notifier.then((_) => widget.onLayoutReady(context, _useCase!));
..notifier.first.then((_) {
widget.onLayoutReady(context, _useCase!);
})
..init();
}

@override
Expand Down
10 changes: 5 additions & 5 deletions packages/clean_framework/test/providers/gateway_unit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void main() {

await useCase.doFakeRequest(const TestDirectOutput('123'));

expect(useCase.entity, EntityFake(value: 'success'));
expect(useCase.entity, const EntityFake(value: 'success'));
});

test('Gateway unit test for failure on direct output', () async {
Expand All @@ -26,7 +26,7 @@ void main() {

await useCase.doFakeRequest(const TestDirectOutput('123'));

expect(useCase.entity, EntityFake(value: 'failure'));
expect(useCase.entity, const EntityFake(value: 'failure'));
});

test('Gateway unit test for success on yield output', () async {
Expand All @@ -39,11 +39,11 @@ void main() {

await useCase.doFakeRequest(const TestSubscriptionOutput('123'));

expect(useCase.entity, EntityFake(value: 'success'));
expect(useCase.entity, const EntityFake(value: 'success'));

gateway.yieldResponse(const TestResponse('with yield'));

expect(useCase.entity, EntityFake(value: 'success with input'));
expect(useCase.entity, const EntityFake(value: 'success with input'));
});

test('Gateway unit test for failure on yield output', () async {
Expand All @@ -55,7 +55,7 @@ void main() {

await useCase.doFakeRequest(const TestSubscriptionOutput('123'));

expect(useCase.entity, EntityFake(value: 'failure'));
expect(useCase.entity, const EntityFake(value: 'failure'));
});

test('props', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ class TestPresenter extends Presenter<TestViewModel, TestOutput, TestUseCase> {
class TestUseCase extends UseCase<EntityFake> {
TestUseCase()
: super(
entity: EntityFake(),
entity: const EntityFake(),
transformers: [
OutputTransformer.from((entity) => TestOutput(entity.value)),
],
);

Future<void> fetch() async {
entity = EntityFake(value: 'a');
entity = const EntityFake(value: 'a');

await Future<void>.delayed(const Duration(milliseconds: 100));

entity = EntityFake(value: 'b');
entity = const EntityFake(value: 'b');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ abstract class FirebaseGateway<O extends Output, R extends FirebaseRequest,
}) : super(context: context, provider: provider);

@override
FailureInput onFailure(FailureResponse failureResponse) => FailureInput();
FailureInput onFailure(FailureResponse failureResponse) {
return FailureInput(message: failureResponse.message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ void main() {
);
};

await useCase.doFakeRequest(TestOutput('123'));
await useCase.doFakeRequest(const TestOutput('123'));

expect(useCase.entity, EntityFake(value: 'success'));
expect(useCase.entity, const EntityFake(value: 'success'));
});

test('FirebaseGateway transport failure', () async {
Expand All @@ -28,9 +28,9 @@ void main() {
);
};

await useCase.doFakeRequest(TestOutput('123'));
await useCase.doFakeRequest(const TestOutput('123'));

expect(useCase.entity, EntityFake(value: 'failure'));
expect(useCase.entity, const EntityFake(value: 'failure'));
});
}

Expand All @@ -55,14 +55,16 @@ class TestGateway extends FirebaseGateway<TestOutput, FirebaseReadIdRequest,
}

class TestOutput extends Output {
TestOutput(this.id);
const TestOutput(this.id);

final String id;

@override
List<Object?> get props => [id];
}

class TestSuccessInput extends SuccessInput {
TestSuccessInput(this.foo);
const TestSuccessInput(this.foo);

final String foo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ void main() {
);
};

await useCase.doFakeRequest(TestOutput('123'));
await useCase.doFakeRequest(const TestOutput('123'));

expect(useCase.entity, EntityFake(value: 'success'));
expect(useCase.entity, const EntityFake(value: 'success'));
});

test('FirebaseWatcherGateway transport failure', () async {
Expand All @@ -28,9 +28,9 @@ void main() {
);
};

await useCase.doFakeRequest(TestOutput('123'));
await useCase.doFakeRequest(const TestOutput('123'));

expect(useCase.entity, EntityFake(value: 'failure'));
expect(useCase.entity, const EntityFake(value: 'failure'));
});
}

Expand All @@ -55,14 +55,16 @@ class TestGateway extends FirebaseWatcherGateway<TestOutput,
}

class TestOutput extends Output {
TestOutput(this.id);
const TestOutput(this.id);

final String id;

@override
List<Object?> get props => [id];
}

class TestSuccessInput extends SuccessInput {
TestSuccessInput(this.foo);
const TestSuccessInput(this.foo);

final String foo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class GatewayFake extends GraphQLGateway {

@override
SuccessInput onSuccess(GraphQLSuccessResponse response) {
return SuccessInput();
return const SuccessInput();
}
}

Expand All @@ -217,7 +217,7 @@ class MutationGatewayFake extends GraphQLGateway {

@override
SuccessInput onSuccess(GraphQLSuccessResponse response) {
return SuccessInput();
return const SuccessInput();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void main() {
};

await useCase.doFakeRequest(TestOutput());
expect(useCase.entity, EntityFake(value: 'success'));
expect(useCase.entity, const EntityFake(value: 'success'));

final request = gateway.buildRequest(TestOutput());
expect(request.variables, null);
Expand All @@ -30,7 +30,7 @@ void main() {
};

await useCase.doFakeRequest(TestOutput());
expect(useCase.entity, EntityFake(value: 'failure'));
expect(useCase.entity, const EntityFake(value: 'failure'));
});
}

Expand All @@ -45,7 +45,7 @@ class TestGateway

@override
SuccessInput onSuccess(GraphQLSuccessResponse response) {
return SuccessInput();
return const SuccessInput();
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/clean_framework_rest/lib/src/rest_gateway.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ abstract class RestGateway<O extends Output, R extends RestRequest,

@override
FailureInput onFailure(FailureResponse failureResponse) {
return FailureInput();
return FailureInput(message: failureResponse.message);
}
}
6 changes: 3 additions & 3 deletions packages/clean_framework_rest/test/rest_gateway_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
};

await useCase.doFakeRequest(TestOutput());
expect(useCase.entity, EntityFake(value: 'success'));
expect(useCase.entity, const EntityFake(value: 'success'));

final request = gateway.buildRequest(TestOutput());
expect(request.params, request.data);
Expand All @@ -27,7 +27,7 @@ void main() {
};

await useCase.doFakeRequest(TestOutput());
expect(useCase.entity, EntityFake(value: 'failure'));
expect(useCase.entity, const EntityFake(value: 'failure'));
});

test('other requests', () {
Expand All @@ -50,7 +50,7 @@ class TestGateway extends RestGateway<TestOutput, TestRequest, SuccessInput> {

@override
SuccessInput onSuccess(RestSuccessResponse response) {
return SuccessInput();
return const SuccessInput();
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/clean_framework_test/lib/src/use_case_fake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UseCaseFake<S extends SuccessInput> extends Fake
implements UseCase<EntityFake> {
UseCaseFake({this.output});

EntityFake _entity = EntityFake();
EntityFake _entity = const EntityFake();
late RequestSubscription subscription;
S? successInput;
final Output? output;
Expand Down Expand Up @@ -54,7 +54,8 @@ class UseCaseFake<S extends SuccessInput> extends Fake
}

class EntityFake extends Entity {
EntityFake({this.value = 'initial'});
const EntityFake({this.value = 'initial'});

final String value;

@override
Expand Down