Module

Hylograph.Kernel.WASM.Halogen

Package
purescript-hylograph-wasm-kernel
Repository
afcondon/purescript-hylograph-wasm-kernel

Halogen Integration for WASM Force Simulations

Provides subscription-based API for integrating WASM simulations with Halogen components. API-compatible with Hylograph.ForceEngine.Halogen for drop-in replacement.

Usage:

import Hylograph.Kernel.WASM.Simulation as Sim
import Hylograph.Kernel.WASM.Events (defaultCallbacks)
import Hylograph.Kernel.WASM.Halogen (subscribeToSimulation)

handleAction Initialize = do
  callbacks <- liftEffect defaultCallbacks
  sim <- liftEffect $ Sim.createWithCallbacks Sim.defaultConfig callbacks
  emitter <- liftEffect $ subscribeToSimulation sim
  void $ H.subscribe $ emitter <#> \event -> case event of
    Tick -> UpdateNodePositions
    Started -> SimStarted
    Stopped -> SimStopped
    AlphaDecayed alpha -> AlphaChanged alpha

#subscribeToSimulation Source

subscribeToSimulation :: forall row linkRow. Simulation row linkRow -> Effect (Emitter SimulationEvent)

Create a Halogen subscription emitter for simulation events.

This function wires up the simulation's callback system to emit Halogen-compatible events. The emitter can be used with H.subscribe.

Note: The simulation must have been created with createWithCallbacks. If created with plain create, this function returns an emitter that never fires.

Re-exports from Hylograph.Kernel.WASM.Events

#SimulationEvent Source

data SimulationEvent

Events 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:

  1. Performance: No boxing/unboxing of event data in hot path
  2. Simplicity: Consumers can read simulation state directly if needed
  3. Flexibility: The consumer decides what data they need