easy-peasy icon indicating copy to clipboard operation
easy-peasy copied to clipboard

Computed properties with a runtime argument missing when used with persistence?

Open GollyJer opened this issue 3 years ago • 5 comments

With persistence turned on our app throws Error in useStoreState: state.assets.getStakedAssetsByAddress is not a function..

It is a computed property with a runtime argument.

getStakedAssetsByAddress: computed(
  (state) => (address) => Object.values(state.stakedAssetsById[address]) || []
),

This exact issue was tackled in v4. https://github.com/ctrlplusb/easy-peasy/issues/503 Could there be a regression in v5?

GollyJer avatar Aug 04 '21 04:08 GollyJer

For a anyone following along I hacked around this issue with a most supremely placed try/catch.

const ownedAssets = useStoreState((state) => {
  try {
    return state.assets.getStakedAssetsByAddress(theAddress);
  } catch {
    return [];
  }
});

GollyJer avatar Aug 21 '21 20:08 GollyJer

Hi @GollyJer

I tried updating the codesandbox in the related issue to the latest easy peasy but I didn't experience issues. Could you provide me a minimal reproduction in codesandbox?

ctrlplusb avatar Aug 23 '21 07:08 ctrlplusb

Hi @ctrlplusb thanks for the response.

We're using Expo so I'll put together an example on snack.expo.dev and post here when ready.

GollyJer avatar Aug 24 '21 03:08 GollyJer

OK. I'm back with the example @ctrlplusb.

Here's the snack. easy-peasy-persist-computed-bug

I set the store up the same way we do our production app. You will notice we build a pretty complex import structure for the store, but it's easier to manage in the long run.

  • Pick the Android emulator.
  • When you initially load the app, with nothing persisted, it works great.
  • Then comment lines 15-23 and uncomment lines 25 - 33, hit the Save button at the top right, and reload the app.
  • You will get state.thing1.getAllTheThings is not a function.
  • Now, pick the iOS emulator.
  • App will load fine the first time.
  • Click Devices > Reload Apps at the bottom right menu of snack.
  • You will get state.thing1.getAllTheThings is not a function.
  • I think this has something to do with persistence because there isn't persisted state when the app initially loads (no error), but there is from that point forward (with error).

And if you're wondering why I'm not using Easy Peasy's persistence. 👉 https://github.com/ctrlplusb/easy-peasy/issues/672 Maybe related?

GollyJer avatar Aug 25 '21 05:08 GollyJer

Thanks for taking the time to put this together for me.

ctrlplusb avatar Sep 01 '21 14:09 ctrlplusb

OK. I'm back with the example @ctrlplusb.

Here's the snack. easy-peasy-persist-computed-bug

I set the store up the same way we do our production app. You will notice we build a pretty complex import structure for the store, but it's easier to manage in the long run.

  • Pick the Android emulator.
  • When you initially load the app, with nothing persisted, it works great.
  • Then comment lines 15-23 and uncomment lines 25 - 33, hit the Save button at the top right, and reload the app.
  • You will get state.thing1.getAllTheThings is not a function.
  • Now, pick the iOS emulator.
  • App will load fine the first time.
  • Click Devices > Reload Apps at the bottom right menu of snack.
  • You will get state.thing1.getAllTheThings is not a function.
  • I think this has something to do with persistence because there isn't persisted state when the app initially loads (no error), but there is from that point forward (with error).

And if you're wondering why I'm not using Easy Peasy's persistence. 👉 #672 Maybe related?

Following the steps - but I'm not able to reproduce the issue (using v6.0.1).

Can you verify that the issue is still present using the latest version, @GollyJer ?

jmyrland avatar Jun 14 '23 15:06 jmyrland

Yeah. This appears to be fixed in the new version. Closing. 💪

GollyJer avatar Jun 16 '23 22:06 GollyJer