Synchronizing TS Objects with functions attached?
I have TS Objects with functions (as part of a browser game).
Is there a simple way or library to take an object, strip it of its functions, send over the data, and rebuild it into a full object with functions attached.
My current best idea is to use a sort of (de-)serialization middleware between Redux/Zustand and liveblocks.
Any hints/tricks/best practices/libraries are welcome.
10 Replies
How would you rebuild it if you're not sending the functions?
If that works, I'm happy to do that actually, but the synchronization library I have looked at only send JSON of the state and I implicitly assumed there's no way to send the functions as well.
Based on that I thought the best idea would be to have both clients agree on what the objects' function should look like and deconstruct/reconstruct the objects that way.
Yup that's an option
Or restructure the objects to just be pure data
Using redux implies your storing the functions in redux?
So story time: I actually started this project with useState / context only and objects with functions, then got to too much state and plugged in redux for the state management mostly because it's proven and has nice-ish debug tools. Then I got errors in redux which made me strip out the functions and put them into util files. Then I plugged in liveblocks and was hugely rewarded by effortless state sync.
But now I hate the syntax of the util functions so much that I'm looking for options to go back to objects with functions, so I've toyed around with Zustand instead since it's also kinda liveblocks compatible.
https://t3ttt.vercel.app/
Create T3TTT App
Generated by create-t3-app
Zustand feels nice and does what I want as a state manager, but the liveblocks integration wasn't that great, i.e. led to that function stripping issue.
tl;dr: started with objects + functions, stripped out functions to have pure data and gain performance, miss my old DX, looking for options
Have you tried writing your own redux/zustand?
At that point, I might as well just write a backend server that can verify the game logic as well.
I mean it's an option:p
It's kinda an inevitability but I had hoped I can make it through alpha without worrying about testers cheating the fact that game logic lives client side
just looking for good dx in my prototype app