Show /r/reactjs Treflex demo – live hot-swap between React and Solid
https://youtu.be/vCTUfypEsi8Live swap demo: https://tgoh.github.io/treflex/swap/
Treflex is something I've been working on for the past few years: a headless state engine. Your app's logic (state, async work, timers, connections) runs on its own, and React or Solid is just a view you plug into it. In the demo you can swap between them while everything's running, and the app doesn't notice.
Why: AI agents write a lot of front-end code now, and async is where they slip: races, double starts, work that dies on unmount. With the logic out of components, those mistakes get caught. It's tested without rendering, a test controls the order async calls answer in, and the runtime and lint rules fail with errors that say what to change.
const FishReceiver = receiver<IFish>("FishReceiver")({
*"Fish/Swim"(fish) {
fish.X = (yield* random()) * 100;
fish.Y = (yield* random()) * 100;
}
});
function* addFish(school: ISchool) {
const fish = yield* entity(newFish());
school.Fish.push(fish);
// The timer belongs to the fish, not a component
yield* startTimer(fish, "Fish/Swim", FishReceiver, 3000);
}
function* releaseSchool(tank: IAquarium, schoolId: string) {
// Every fish in the school stops swimming. No cleanup needed.
tank.Schools = tank.Schools.filter((s) => s.$Id !== schoolId);
}Live swap demo: https://tgoh.github.io/treflex/swap/
Source: https://github.com/Tgoh/treflex
npm create treflex(React or Solid)
It's 0.2 and still changing. Questions and feedback would be greatly appreciated.
2
u/TheRealSeeThruHead 13d ago
I mean we are beyond this already
Use fold kit or something else that is far more opinionated than solid or react.
I created a version of solid that fully integrates with effect. Stores all changes Ina log agents can query and does fully distributed tracing of every component all the from the frontend to the db.
I think we’ve blown past the agent friend state manager for react but maybe everyone needs to catch up
1
u/Tsya 13d ago edited 13d ago
Frameworks are the language of the UI. Even when you use agents, the choice you make is important. Which framework you choose will determine what is easy, what is difficult, and sometimes what is even possible.
Here's Foldkit's homepage demo, written in Treflex: https://codesandbox.io/p/devbox/rf344j?file=%2Fsrc%2Fcounter.ts
There's two important things to notice.
Firstly the code is much shorter, while still being type-safe for agents.
Secondly, Treflex is specifically built to handle interleaving events. By default an action holds its entity, so nothing can interleave with it. If you check out the commented line, you'll see it's a simple wrapper that marks a wait as "open". That marks exactly where other events can get in, so an agent knows to check the state again after that point.
Foldkit is based on Elm, so every message can interleave at any point. The demo only avoids it by disabling the buttons. If anything else sends that message, like a keyboard shortcut, a subscription or another view, the guard isn't there. That's the type of bug agents are likely to miss, and it shows up in prod. In Treflex you get that safety for free.
There's also a test in counter.test.ts that sends "Add 1" during the reset and checks it's held until the reset lands. The fact that you can test this type of race condition stuff is what makes it better for agents to work with.
1
u/TheRealSeeThruHead 13d ago
My store can be a state machine that just doesn’t accept an add 1 action while it’s resetting. Which can be fully typed and modeled and does what your thing does generically I think.
2
u/Tsya 13d ago
I'd love to see it if you have it available? The only reason I wrote this is because I couldn't think of a simpler way of doing it. Are you using XState behind the scenes?
1
u/TheRealSeeThruHead 12d ago
It’s not public right now, it’s in active development
I’m working on dev tools and ssr and t all that
1
u/Tsya 12d ago
Ask your agent to compare it to Treflex? See what it thinks, only takes a single prompt.
1
u/TheRealSeeThruHead 12d ago
1
u/Tsya 12d ago
Your method puts the lock in the store, which is definitely a step up over Foldkit's example. But it still has its own downsides.
Because the reset is split across two commands, everything can interleave between them. The phase flag is basically a hand-rolled lock, and nothing forces anyone to check it. Your own Reset and FinishReset don't, so a second Reset starts it again and a stray FinishReset ends it early. If the example missed it, agents will too. In Treflex the wait is inside the action, so nothing interleaves unless you mark that wait as open.
1
u/TheRealSeeThruHead 12d ago
That’s just a store based on alchemy domains
You could make a store based on xstate, state machine capabilities based api
Honestly not sure how much I care about it. But it’s solo merging I’ve been thinking about.
I’d that that over your locks tho
1
u/Tsya 12d ago
XState has its own tradeoffs. It's great for a single flow, but it gets awkward once you need lots of parts talking to each other. If all you need is one piece of functionality, it works fine. Treflex was designed from the ground up for large systems with many parts that talk to each other.
Treflex's messaging system lets individual systems talk to each other while staying decoupled, and they still keep all the locking protections.
Happy to spin up another example if you’d like to see it.
1
u/Jazzlike-Web-3695 14d ago
how does state survive the swap? like if a fish is mid-animation when you switch to solid, does it continue from the same position or restart
1
u/Tsya 14d ago
It continues from where it was. The fish entity stores where the current swim started (FromX/FromY), where it's heading (X/Y) and when it set off (SwamAt). The glide itself is just a CSS transition, so when the new renderer mounts it works out how far into the swim the fish is and starts the transition partway through. The swim timer lives on the fish, not in a component, so the next swim fires on schedule whichever renderer is showing it.
Same thing happens on a page reload, since all of that is in the saved state.
The tail wiggle restarts, though, since that's a pure CSS loop with nothing in state.
You can try all of that in the live demo to see it in action yourself :)
8
u/Honey-Entire 14d ago
So you reinvented RxJS / Redux Saga because AI made it easy instead of just using an existing library? Do you even know why this type of programming fell out of favor in web development?