App icon indicating copy to clipboard operation
App copied to clipboard

[hold for payment 25 august] Onyx changes break Exisiting App architecture.

Open parasharrajat opened this issue 5 years ago • 58 comments

If you haven’t already, check out our contributing guidelines for onboarding and email [email protected] to request to join our Slack channel!


cc: @marcaaron @kidroca

ISSUE

There few parts that will stop working due to the recent changes to Onyx. We have to either plan for migration or fix Onyx so that those continue working.

There is a component Enablepayment which uses the following implementation.

  1. It disregards the stored values in Onyx for userWallet. https://github.com/Expensify/App/blob/50828ab6752324988fbfdc7020878c80adf174bd/src/pages/EnablePayments/index.js#L50-L58.
  2. We start with a userWallet:{} value.
  3. After the component is mounted we make an API call to get the new data.
  4. But If the data coming from the backend is the same as the stored value in Onyx, there will not be any rerendered.
  5. Due to this, we will never be able to show the real data on the screen.
  6. There will be many instances of this approach in the App and multiple times data will not change but we still want to make a request to check for updated value and show it to the user.

Slack thread => https://expensify.slack.com/archives/C01GTK53T8Q/p1628106963153400

Expected Result:

UI should be updated to show the recent state of data.

Actual Result:

  1. No change in the UI state.

Workaround:

None. It breaks the App.

Platform:

Where is this issue occurring?

  • Web
  • iOS
  • Android
  • Desktop App
  • Mobile Web

Version Number: 1.0.82 Logs: https://stackoverflow.com/c/expensify/questions/4856 Notes/Photos/Videos: Any additional supporting documentation Expensify/Expensify Issue URL:

View all open jobs on Upwork

parasharrajat avatar Aug 07 '21 16:08 parasharrajat

Triggered auto assignment to @arielgreen (AutoAssignerTriage), see https://stackoverflow.com/c/expensify/questions/4749 for more details.

MelvinBot avatar Aug 07 '21 16:08 MelvinBot

My point here is What is an issue with triggering a render even if data is not changed? If user merge(Onyx.merge) or set(Onyx.set) a new data and we should trigger a render.

If we want to block a render then this should be handled in the component level shouldComponentUpdate.

The issue seems to be due to the Object comparison here. https://github.com/Expensify/react-native-onyx/blob/d7553b95e982ab78f6bb2064f6b0549f0ace94c2/lib/Onyx.js#L479

Even if we want to drop a render if data is not changed and then we should only do Instance matching (ob1 === ob2).

parasharrajat avatar Aug 07 '21 16:08 parasharrajat

We're not doing it to skip a render, but to skip re-writing the same data to storage You can add a log point in the code you've linked to see it's often the case we're trying to write the same value

To let components re-render and still skip a write to storage we can move this to the top of the method

    // Optimistically inform subscribers on the next tick
    Promise.resolve().then(() => keyChanged(key, val));

But do we want to raise a "change" event, when storage didn't really change ?


It doesn't seem valid to use something like initWithStoredValue: false to capture loading state

  • what if you're offline - you'll be stuck loading and with a fullscreen loader it might be impossible to go back
  • if the network call fails - you're stuck loading as well

Instead of relying on storage to raise a "change" event, even though it didn't really change, we can capture a loading state

  • either locally in the component state: { isLoading: true }
    • not possible due to current architecture that intentionally hides promises
  • or in the storage data itself - { walletKey1, walletKey2, isRefreshing: true }
    • this is already an existing pattern: Onyx.merge(ONYXKEYS.PLAID_BANK_ACCOUNTS, {loading: true});

It's cleaner and easier to reason about instead of having to explain why the component uses initWithStoedValues

kidroca avatar Aug 09 '21 07:08 kidroca

After analysing initWithStoedValues the component shared in the ticket description is the only component relying on initWithStoredValue for its loading state

initWithStoredValue has 6 usages

1. AdditionalDetailsStep (walletAdditionalDetails)

Has defaultProps and tracks loading state https://github.com/Expensify/App/blob/50828ab6752324988fbfdc7020878c80adf174bd/src/pages/EnablePayments/AdditionalDetailsStep.js#L39-L43

2. EnablePaymentsPage (userWallet)

Discussed in the current issue - does not track loading state but relies on initWithStoedValues and storage to change

3. OnfidoStep (walletOnfidoData)

Has defaultProps and tracks loading state https://github.com/Expensify/App/blob/50828ab6752324988fbfdc7020878c80adf174bd/src/pages/EnablePayments/OnfidoStep.js#L42-L47

4. TermsStep (walletTerms)

Has defaultProps and tracks loading state https://github.com/Expensify/App/blob/50828ab6752324988fbfdc7020878c80adf174bd/src/pages/EnablePayments/TermsStep.js#L31-L35

5. Expensify (updateAvailable)

It's possible to have a problem here, updateAvailable is not read from storage, and the storage value could be true for some reason.
https://github.com/Expensify/App/blob/50828ab6752324988fbfdc7020878c80adf174bd/src/Expensify.js#L53-L63

I don't know why we ignore the storage value of updateAvailable, I guess there's a check that would set it to true at some point. If that's the case it can just be made to always init as false here: https://github.com/Expensify/App/blob/50828ab6752324988fbfdc7020878c80adf174bd/src/components/OnyxProvider.js#L15

6. LHN loading state

https://github.com/Expensify/App/blob/50828ab6752324988fbfdc7020878c80adf174bd/src/libs/actions/App.js#L19-L24

Always treated like false until it loads https://github.com/Expensify/App/blob/50828ab6752324988fbfdc7020878c80adf174bd/src/libs/actions/App.js#L54-L61

BTW it's already initalised with false and initWithStoedValues is not needed https://github.com/Expensify/App/blob/50828ab6752324988fbfdc7020878c80adf174bd/src/components/OnyxProvider.js#L24

kidroca avatar Aug 09 '21 08:08 kidroca

Nice writeup. So what should be done for these cases? I think initWithStoredValues should be considered a special case. Instead of causing write, can't we just update the state and trigger data flow?

It doesn't seem valid to use something like initWithStoredValue: false to capture loading state

I don't tightly couple the usages of initWithStoredValue with capturing loading state. Although, it is used in this way, overall there is a case to consider.

  1. A component initialized with initWithStoredValue: false.
  2. After the API call, we get the same data.
  3. Component does not update??? <= what about this? shouldn't the data be passed down to the component when we call Onyx.set or merge.

parasharrajat avatar Aug 09 '21 11:08 parasharrajat

Great conversation around this and it sounds like it might be worth exploring a deprecation of initWithStoredValues based on @kidroca's insights.

Is there some interim solution we can go with for now? e.g. could we always notify any subscribers that have initWithStoredValues: false to get around this issue and quickly restore the previous behavior?

marcaaron avatar Aug 09 '21 17:08 marcaaron

@marcaaron yeah we can just send the notification without updating the Onyx Store. There are two ways to do this as per the code.

  1. we move the keychanged call to before checking the cache-store and returning early with an empty promise. https://github.com/Expensify/react-native-onyx/blob/d7553b95e982ab78f6bb2064f6b0549f0ace94c2/lib/Onyx.js#L479.

  2. we do the above but pass a parameter to keychanged to only trigger notifications for keys which has initWithStoredValues: false.

It solves the issue without breaking new changes of Onyx.

parasharrajat avatar Aug 09 '21 18:08 parasharrajat

I'm not sure 1 will work - bc we need to set the value before calling keyChanged()?

we do the above but pass a parameter to keychanged to only trigger notifications for keys which has initWithStoredValues: false

So something like this... ?

const shouldCacheNewValue = !cache.hasCacheForKey(key) || !_.isEqual(val, cache.getValue(key));

if (shouldCacheNewValue)
    cache.set(key, val);
}

// Optimistically inform subscribers on the next tick
Promise.resolve().then(() => keyChanged(key, val, shouldCacheNewValue));

Then handle the update to subscribers in keyChanged()?

marcaaron avatar Aug 09 '21 19:08 marcaaron

Oh Yeah. for the other cases, this is necessary. Sorry slipped from my mind.

Then, if shouldCacheNewValue is false, only trigger a notification only for initWithStoredValues: false if we want to skip unnecessary render for all keys.

parasharrajat avatar Aug 09 '21 19:08 parasharrajat

@marcaaron, since you're eng triaging this, can you go ahead and throw External on this when it's ready to be pushed forward?

arielgreen avatar Aug 09 '21 20:08 arielgreen

@arielgreen Seems like a regression though. We usually have whoever caused the regression address it and not create separate tickets - so I'm not sure what to do in this case.

marcaaron avatar Aug 09 '21 20:08 marcaaron

Everything that's needed to achieve the previous behaviour is to move this line to the start of the call

Promise.resolve().then(() => keyChanged(key, val));

It's just a matter of deciding if it's correct to call keysChanged when they actually didn't.

Reverting to the previous behaviour would also degrade performance as it will cause more re-renders, but at least we'll skip writing to storage

kidroca avatar Aug 09 '21 21:08 kidroca

I'm not sure 1 will work - bc we need to set the value before calling keyChanged()?

I don't see a reason for this If you're talking about cache.set it doesn't matter if we update cache or skip the update if the value is already there

kidroca avatar Aug 09 '21 21:08 kidroca

const shouldCacheNewValue = !cache.hasCacheForKey(key) || !_.isEqual(val, cache.getValue(key));

if (shouldCacheNewValue)
    cache.set(key, val);
}

for keys whose value is different from the cache.

parasharrajat avatar Aug 09 '21 21:08 parasharrajat

Reverting to the previous behaviour would also degrade performance as it will cause more re-renders, but at least we'll skip writing to storage

To clarify the suggestion is to only run keysChanged() logic on things that:

  1. Changed
  2. Did not change but the subscriber has initWithStoredValues: false

So I think this just means things that are flagged with initWithStoredValues won't experience the optimization (but very few things use it).

marcaaron avatar Aug 09 '21 21:08 marcaaron

Also, I think if the component is optimized a render should not be big deal. We are already reusing the Onyx subscriptions so resubscribing to WitOnyx should be fast.

parasharrajat avatar Aug 09 '21 22:08 parasharrajat

const shouldCacheNewValue

The problem is not caching the value but writing it to storage - we should not skip caching but the hard write to storage

kidroca avatar Aug 09 '21 22:08 kidroca

I don't see proposals that addresses this:

2. Did not change but the subscriber has initWithStoredValues: false

And I don't think it's possible to address it in set - there's no information regarding the connection that's getting notified

  1. How would you know (inside set) that you should call keysChanged because initWithStoredValues is false ?
  2. What about consecutive calls
    1. A component is configured to initWithStoredValues: false
    2. The fist set is triggering keysChanged even when the value in storage is the same
    3. Consecutive set calls should skip keysChanged if the value in storage didn't change

We should either always skip notifying when storage didn't change or always notify. There's no way to do it just for the dontInitWithLocalState components and then only for their 2nd render

kidroca avatar Aug 09 '21 22:08 kidroca

I think the optimisation updates just revealed a hidden problem - other components with loading state would capture it in Onyx

kidroca avatar Aug 09 '21 22:08 kidroca

Alright, I'd still recommend that we fix this to restore the behavior that has changed. Less concerned with how it is resolved as long as the solution is simple.

marcaaron avatar Aug 09 '21 22:08 marcaaron