import { Dryad , Command , CallOrder } from "dryadic" ; import Server , { msg , whenNodeEnd , whenNodeGo } from "@supercollider/server" ; const { AddActions , groupNew , nodeFree } = msg ; interface Context { nodeID : number ; group : number ; parentGroup : number ; scserver : Server ; id : string ; } /**
 * Creates a group on the server; sets .group in context for its children,
 * so any Synths or Groups will be spawned inside this group.
 */ export default class Group extends Dryad { /**
     * If there is no SCServer in the parent context,
     * then this will wrap itself in an SCServer
     */ requireParent ( ) { return "SCServer" ; } prepareForAdd ( ) { return { callOrder : CallOrder . SELF_THEN_CHILDREN , updateContext : ( context ) => { const nodeID = context . scserver . state . nextNodeID ( ) ; return { nodeID , group : nodeID , parentGroup : context . group || 0 , } ; } , } ; } add ( ) { return { scserver : { msg : ( context ) => groupNew ( context . nodeID , AddActions . TAIL , context . parentGroup ) , } , run : ( context ) => whenNodeGo ( context . scserver , context . id , context . nodeID ) , } ; } remove ( ) { return { scserver : { msg : ( context ) => nodeFree ( context . nodeID ) , } , run : ( context ) => whenNodeEnd ( context . scserver , context . id , context . nodeID ) , } ; } }