import { group } from "@thi.ng/hiccup-svg/group" ; import { rect } from "@thi.ng/hiccup-svg/rect" ; import { svg } from "@thi.ng/hiccup-svg/svg" ; import type { EventBus } from "@thi.ng/interceptors" ; import { initGraph , node } from "@thi.ng/rstream-graph/graph" ; import { map } from "@thi.ng/transducers/map" ; import { range2d } from "@thi.ng/transducers/range2d" ; import * as ev from "./events" ; import * as paths from "./paths" ; param bus export function initDataflow ( bus ) { const graph = initGraph ( bus . state , { grid : { fn : grid , ins : { cols : { path : paths . COLS } , rows : { path : paths . ROWS } , } , } , rotation : { fn : rotate , ins : { shapes : { stream : "/grid/node" } , theta : { path : paths . THETA } , } , } , svg : { fn : createSVG , ins : { shapes : { stream : "/rotation/node" } , cols : { path : paths . COLS } , rows : { path : paths . ROWS } , stroke : { path : paths . STROKE } , } , outs : { "*" : ( node ) => node . subscribe ( { next : ( svg ) => bus . dispatch ( [ ev . UPDATE_SVG , svg ] ) , } ) , } , } , } ) ; return graph ; } /**
 * Implementation for grid generator node.
 */ const grid = node ( map ( ( { cols , rows } ) => [ ... map ( ( [ x , y ] ) => [ "rect" , { x , y , width : 1 , height : 1 } ] , range2d ( cols , rows ) ) , ] ) ) ; /**
 * Implementation for rotate node. Injects SVG `transform` attribute in
 * all received shapes.
 */ const rotate = node ( map ( ( { shapes , theta } ) => shapes . map ( ( s ) => ( ( s [ 1 ] . transform = `rotate(${ theta } ${ s [ 1 ] . x + 0.5 } ${ s [ 1 ] . y + 0.5 })` ) , s ) ) ) ) ; /**
 * Implementation for svg dataflow node. Wraps received shapes as
 * complete svg document in hiccup format.
 */ const createSVG = node ( map ( ( { shapes , cols , rows , stroke } ) => svg ( { class : "w-100 h-100" , preserveAspectRatio : "xMidYMid" , viewBox : `-1 -1 ${ cols + 2 } ${ rows + 2 }` , } , rect ( [ - 1 , - 1 ] , cols + 2 , rows + 2 , { fill : "black" } ) , group ( { stroke : "white" , fill : "none" , "stroke-width" : stroke , } , ... shapes ) ) ) ) ;