keajs / kea-localstorage

Persist the state of your reducers in localstorage
https://kea.js.org/plugins/localstorage
MIT License
9 stars 3 forks source link

Fix unexpected error message of path when using logic.extend() #12

Closed kairyu closed 4 years ago

kairyu commented 4 years ago

First of all, thank you for the great plugin!

Recently I tried to use kea-localstorage and kea-router together in a project, and I found I kept getting the error "Logic store must have a path specified in order to persist reducer values" from console even if I have exactly defined the path as well.

I have written a minimal working example which can reproduce this issue: https://github.com/kairyu/kea-localstorage-example

At the end of the day, I found it seems that because kea-router uses logic.extend() to extend the router logic which has no path specified, the extended logic will also be passed into the buildStep of kea-localstorage as input, and then it causes the error message.

So I thought of a quick fix which just simply change the input.path to logic.path. But I'm not sure does it really make sense. It would be great if you could get any chance to review this. Thank you in advance.

mariusandra commented 4 years ago

Hi! Thanks for raising the issue!

The fix wasn't the right one unfortunately, as logic.path exists for every logic, including those that didn't have a path explicitly set. So I changed it to check if there is either no input.path or if logic.path starts with kea.inline.. In that case there was no path explicitly set.

There was another and probably bigger issue though. On logic.extend, the plugin was saving into localstorage all the reducers for a second time. I fixed that by adding code that caches if a reducer has already been persisted or not.

All of that is now out with v1.0.1 :)

Thanks again for finding, documenting and trying to fix the bug! 👍

kairyu commented 4 years ago

Thank you very much for looking into the issue and figuring out the proper solution in short time! I'm looking forward to the releasing of the fixed code.

mariusandra commented 4 years ago

Hey, 1.0.1 is already released with the patch. I now also pushed the updated code to github, sorry it got blocked before!