Using SolidJS's produce(), but with Immer-style patch generation?
Is it possible to use SolidJS's
produce
, but with patch generation akin to Immer's produceWithPatches
? (being able to only store the delta (patches), as well as supporting things like classes)
I'm currently using a combination of Immer's produceWithPatches
and reconcile
to work with a state that's made up of nested instances of various classes. The only reason I'm using Immer in the first place is to get the undo/redo functionality with little fuss, but it doesn't play well with SolidJS, and is slow and results in unnecessary re-renders.6 Replies
I believe you can just use the same function that you get in
produce
and run it through both immer and solid separately?i think solid primitives has something just for this, along with a separate undo/redo package
https://primitives.solidjs.community/package/deep#capturestoreupdates
Solid Primitives
A library of high-quality primitives that extend SolidJS reactivity
Immer has copy-on-write, so if I do something like this:
immerProduceWithPatches
will generate patches, but it will not be changed on the underlying s
object that Solid's produce
(solidProduce
here) gives you. Even applyPatches
is copy-on-write, so it has the same issue.
I'm messing around with that right now, but SolidJS stores in general don't seem to support reactivity with classes.
Wrapping things in createMutable
does help, but the granularity is lost; updating a value in a class causes captureStoreUpdates
to return an instance of the class, instead of just the one property.
Example:
When running store.arr[1].someVal = 0;
, the path of the delta that gets generated is ['arr', 1]
, instead of the expected ['arr', 1, 'someVal']
. It stores an instance of the Subclass
class instead of just a number.
Also, when you have nested createMutable
s, calling unwrap
only unwraps the top-most value, and structuredClone
still fails if any sub-properties aren't unwrapped.
After re-reading this, I got the idea of re-implementing Immer's applyPatches
, but in a mutative way, which solved my issue.Release as a package, I might need it soon lol
Oh, it's certainly not production ready. It doesn't support
Map
s or Set
s, since my project doesn't use those, but here you go:
I ended up cleaning solid store's proxy before passing it to immer's produce/produceWithPatches: