import Base import ./deferred_command.bend as Deferred import ./trace_facts.bend as TraceFacts import ./commands.bend as Cmd import ./lifetime_scope.bend as Sc import ./world.bend as W import ./transaction.bend as T import ./relation_commands.bend as A # Ordinary population receives the same full World context as Cmd.tx_spawn. # Membership is assigned only after successful activation and population. def assigned(~S:Data,~C:Type,~R:Type,~E:Data,seen:W.World>,R,A.Notice> & Unit) -> W.World>,R,A.Notice>: match seen: case (world,_):world def assign(~S:Data,~C:Type,~R:Type,~E:Data,world:W.World>,R,A.Notice>,handle:W.Handle,name:String) -> W.World>,R,A.Notice>: match handle: case W.Handle{_,id}:assigned(~S,~C,~R,~E,W.with_store(~S,~A.Store>,~R,~A.Notice,~Unit,store => Sc.assigned(~S,~C,store,name,id),world)) def activated(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:W.World>,R,A.Notice> -> W.Handle -> P -> W.World>,R,A.Notice>,seen:W.World>,R,A.Notice> & W.Status,+handle:W.Handle,payload:P,name:String) -> W.World>,R,A.Notice>: match seen: case (world,W.Accepted{}):assign(~S,~C,~R,~E,populate(world,handle,payload),handle,name) case (world,W.Rejected{_}):world def apply(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:W.World>,R,A.Notice> -> W.Handle -> P -> W.World>,R,A.Notice>,world:W.World>,R,A.Notice>,+handle:W.Handle,payload:P,name:String) -> W.World>,R,A.Notice>: activated(~S,~C,~R,~E,~P,~populate,W.activate(~S,~A.Store>,~R,~A.Notice,world,handle),handle,payload,name) def reserved(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:W.World>,R,A.Notice> -> W.Handle -> P -> W.World>,R,A.Notice>,seen:W.World>,R,A.Notice> & W.Reservation,undo:List<(W.World>,R,A.Notice> -> W.World>,R,A.Notice>)>,commands:List>,R,A.Notice>>>,events:List<&2,A.Notice>,trace:TraceFacts.State,payload:P,name:String) -> T.Tx>,R,A.Notice>,A.Notice> & Sc.SpawnResult: match seen: case (world,W.ReservationRejected{error}):(T.Tx{world,undo,commands,events,trace},Sc.SpawnRejected{payload,error}) case (world,W.Reserved{+handle}):(Cmd.track_pending(~S,~A.Store>,~R,~A.Notice,T.Tx{world,undo,Deferred.Raw{(world => apply(~S,~C,~R,~E,~P,~populate,world,handle,payload,name))} <> commands,events,trace},handle),Sc.Spawned{handle}) def checked(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:W.World>,R,A.Notice> -> W.Handle -> P -> W.World>,R,A.Notice>,same:Bool,world:W.World>,R,A.Notice>,undo:List<(W.World>,R,A.Notice> -> W.World>,R,A.Notice>)>,commands:List>,R,A.Notice>>>,events:List<&2,A.Notice>,trace:TraceFacts.State,payload:P,name:String) -> T.Tx>,R,A.Notice>,A.Notice> & Sc.SpawnResult: match same: case False{}:(T.Tx{world,undo,commands,events,trace},Sc.SpawnRejected{payload,W.MissingEntity{}}) case True{}:reserved(~S,~C,~R,~E,~P,~populate,W.reserve_id(~S,~A.Store>,~R,~A.Notice,world),undo,commands,events,trace,payload,name) def namespaced(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:W.World>,R,A.Notice> -> W.Handle -> P -> W.World>,R,A.Notice>,seen:W.World>,R,A.Notice> & U32,scope:Sc.Scope,undo:List<(W.World>,R,A.Notice> -> W.World>,R,A.Notice>)>,commands:List>,R,A.Notice>>>,events:List<&2,A.Notice>,trace:TraceFacts.State,payload:P) -> T.Tx>,R,A.Notice>,A.Notice> & Sc.SpawnResult: match seen scope: case (world,current) Sc.Scope{namespace,name}:checked(~S,~C,~R,~E,~P,~populate,U32.is_eq(current,namespace),world,undo,commands,events,trace,payload,name) def tx_spawn_in(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:W.World>,R,A.Notice> -> W.Handle -> P -> W.World>,R,A.Notice>,tx:T.Tx>,R,A.Notice>,A.Notice>,scope:Sc.Scope,payload:P) -> T.Tx>,R,A.Notice>,A.Notice> & Sc.SpawnResult: match tx: case T.Tx{world,undo,commands,events,trace}:namespaced(~S,~C,~R,~E,~P,~populate,W.namespace(~S,~A.Store>,~R,~A.Notice,world),scope,undo,commands,events,trace,payload)