Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Bug 1876567 - Fix Middleware Issue with Processing Translations States #5302

Merged
merged 3 commits into from
Jan 25, 2024

Conversation

ohall-m
Copy link
Contributor

@ohall-m ohall-m commented Jan 25, 2024

This patch corrects an issue where isTranslateProcessing and isRestoreProcessing states were not updating due to not sending along pre-process actions in EngineDelegateMiddleware.

Pull Request checklist

  • Quality: This PR builds and passes detekt/ktlint checks (A pre-push hook is recommended)
  • Tests: This PR includes thorough tests or an explanation of why it does not
  • Changelog: This PR includes a changelog entry or does not need one
  • Accessibility: The code in this PR follows accessibility best practices or does not include any user facing features

After merge

  • Breaking Changes: If this is a breaking Android Components change, please push a draft PR on Reference Browser to address the breaking issues.

To download an APK when reviewing a PR (after all CI tasks finished running):

  1. Click on Checks at the top of the PR page.
  2. Click on the firefoxci-taskcluster group on the left to expand all tasks.
  3. Click on the build-apk-{fenix,focus,klar}-debug task you're interested in.
  4. Click on View task in Taskcluster in the new DETAILS section.
  5. The APK links should be on the right side of the screen, named for each CPU architecture.

GitHub Automation

https://bugzilla.mozilla.org/show_bug.cgi?id=1876567

Comment on lines 47 to 48
is TranslationsAction.TranslateAction -> translate(context.store, next, action)
is TranslationsAction.TranslateRestoreAction -> translateRestoreOriginal(context.store, next, action)
Copy link
Member

Choose a reason for hiding this comment

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

Could we reorder these TranslationsAction to be after the EngineAction? Just nice to keep all the EngineAction altogether.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, updated!

),
)

assertEquals(false, store.state.findTab(tab.id)?.translationsState?.isTranslateProcessing)
Copy link
Member

Choose a reason for hiding this comment

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

Use assertFalse. Same thing 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.

Updated, is it okay to force unwrap in a test?

Copy link
Contributor

Choose a reason for hiding this comment

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

Definitely. The test will just fail anyway if it crashes

store.waitUntilIdle()

verify(engineSession).requestTranslate(any(), any(), any())
assertEquals(true, store.state.findTab(tab.id)?.translationsState?.isTranslateProcessing)
Copy link
Member

Choose a reason for hiding this comment

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

Use assertTrue. Same thing 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.

Updated

is TranslationsAction.TranslateAction -> translate(context.store, action)
is TranslationsAction.TranslateRestoreAction -> translateRestoreOriginal(context.store, action)
is TranslationsAction.TranslateAction -> translate(context.store, next, action)
is TranslationsAction.TranslateRestoreAction -> translateRestoreOriginal(context.store, next, action)
is EngineAction.ClearDataAction -> clearData(context.store, action)
is EngineAction.PurgeHistoryAction -> purgeHistory(context.state)
else -> next(action)
Copy link
Member

@gabrielluong gabrielluong Jan 25, 2024

Choose a reason for hiding this comment

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

It's possible that this else should just be a no-op and we should always just be calling next(action). What do you think @MatthewTighe?

We don't have to make this change here in this PR, but might be worth a follow up bug.

Copy link
Contributor

@MatthewTighe MatthewTighe Jan 25, 2024

Choose a reason for hiding this comment

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

It looks generally like this Middleware intercepts actions and then dispatches a different action to complete the reducer chain. Since this is pre-existing, core browser functionality I would hesitate to update the existing code without an investigation.

Visually, it might make the distinction between handling more clear and quickly communicated to readers to write the translate actions like:

is TranslationsAction.TranslateAction -> {
    next(action)
    translate(context.store, action)
}
is TranslationsAction.TranslateRestoreAction -> {
    next(action)
    translateRestoreOriginal(context.store, action)
}

One other thing to note is that the context.store reference in Middleware::invoke will actually update as a result of the reducer chain, so the state will be different after calling next. If you want the original state, you can capture it before calling next or handling the middleware work first (if it won't slow the rest of the chain noticeably)

For example:

is TranslationsAction.TranslateAction -> {
    val currentState = context.store.state
    next(action)
    translate(currentState, action)
}
// or 
is TranslationsAction.TranslateRestoreAction -> {
    translateRestoreOriginal(context.store, action)
    next(action)
}

Copy link
Member

@gabrielluong gabrielluong Jan 25, 2024

Choose a reason for hiding this comment

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

+1 Definitely worth a followup investigation for adding the next(action) outside of the else. No action needed here in the PR, but let's file a followup bug to keep track of this and link the description to the comments here.

+1 to add the next(action) inside of the is TranslationsAction.TranslateAction -> {} instead of the translate() function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One other thing to note is that the context.store reference in Middleware::invoke will actually update as a result of the reducer chain, so the state will be different after calling next. If you want the original state, you can capture it before calling next or handling the middleware work first (if it won't slow the rest of the chain noticeably)

This is my intention:

  • action(next) switches in inProgress on the store
  • translate/restore will remove inProgress when complete

I think I probably want translate/restore to use the updated/most current store, is that right?

Made bug 1876598. Please add to the bug or let me know if I misunderstood anything in the filing.

This patch corrects an issue where `isTranslateProcessing` and
`isRestoreProcessing` states were not updating due to not sending along
pre-process actions in `EngineDelegateMiddleware`.
@github-actions github-actions bot added approved PR that has been approved and removed 🕵️‍♀️ needs review PRs that need to be reviewed labels Jan 25, 2024
@ohall-m ohall-m added the 🛬 needs landing PRs that are ready to land label Jan 25, 2024
@ohall-m ohall-m closed this Jan 25, 2024
@ohall-m ohall-m reopened this Jan 25, 2024
@ohall-m ohall-m closed this Jan 25, 2024
@ohall-m ohall-m reopened this Jan 25, 2024
@ohall-m ohall-m closed this Jan 25, 2024
@ohall-m ohall-m reopened this Jan 25, 2024
@mergify mergify bot merged commit 8f6bc46 into mozilla-mobile:main Jan 25, 2024
114 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved PR that has been approved 🛬 needs landing PRs that are ready to land
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants