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

Convert StateService to suspend functions #2500

Merged
merged 7 commits into from
Dec 11, 2020

Conversation

aqulu
Copy link
Contributor

@aqulu aqulu commented Dec 8, 2020

Signed-off-by: aqulu [email protected]

Pull Request Checklist

  • Changes has been tested on an Android device or Android emulator with API 21
  • UI change has been tested on both light and dark themes
  • Pull request is based on the develop branch
  • Pull request updates CHANGES.md
  • Pull request includes screenshots or videos if containing UI changes
  • Pull request includes a sign off

Summary

  • Converted StateService to suspendable functions [SDK] Migrate Services API from MatrixCallback mode to coroutines #2449
  • RxRoom launches suspendable functions using kotlinx-coroutines-rx2 rxCompletable
    • rxCompletable internally launches the suspendable function on GlobalScope. The job will be cancelled when the resulting Completable is, which is similar to completableBuilder's behavior in matrix-sdk-android-rx
  • WidgetPostAPIHandler uses GlobalScope.launch to run suspendable functions
    • Attention: Directly using GlobalScope is discouraged.
    • As WidgetPostAPIHandler is not tied to any lifecycle. Using a new CoroutineScope instance does not solve the issue, as running jobs would not be cancelled by lifecycle events.
      • WidgetPostAPIHandler seems to be used only by WidgetViewModel so it could potentially be tied to it's lifecycle to avoid using GlobalScope.
    • Prior to these changes, the function calls were not constrained either, thus using GlobalScope seems appropriate.
      • StateService internally used TaskExecutor's coroutine scope. All jobs in this scope will be cancelled when CacheService.clearCache is invoked. As far as i understand this is a side-effect of cache clearing rather than it's primary goal.

@bmarty bmarty requested a review from ganfra December 8, 2020 11:37
Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR.
I got just one question, and I will be more comfortable if @ganfra can have a look on the change regarding the Widget part.
Also the CI is not happy, could you fix that please?
Thanks!

.executeBy(taskExecutor)
body: JsonDict
) {
withContext(coroutineDispatchers.main) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why changing the context here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, didn't read executeBy(taskExecutor) internals properly.
TaskExecutor.execute ran this on the callback's thread (by transforming it with toDispatcher() internally). I'll remove the context change.

override fun updateJoinRule(joinRules: RoomJoinRules?, guestAccess: GuestAccess?, callback: MatrixCallback<Unit>): Cancelable {
return taskExecutor.executorScope.launchToCallback(coroutineDispatchers.main, callback) {
override suspend fun updateJoinRule(joinRules: RoomJoinRules?, guestAccess: GuestAccess?) {
withContext(coroutineDispatchers.main) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was launched this in the coroutineDispatchers.main context by taskExecutor.executorScope.launchToCallback(coroutineDispatchers.main, callback) before.
I'm happy to remove the explicit withContext though.

override fun updateAvatar(avatarUri: Uri, fileName: String, callback: MatrixCallback<Unit>): Cancelable {
return taskExecutor.executorScope.launchToCallback(coroutineDispatchers.main, callback) {
override suspend fun updateAvatar(avatarUri: Uri, fileName: String) {
withContext(coroutineDispatchers.main) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aqulu
Copy link
Contributor Author

aqulu commented Dec 8, 2020

Thanks for the review!

Also the CI is not happy, could you fix that please?

I'll fix the CI errors and get rid of the context-changes you pointed out.

@aqulu
Copy link
Contributor Author

aqulu commented Dec 8, 2020

CI should be fixed now. Looks like buildkite's "Compile and run Unit tests" job hung up - executing the command on my local machine completes successfully.

stateKey = null,
body = params
)
widgetPostAPIMediator.sendSuccess(eventData)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be done inside the try block? If it throws, sendError will also be called, which isn't strictly the previous behaviour.
(Same for below)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out! Fixed in ed822be

@Dominaezzz
Copy link
Contributor

I've been quite busy lately and haven't been able to contribute to this, but I wasn't expecting anybody else to do these.
Might be worth "reserving" each service in the tracking issue so no one duplicates work because these can be annoying enough to untangle and maintain previous behaviour, don't want my (or anyone else's) work to be made redundant 😭.

Also, feel free to tag me to review these.

@aqulu
Copy link
Contributor Author

aqulu commented Dec 8, 2020

I've been quite busy lately and haven't been able to contribute to this, but I wasn't expecting anybody else to do these.

@Dominaezzz Sorry for interfering with these, that wasn't my intention 🙇
I'll hold off from the other converting tasks, as it might be more consistent if done by the same person.

@Dominaezzz
Copy link
Contributor

Oh no! 😨 Don't stop on my behalf! It would be amazing to have these done sooner than later aha.
I don't currently have a lot of time, so it would be awesome if you continued.
Consistency can be solved with reviews but I don't think there's much to worry about there anyway.

Looking forward to your next PR! 🎉 😀

Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now, thanks for the update!
It seems that we have touch the StateService. Would you mind to rebase your change on develop, or merge develop to your branch?

@bmarty
Copy link
Member

bmarty commented Dec 11, 2020

Actually I think I can do it with Github UI

@bmarty bmarty merged commit 3eba43f into element-hq:develop Dec 11, 2020
@aqulu aqulu deleted the feature/state_service_coroutines branch December 12, 2020 00:13
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.

3 participants