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

Reapply onyx upgrade use cache with fixes #24041

Merged

Conversation

hannojg
Copy link
Contributor

@hannojg hannojg commented Aug 2, 2023

Details

Recently we tried to merge this performance improvement PR:

but it caused a bunch of regressions. This PR is reapplying the changes + fixes the found regressions

Fixes:

I asked @Beamanator if we can run a full regression test suite, as this PR includes very heavy onyx changes!

Fixed Issues

$ #22644
PROPOSAL:

Tests

  • Test the reproduction steps from the issues and confirm that the bugs aren't happening
  • Verify that no errors appear in the JS console

Offline tests

n/a

QA Steps

Same as testing

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
Screen.Recording.2023-08-02.at.14.18.54.mov
Mobile Web - Chrome
mweb-Android.mov
Mobile Web - Safari
mWeb-ios.mp4
Desktop

Uploading desktop.mov…

iOS
ios.mp4
Android
Screen.Recording.2023-08-02.at.14.27.25.mov

@hannojg hannojg force-pushed the reapply-onyx-upgrade-use-cache-with-fixes branch from ceb3aad to bcc91e6 Compare August 2, 2023 11:53
@hannojg hannojg force-pushed the reapply-onyx-upgrade-use-cache-with-fixes branch from bcc91e6 to cd62597 Compare August 2, 2023 12:06
@melvin-bot
Copy link

melvin-bot bot commented Aug 2, 2023

Hey! I see that you made changes to our Form component. Make sure to update the docs in FORMS.md accordingly. Cheers!

@hannojg
Copy link
Contributor Author

hannojg commented Aug 2, 2023

Just as a side note: the code of the initial IOU onyx values has been 2 years old, i tried to check every use case and it seems to be safe to be removed now

@hannojg
Copy link
Contributor Author

hannojg commented Aug 4, 2023

@hannojg hannojg changed the title [WIP] Reapply onyx upgrade use cache with fixes Reapply onyx upgrade use cache with fixes Aug 7, 2023
@hannojg hannojg marked this pull request as ready for review August 7, 2023 15:35
@hannojg hannojg requested a review from a team as a code owner August 7, 2023 15:35
@melvin-bot melvin-bot bot requested review from mollfpr and removed request for a team August 7, 2023 15:36
@melvin-bot
Copy link

melvin-bot bot commented Aug 7, 2023

@mollfpr Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@hannojg hannojg marked this pull request as draft August 7, 2023 15:37
@hannojg hannojg marked this pull request as ready for review August 7, 2023 15:39
@hayata-suenaga hayata-suenaga mentioned this pull request Aug 7, 2023
56 tasks
@mvtglobally
Copy link

We are seeing issue #23962 on this build. It is fixed in Staging and PROD. Should we log it again?

@AndrewGable
Copy link
Contributor

@mvtglobally - I believe @hannojg fixed this, we are rebuilding a new build now with the fix.

@mvtglobally
Copy link

@AndrewGable so it was a bug?
Also, will we need to start from scratch on a new build? We are about ~30% complete

@mvtglobally
Copy link

@hannojg regression is completed.
Should we just retest the issues logged this round?

@hannojg
Copy link
Contributor Author

hannojg commented Aug 15, 2023

Thanks for letting me know and for testing! I still have to fix the last issue. Will let you know when it's good for testing (probably in the next few hours!)

@mountiny
Copy link
Contributor

Triggered a new build

@OSBotify
Copy link
Contributor

@hannojg
Copy link
Contributor Author

hannojg commented Aug 15, 2023

@mvtglobally The new builds are ready, can your team please test again? Thanks!

@hannojg
Copy link
Contributor Author

hannojg commented Aug 15, 2023

@mollfpr can you check as well please?

@mountiny
Copy link
Contributor

Asked QA for a review

@mountiny
Copy link
Contributor

@situchan have you been able to check this one out?

@situchan
Copy link
Contributor

on it now

Copy link
Contributor

@situchan situchan left a comment

Choose a reason for hiding this comment

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

Looks good so far.

Comment on lines -122 to -124
if (isEmojiPickerVisible) {
Keyboard.dismiss();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What issue was fixed by this removal?

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 one #24041 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

This was originally added in #8221 to fix emoji select on first tap not working on android.
I confirmed that's not reproducible after revert.

8.mov

Copy link
Contributor

@ntdiary ntdiary Aug 24, 2023

Choose a reason for hiding this comment

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

Hi, in my test, after removing this code, the keyboard no longer appears when dismissing the emoji modal on Android native.

test.mp4

How about moving Keyboard.dismiss() into the onPress handler in EmojiPickerButton? This could fix #24041 (comment) without impacting Android platform.

onPress={() => {
if (!EmojiPickerAction.emojiPickerRef.current.isEmojiPickerVisible) {
EmojiPickerAction.showEmojiPicker(props.onModalHide, props.onEmojiSelected, emojiPopoverAnchor.current, undefined, () => {}, props.reportAction);
} else {
EmojiPickerAction.emojiPickerRef.current.hideEmojiPicker();
}
}}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @ntdiary

You are saying

the keyboard no longer appears

The code here is about dismissing the keyboard. I'd never expect this code to open the keyboard?

Copy link
Contributor

@ntdiary ntdiary Aug 24, 2023

Choose a reason for hiding this comment

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

image

Ah, this may seem a bit unbelievable 😂, but let me explain a bit more: as shown in the image above, for android, if we don't manually call blur ( or dismiss), the focus state inside the composer input box will not change when opening the emoji modal. Then after we close the modal, even though we call focus on the composer input box, because the internal focus state did not change, the part where the keyboard is supposed to show gets ignored.

So here, dismiss can clean up the internal focus state, allowing the subsequent focus to successfully show the keyboard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh wow, I didn't know that, thanks for sharing!! So you say we should add this code back? 😊

Copy link
Contributor

Choose a reason for hiding this comment

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

I was also surprised when I first discovered this android behavior. And don't worry, I just made a note here, we still have another issue and may do something there as needed. 😄

@@ -339,6 +327,8 @@ class BaseOptionsSelector extends Component {
selectTextOnFocus
blurOnSubmit={Boolean(this.state.allOptions.length)}
spellCheck={false}
autoFocus={this.props.autoFocus}
shouldDelayFocus={this.props.shouldDelayFocus}
Copy link
Contributor

Choose a reason for hiding this comment

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

Confirmed that this fixes Keyboard opens multiple times when selecting members to request money.
Curious about the root cause though?

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 is mostly an order of event issue.
By this we call textInputRef.focus in the useEffect of the BaseTextInput.js component. This call will happen later than a call in componentDidMount. A (simplified) general rule in react is is that componentDidMount will always run before any useEffect hooks in the current batch of work.

Now, when we navigate to a new screen there is some code in react-navigation that will dismiss the keyboard when we transition to a new screen. When the focus happens on the class component componentDidMount the order of events is the following:

  • Transition to a new screen starts
  • Component gets rendered, calls .focus(). The keyboard starts to open
  • Transition causes Keyboard.dismiss to get called
  • (If we are lucky in terms of timing), the timeout of selectTextOnFocus kicks in and focuses the input again (keyboard opens again) [if you are curious check the JS code of the react-native-web textinput component]

Now, we call the focus from the useEffect, and the order of events is roughly this:

  • Transition to a new screen starts
  • Component gets rendered
  • Transition causes Keyboard.dismiss to get called
  • useEffect fires focusing the input

@mountiny mountiny self-requested a review August 15, 2023 17:10
Copy link
Contributor

@situchan situchan left a comment

Choose a reason for hiding this comment

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

Tested all mentioned issues in this PR. All are not reproducible.
Still testing more cases

@@ -83,7 +83,6 @@ const defaultProps = {
isSubmitButtonVisible: true,
formState: {
isLoading: false,
errors: null,
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB: I assume crash was fixed in onyx repo. Now what's this removal for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The crash wasn't fixed in the onyx repo. It is invalid when looking at the logic of the component to have errors as null. It's never expected to be null.
Its a fix for this bug:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't find the exact bug ticket at the moment. I know it was one of the bugs that was found during going through the regressions here: https://expensify.slack.com/archives/C01GTK53T8Q/p1690897222550089

Have to leave for vacation in a few minutes so I can't find it right now, sorry 😓

Copy link
Contributor

Choose a reason for hiding this comment

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

Nvm. Hopefully this PR and #18648 would be merged without further change requests from you. Have a good vacation!

@mvtglobally
Copy link

Looks pass on mWeb. We are checking other platforms https://github.com/Expensify/App/assets/43995119/d28158f7-b4c1-4783-be84-b557628d87a4

@mountiny
Copy link
Contributor

@situchan could you fill out the checklist please?

@situchan
Copy link
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android

Copy link
Contributor

@situchan situchan left a comment

Choose a reason for hiding this comment

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

We did good amount of tests but still there might be edge cases we missed, which will be discovered by our good bug reporters.
After merge, I will keep watching slack channel if any bug is reported related to this PR, so that we can fix quickly before being deploy blocker.

Navigation.navigate(ROUTES.getMoneyRequestRoute(iouType, reportID));
return resetMoneyRequestInfo(`${iouType}${reportID}`).then(() => {
Navigation.navigate(ROUTES.getMoneyRequestRoute(iouType, reportID));
});
Copy link
Contributor

Choose a reason for hiding this comment

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

It's pretty uncommon to have to wait for a merge to navigate. If we are going to chain off of Onyx.merge() I think we should leave some kind of comment to explain why this is the exception to the rule.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the problem here is that now when we have withOnyx improvement we may sometimes read outdate value. For instance we have a screen that operates on value "X". Let say we had a flow that first modifies the value "X" then navigates to our screen. Before what was happening is withOnyx wrapped screen was waiting for onyx values by waiting for connect method and then info about value "X". in connect method there were a lot of promises so we were sure that our modification was succeeded before we actually rendered content of the new screen. Now it's possible that we will modify the "X" navigate to screen read previous value of "X" from cache then once modification takes place we render the component one more time.

Copy link
Contributor

Choose a reason for hiding this comment

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

waiting for merge prevents reading outdate value from what I understand

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can easily miss some cases as previously we didn't need to care about it as we were sure that we won't read from onyx before the modification happen.
Maybe we should persist what is stale and don't read from cache when it is or indicate that we don't worry about reading outdated data.

Copy link
Contributor

@Szymon20000 Szymon20000 Aug 16, 2023

Choose a reason for hiding this comment

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

Maybe we can do it in a bit safer way (but you better now how many such cases we can have in the app. Maybe those from the pr are the only ones)
If we don't mind reading outdated data (want to show messages as soon as possible we can add a flag)

withOnyx({
  allowReadingStaleData: true
})

Then on every merge/set/remove we update two simple maps so we know if the data in cache is up to date
StaleStorageLastOperation[key] = operation_number_that_modifies_the_key
toDoOperations.push(operationId)
Then
isDataStale = (key) => (toDoOperation.has(StaleStorageLastOperation[key])).

if the data is stale we don't get it from cache in withOnyx wrapper unless someone set allowReadingStaleData to true

}
Navigation.navigate(ROUTES.getMoneyRequestConfirmationRoute(iouType, reportID));
Promise.all(promises).then(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar comment here. Pushing all these merge promises into the array has made this code harder to reason about I think. We should document every reason why we will need to await an Onyx.merge() call.

src/libs/actions/QueuedOnyxUpdates.js Show resolved Hide resolved
@sophiepintoraetz
Copy link
Contributor

Can someone clarify for me what further action is happening here? I have an issue that's waiting on this PR deploying.

@mountiny
Copy link
Contributor

@sophiepintoraetz seems like the automation here failed, but it should be deployed now so you can take your issue off hold

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.