this.state just like regular state, so your binding callbacks and other methods can read them without any special handling.
The addComputedProp() method
Call this.addComputedProp(name, fn, dependencies) from inside beforeInit() to register a computed property.
The computed value is available immediately after registration via
this.state.<name>.
Dependencies and DOM re-renders
Thedependencies array tells Sprincul which state properties this computed value depends on. When any dependency changes, Sprincul invalidates the computed value and schedules a DOM update for elements bound to it.
Registering computed properties from beforeInit()
You must call addComputedProp() from inside beforeInit(). This is because Sprincul needs the computed property registered before it sets up bindings, and beforeInit() is the hook that runs at that point. Calling addComputedProp() outside beforeInit() — before the core is available — will throw an error.
A full example: Totals
TheTotals model computes a running total from price and qty. Both state properties are initialized in beforeInit(), and the computed total property is registered with both as dependencies. When either input changes, Sprincul recomputes total and updates the bound <span>.
- The event handler updates
this.state.priceorthis.state.qty. - Sprincul detects the dependency change and schedules an update for
total. - On the next animation frame,
showTotalis called with the bound<span>. this.state.totalreturns the freshly computed value (price * qty), and the element’s text is updated.
Batching behavior
Computed property invalidations run in the samerequestAnimationFrame batch as regular state updates. If you change multiple dependencies in the same synchronous block, the computed property recomputes once and the DOM updates once — not once per dependency change. This keeps rapid updates predictable and avoids redundant work.