import Base import ./deferred_command.bend as Deferred import ./trace_facts.bend as TraceFacts import entity_key.bend as Key import ./world.bend as W import ./transaction.bend as T import ./commands.bend as Cmd import ./relation_commands.bend as A import ./relation_types.bend as G import ./relation_cleanup.bend as Cleanup import ./string_equality.bend as Text # Lifetime metadata is separate from component owners and never proves liveness. type Scope<-S:Data> is Data: Scope{namespace:U32,name:String} type Member is Data: Member{name:String,id:Key.Key} type Store<-S:Data,C:Type> is Type: Store{components:C,members:List<&2,Member>} def empty(~S:Data,~C:Type,components:C) -> Store: Store{components,[]} def without(members:List<&2,Member>,+id:Key.Key) -> List<&2,Member>: match members: case []:[] case Member{+name,+mid} <> rest: +remaining = without(rest,id) Bool.pick(List<&2,Member>,Key.equal(mid,id),remaining,Member{name,mid} <> remaining) def select(members:List<&2,Member>,+name:String) -> List<&2,Key.Key>: match members: case []:[] case Member{+key,+id} <> rest: +remaining = select(rest,name) Bool.pick(List<&2,Key.Key>,Text.equal(key,name),id <> remaining,remaining) def assigned(~S:Data,~C:Type,store:A.Store>,name:String,+id:Key.Key) -> A.Store> & Unit: match store: case A.Store{Store{components,members},graph}:(A.Store{Store{components,List.append(&2,Member,without(members,id),[Member{name,id}])},graph},Unit{}) def observed(~S:Data,~C:Type,store:A.Store>,name:String) -> A.Store> & List<&2,Key.Key>: match store: case A.Store{Store{components,+members},graph}:(A.Store{Store{components,members},graph},select(members,name)) def scope_returned(~S:Data,~C:Type,~R:Type,~E:Data,name:String,seen:W.World>,R,A.Notice> & U32) -> W.World>,R,A.Notice> & Scope: match seen: case (world,namespace):(world,Scope{namespace,name}) def scope(~S:Data,~C:Type,~R:Type,~E:Data,world:W.World>,R,A.Notice>,name:String) -> W.World>,R,A.Notice> & Scope: scope_returned(~S,~C,~R,~E,name,W.namespace(~S,~A.Store>,~R,~A.Notice,world)) def cleared(~S:Data,~C:Type,~E:Data,members:List<&2,Member>,seen:C & List<&2,A.Notice>) -> Store & List<&2,A.Notice>: match seen: case (components,records):(Store{components,members},records) def clear_components(~S:Data,~C:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,store:Store,handle:W.Handle) -> Store & List<&2,A.Notice>: match store handle: case Store{components,members} W.Handle{+namespace,+id}:cleared(~S,~C,~E,without(members,id),clear(components,W.Handle{namespace,id})) def cleanup_result(~S:Data,~C:Type,~R:Type,~E:Data,result:Cleanup.CleanupResult,R,E>) -> W.World>,R,A.Notice>: match result: case Cleanup.Complete{world}:world case Cleanup.Incomplete{world,frames}:W.event_publish(~S,~A.Store>,~R,~A.Notice,world,[A.CleanupPaused{frames}]) def cleanup_members(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~descriptors:List<&2,G.Descriptor>,members:List<&2,Key.Key>,world:W.World>,R,A.Notice>,+namespace:U32) -> W.World>,R,A.Notice>: match members: case []:world case id <> rest:cleanup_members(~S,~C,~R,~E,~clear,~record,~descriptors,rest,cleanup_result(~S,~C,~R,~E,Cleanup.automatic(~S,~Store,~R,~E,~clear_components(~S,~C,~E,~clear),~record,world,W.Handle{namespace,id},descriptors)),namespace) def cleanup_observed(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~descriptors:List<&2,G.Descriptor>,namespace:U32,seen:W.World>,R,A.Notice> & List<&2,Key.Key>) -> W.World>,R,A.Notice>: match seen: case (world,members):cleanup_members(~S,~C,~R,~E,~clear,~record,~descriptors,members,world,namespace) def cleanup_apply(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~descriptors:List<&2,G.Descriptor>,world:W.World>,R,A.Notice>,scope:Scope) -> W.World>,R,A.Notice>: match scope: case Scope{namespace,name}:cleanup_observed(~S,~C,~R,~E,~clear,~record,~descriptors,namespace,W.with_store(~S,~A.Store>,~R,~A.Notice,~List<&2,Key.Key>,store => observed(~S,~C,store,name),world)) def cleanup_checked(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~descriptors:List<&2,G.Descriptor>,same:Bool,tx:T.Tx>,R,A.Notice>,A.Notice>,scope:Scope) -> T.Tx>,R,A.Notice>,A.Notice> & A.QueueStatus: match same: case False{}:(tx,A.MissingEntity{}) case True{}:(T.stage(~W.World>,R,A.Notice>,~A.Notice,tx,world => cleanup_apply(~S,~C,~R,~E,~clear,~record,~descriptors,world,scope)),A.Queued{}) def cleanup_namespaced(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~descriptors:List<&2,G.Descriptor>,seen:W.World>,R,A.Notice> & U32,scope: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) -> T.Tx>,R,A.Notice>,A.Notice> & A.QueueStatus: match seen scope: case (world,current) Scope{+namespace,+name}:cleanup_checked(~S,~C,~R,~E,~clear,~record,~descriptors,U32.is_eq(namespace,current),T.Tx{world,undo,commands,events,trace},Scope{namespace,name}) def tx_cleanup(~S:Data,~C:Type,~R:Type,~E:Data,~clear:C -> W.Handle -> C & List<&2,A.Notice>,~record:W.Handle -> A.Notice,~descriptors:List<&2,G.Descriptor>,tx:T.Tx>,R,A.Notice>,A.Notice>,scope:Scope) -> T.Tx>,R,A.Notice>,A.Notice> & A.QueueStatus: match tx: case T.Tx{world,undo,commands,events,trace}:cleanup_namespaced(~S,~C,~R,~E,~clear,~record,~descriptors,W.namespace(~S,~A.Store>,~R,~A.Notice,world),scope,undo,commands,events,trace) type SpawnResult is Type: Spawned{handle:W.Handle} SpawnRejected{payload:P,error:W.WorldError} def populate_store(~S:Data,~C:Type,~P:Type,~populate:C -> W.Handle -> P -> C,store:A.Store>,handle:W.Handle,payload:P,name:String) -> A.Store> & Unit: match store handle: case A.Store{Store{components,members},graph} W.Handle{+ns,+id}:(A.Store{Store{populate(components,W.Handle{ns,id},payload),List.append(&2,Member,without(members,id),[Member{name,id}])},graph},Unit{}) def populate_returned(~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 populate_world(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:C -> W.Handle -> P -> C,world:W.World>,R,A.Notice>,+handle:W.Handle,payload:P,name:String) -> W.World>,R,A.Notice>: populate_returned(~S,~C,~R,~E,W.with_store(~S,~A.Store>,~R,~A.Notice,~Unit,store => populate_store(~S,~C,~P,~populate,store,handle,payload,name),world)) def spawn_activated(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:C -> W.Handle -> P -> C,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{}):populate_world(~S,~C,~R,~E,~P,~populate,world,handle,payload,name) case (world,W.Rejected{_}):world def spawn_apply(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:C -> W.Handle -> P -> C,world:W.World>,R,A.Notice>,+handle:W.Handle,payload:P,name:String) -> W.World>,R,A.Notice>: spawn_activated(~S,~C,~R,~E,~P,~populate,W.activate(~S,~A.Store>,~R,~A.Notice,world,handle),handle,payload,name) def spawn_reserved(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:C -> W.Handle -> P -> C,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> & SpawnResult: match seen: case (world,W.ReservationRejected{error}):(T.Tx{world,undo,commands,events,trace},SpawnRejected{payload,error}) case (world,W.Reserved{+handle}):(Cmd.track_pending(~S,~A.Store>,~R,~A.Notice,T.Tx{world,undo,Deferred.Raw{(world => spawn_apply(~S,~C,~R,~E,~P,~populate,world,handle,payload,name))} <> commands,events,trace},handle),Spawned{handle}) def spawn_checked(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:C -> W.Handle -> P -> C,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> & SpawnResult: match same: case False{}:(T.Tx{world,undo,commands,events,trace},SpawnRejected{payload,W.MissingEntity{}}) case True{}:spawn_reserved(~S,~C,~R,~E,~P,~populate,W.reserve_id(~S,~A.Store>,~R,~A.Notice,world),undo,commands,events,trace,payload,name) def spawn_namespaced(~S:Data,~C:Type,~R:Type,~E:Data,~P:Type,~populate:C -> W.Handle -> P -> C,seen:W.World>,R,A.Notice> & U32,scope: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> & SpawnResult: match seen scope: case (world,current) Scope{namespace,name}:spawn_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:C -> W.Handle -> P -> C,tx:T.Tx>,R,A.Notice>,A.Notice>,scope:Scope,payload:P) -> T.Tx>,R,A.Notice>,A.Notice> & SpawnResult: match tx: case T.Tx{world,undo,commands,events,trace}:spawn_namespaced(~S,~C,~R,~E,~P,~populate,W.namespace(~S,~A.Store>,~R,~A.Notice,world),scope,undo,commands,events,trace,payload)