Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Create custom CoroutineScope to be used application-wide #410

Merged
merged 1 commit into from
Jan 5, 2023

Conversation

igorescodro
Copy link
Owner

A new class called AppCoroutineScope was created to wrap the functionality of a CoroutineScope without exposing the .cancel() API. Since this scope should match the lifecycle of the App, it should never be cancelled. This class will avoid human error by not exposing the function to cancel the scope.

The decision to create a wrapper instead of implementing the CoroutineScope interface was because the .cancel() function is not part of the interface itself, it's an extension function. Due to the Liskov Substitution Principle, it would be possible to attribute a CustomCoroutineScope in a CoroutineScope interface, making it possible to access the .cancel() function even if it was implemented in the custom one.

For the moment, only the .launch() function is exposed. If other functions (such as .async()) are needed then future changes will add them.

A new class called `AppCoroutineScope` was created to wrap the
functionality of a `CoroutineScope` without exposing the `.cancel()`
API. Since this scope should match the lifecycle of the App, it should
never be cancelled. This class will avoid human error by not exposing
the function to cancel the scope.

The decision to create a wrapper instead of implementing the
`CoroutineScope` interface was because the `.cancel()` function is not
part of the interface itself, it's an extension function. Due to the
Liskov Substitution Principle, it would be possible to attribute a
`CustomCoroutineScope` in a `CoroutineScope` interface, making it
possible to access the `.cancel()` function even if it was implemented
in the custom one.

For the moment, only the `.launch()` function is exposed. If other
functions (such as `.async()`) is needed, than future changes will add
them.
@igorescodro igorescodro merged commit 19260b5 into main Jan 5, 2023
@igorescodro igorescodro deleted the custom-scope branch January 5, 2023 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant