Using Aurelia Store what is the best way to copy your state within Actions?

263 Views Asked by At

What are some ways in which to deep clone my state inside an Action?

Possible solutions I’ve seen are Immer.js, JSON.parse(JSON.stringify), or create a recursive function which does a deep clone. I was even thinking of recreating what Redux does and create Reducers and combine them for my entire state but there are reasons which I decided against it. I like in Aurelia store how I can push Actions to the store, inside each Custom Element’s bind() method. I believe this is a better way because it promotes high cohesion and low coupling. Whereas with Redux I had all my Actions and Reducers centralized with the store.

I’ve never used Immer.js and I was wondering what are some Pros and Cons of using it?

I like the idea of using JSON.parse(JSON.stringify) because it ensures my state is serializable at all times. Which will be good if I want to persist my state to some type of storage. It allows me to identify problems early like when I tried to use Set and Map object types and I quickly found out that those don’t serilize/deserialize with the above methods. But I’m wondering if my store grows too large will JSON.parse(JSON.stringify) become too slow?

Then there is the option of just creating a recursive function which copies my state. Has anyone tried this?

Thanks

EDIT - September 26, 2018 I also asked this question on the aurelia discourse forum and received some helpful responses there. https://discourse.aurelia.io/t/using-aurelia-store-what-is-the-best-way-to-copy-your-state-within-actions/

0

There are 0 best solutions below