import Base import ../../change_tick.bend as Tick import ../../deferred_command.bend as Deferred import domains.bend as D import routing.bend as Route import ../../world.bend as W type BarrierState is Type: BarrierState{world: W.World,domains: List<&2,D.Domain>,tick: Nat} def collected(~S: Data,~C: Type,~R: Type,~E: Data,joined: W.World & List<&2,D.Domain>,tick: Nat) -> BarrierState: (world,domains) = joined BarrierState{world,domains,tick} def command_collected(~S: Data,~C: Type,~R: Type,~E: Data,~classify: E -> Maybe>,command: Deferred.Command>,state: BarrierState) -> BarrierState: match state: case BarrierState{world,domains,+tick}: collected(~S,~C,~R,~E,Route.collect(~S,~C,~R,~E,~classify,Deferred.run_disabled(~W.World,command,world),tick,domains),tick) def closed(~S:Data,~C:Type,~R:Type,~E:Data,state:BarrierState) -> BarrierState: match state: case BarrierState{world,domains,tick}:BarrierState{W.leave_run(~S,~C,~R,~E,world),domains,tick} def drain(~S: Data,~C: Type,~R: Type,~E: Data,~classify: E -> Maybe>,commands: List>>,state: BarrierState) -> BarrierState: match commands: case []: closed(~S,~C,~R,~E,state) case command <> rest: drain(~S,~C,~R,~E,~classify,rest,command_collected(~S,~C,~R,~E,~classify,command,state)) # The deferred barrier advances the actual World clock once. Every command # publishes a distinct batch stamped with that same barrier tick. def opened(~S: Data,~C: Type,~R: Type,~E: Data,~classify: E -> Maybe>,advanced: W.World & Tick.Tick,domains: List<&2,D.Domain>) -> BarrierState: (world,+tick) = advanced match world: case W.World{+namespace,allocator,store,resource,+events,pending,+registrations,+nextSystemId,+clock}: drain(~S,~C,~R,~E,~classify,List.reverse(&1,Deferred.Command>,pending),BarrierState{W.World{namespace,allocator,store,resource,events,[],registrations,nextSystemId,clock},domains,Tick.total(tick)}) def barrier(~S: Data,~C: Type,~R: Type,~E: Data,~classify: E -> Maybe>,world: W.World,domains: List<&2,D.Domain>) -> BarrierState: opened(~S,~C,~R,~E,~classify,W.enter_run(~S,~C,~R,~E,world),domains)