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

How to delete records from a model referenced by a different model?

Open mfbx9da4 opened this issue 4 years ago • 1 comments

I have two models, where model A references items in model B. When I delete items from model B, react rerenders before I can update model A to also delete the referenced items.

Example:

I have a feed. The feed is made up of different types of items. Each type of item is stored in its respective model. The feed order is stored as an array of item IDs. The feed order is also stored in it's own model. The feed order references items in their different substores.

I have made a minimal failing example here. The issue occurs after clicking the button three times

https://codesandbox.io/s/deleting-referenced-items-kmw1e

The key line is

else console.error(">>>> feed order contains deleted item", id);

It's problematic that the feed order might contain deleted items because it could mean there is a programming error that resulted in bad references. In this case it's not a programming error, the second store just hasn't updated yet.

Is there a way I might be able to batch the createAndDeleteTodo, to not evaluate all listeners until the entire thunk and all subthunks have completed?

In the above example it's trivial enough to just have one master action which updates the feed order and the items but it would feel cumbersome if there was more than just one type of item as each item type lives in it's own respective model. Would this be the recommended strategy?

mfbx9da4 avatar Mar 08 '21 13:03 mfbx9da4

Sorry to bother you. Would really appreciate some insight here on a recommended approach.

I've looked into this batching thunk issue but it doesn't seem to have any effect on the above issue.

https://github.com/ctrlplusb/easy-peasy/issues/119

Thanks for the great lib 🥇

mfbx9da4 avatar Mar 24 '21 14:03 mfbx9da4

Through example! This seems like a tricky issue.. Did you find a solution for this?

jmyrland avatar Sep 16 '22 18:09 jmyrland

I'm no longer working on the easy-peasy project but the solution is clear... Have a master action for writing inter-dependant data.

mfbx9da4 avatar Sep 16 '22 18:09 mfbx9da4