Hylograph.Kernel.D3.Events
- Package
- purescript-hylograph-d3-kernel
- Repository
- afcondon/purescript-hylograph-d3-kernel
Simulation Events
Event types and callback system for force simulations. Designed for integration with UI frameworks like Halogen.
Key design principles:
- Callbacks are
Effect Unit- no monadic overhead in hot path - All callbacks are optional (stored in Refs, default to no-op)
- Event types are for documentation/Halogen integration, not for internal use
#SimulationEvent Source
data SimulationEventEvents that can occur during simulation lifecycle.
These types are primarily for Halogen integration via subscriptions. Internally, callbacks are invoked directly without constructing these values.
Usage with Halogen:
handleAction (SimEvent event) = case event of
Tick -> liftEffect updateDOMPositions
Started -> H.modify_ _ { simRunning = true }
Stopped -> H.modify_ _ { simRunning = false }
AlphaDecayed alpha -> when (alpha < 0.1) $ liftEffect doSomething
Constructors
Instances
#SimulationCallbacks Source
type SimulationCallbacks = { onAlphaThreshold :: Ref (Number -> Effect Unit), onStart :: Ref (Effect Unit), onStop :: Ref (Effect Unit), onTick :: Ref (Effect Unit) }Callbacks for simulation events.
All callbacks are stored in Refs to allow dynamic updates and to keep the tick loop allocation-free (no closures created per-tick).
The callbacks are Effect Unit rather than taking event data because:
- Performance: No boxing/unboxing of event data in hot path
- Simplicity: Consumers can read simulation state directly if needed
- Flexibility: The consumer decides what data they need
#defaultCallbacks Source
defaultCallbacks :: Effect SimulationCallbacksCreate default callbacks (all no-ops).
Usage:
callbacks <- defaultCallbacks
Ref.write updateDOMPositions callbacks.onTick
sim <- createWithCallbacks config callbacks
#onSimulationTick Source
onSimulationTick :: Effect Unit -> SimulationCallbacks -> Effect UnitSet the tick callback.
WARNING: This callback runs 60x/second during simulation. Keep it fast! Avoid allocations, complex logic, or console.log.
Good:
onSimulationTick (updateGroupPositions nodes) callbacks
Bad:
onSimulationTick (do
nodes <- Ref.read nodesRef -- Extra ref read
log "tick" -- Console I/O
H.tell ... -- Halogen message (allocates)
) callbacks
#onSimulationStart Source
onSimulationStart :: Effect Unit -> SimulationCallbacks -> Effect UnitSet the start callback.
Called when simulation starts running or is reheated from stopped state. Safe to do heavier work here (only called occasionally).
#onSimulationStop Source
onSimulationStop :: Effect Unit -> SimulationCallbacks -> Effect UnitSet the stop callback.
Called when simulation stops (alpha < alphaMin). Safe to do heavier work here.
#onAlphaDecay Source
onAlphaDecay :: (Number -> Effect Unit) -> SimulationCallbacks -> Effect UnitSet the alpha threshold callback.
Called when alpha crosses common thresholds: 0.5, 0.1, 0.01. The callback receives the new alpha value.
Useful for:
- Progressive rendering (show more detail as simulation settles)
- UI updates (e.g., hide "Simulating..." spinner)
- Analytics (track how long simulations run)