import Base import ./trace_relation.bend as TraceRel import ./deferred_command.bend as Deferred import ./trace_facts.bend as TraceFacts import entity_key.bend as Key import relation_types.bend as G import relation_reorder_core.bend as C import relation_commands.bend as A import world.bend as W import transaction.bend as X import capabilities.bend as Cap type Args is Data: Args{parent:W.Handle,children:List<&2,W.Handle>} type Writable is Type: Writable{reorder:Cap.Request,A.QueueStatus>} def descriptor(~S:Data,value:G.HierarchyDescriptor) -> G.Descriptor: match value: case G.HierarchyDescriptor{key,name,inverse}: G.Descriptor{key,name,inverse,G.Hierarchy{}} def store_reorder(~S:Data,~C:Type,store:A.Store,d:G.Descriptor,parent:Key.Key,children:List<&2,G.ChildStatus>,parentLive:Bool) -> A.Store & Maybe<&2,G.Error>: match store: case A.Store{components,graph}: A.relate_result(~S,~C,C.reorder(~S,graph,d,parent,children,parentLive),components) def error_target(error:G.Error,parent:Key.Key) -> Key.Key: match error: case G.MissingChildEntity{_,child,_}: child case G.DuplicateChild{_,child,_}: child case G.ChildNotRelatedToParent{_,child,_}: child case _: parent def publish(~S:Data,~C:Type,~R:Type,~E:Data,observed:W.World,R,A.Notice> & Maybe<&2,G.Error>,d:G.Descriptor,+parent:Key.Key) -> W.World,R,A.Notice>: match observed: case (world,None{}): world case (world,Some{+error}): W.event_publish(~S,~A.Store,~R,~A.Notice,world,[A.RelationFailed{G.Failure{d,G.ReorderChildren{},parent,error_target(error,parent),error}}]) def children_prepend(~S:Data,~C:Type,~R:Type,~E:Data,result:W.World,R,A.Notice> & List<&2,G.ChildStatus>,id:Key.Key,live:Bool) -> W.World,R,A.Notice> & List<&2,G.ChildStatus>: (world,rest) = result (world,G.ChildStatus{id,live} <> rest) def child_next(~S:Data,~C:Type,~R:Type,~E:Data,checked:W.World,R,A.Notice> & Bool,id:Key.Key,next:W.World,R,A.Notice> -> W.World,R,A.Notice> & List<&2,G.ChildStatus>) -> W.World,R,A.Notice> & List<&2,G.ChildStatus>: (world,live) = checked children_prepend(~S,~C,~R,~E,next(world),id,live) def child_statuses(~S:Data,~C:Type,~R:Type,~E:Data,children:List<&2,W.Handle>,world:W.World,R,A.Notice>) -> W.World,R,A.Notice> & List<&2,G.ChildStatus>: match children: case []: (world,[]) case W.Handle{+ns,+id} <> rest: child_next(~S,~C,~R,~E,W.valid(~S,~A.Store,~R,~A.Notice,world,W.Handle{ns,id}),id,world => child_statuses(~S,~C,~R,~E,rest,world)) def apply_children_result(~S:Data,~C:Type,~R:Type,~E:Data,observed:W.World,R,A.Notice> & List<&2,G.ChildStatus>,+d:G.Descriptor,+parent:Key.Key,parentLive:Bool) -> W.World,R,A.Notice> & Maybe<&2,G.Error>: (world,children) = observed W.with_store(~S,~A.Store,~R,~A.Notice,~Maybe<&2,G.Error>,store => store_reorder(~S,~C,store,d,parent,children,parentLive),world) def apply_parent_result(~S:Data,~C:Type,~R:Type,~E:Data,checked:W.World,R,A.Notice> & Bool,d:G.Descriptor,parent:Key.Key,children:List<&2,W.Handle>) -> W.World,R,A.Notice> & Maybe<&2,G.Error>: (world,live) = checked apply_children_result(~S,~C,~R,~E,child_statuses(~S,~C,~R,~E,children,world),d,parent,live) def apply_result(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,world:W.World,R,A.Notice>,args:Args) -> W.World,R,A.Notice> & Maybe<&2,G.Error>: match args: case Args{W.Handle{+ns,+parent},children}: apply_parent_result(~S,~C,~R,~E,W.valid(~S,~A.Store,~R,~A.Notice,world,W.Handle{ns,parent}),descriptor(~S,d),parent,children) def apply_published(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,args:Args,result:W.World,R,A.Notice> & Maybe<&2,G.Error>) -> W.World,R,A.Notice>: match args: case Args{parent,_}:publish(~S,~C,~R,~E,result,descriptor(~S,d),W.handle_key(~S,parent)) def apply_traced(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,trace:TraceFacts.State,args:Args,result:W.World,R,A.Notice> & Maybe<&2,G.Error>) -> W.World,R,A.Notice> & TraceFacts.State: match args result: case Args{+parent,+children}, (world,+error):(publish(~S,~C,~R,~E,(world,error),descriptor(~S,d),W.handle_key(~S,parent)),TraceRel.reorder_applied(~S,trace,descriptor(~S,d),parent,children,error)) def apply(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,world:W.World,R,A.Notice>,+args:Args) -> W.World,R,A.Notice>: apply_published(~S,~C,~R,~E,~d,args,apply_result(~S,~C,~R,~E,~d,world,args)) def namespaces(~S:Data,children:List<&2,W.Handle>,+namespace:U32) -> Bool: match children: case []: True{} case W.Handle{ns,_} <> rest: Bool.and(U32.is_eq(namespace,ns),namespaces(~S,rest,namespace)) def same_world(~S:Data,+args:Args,+namespace:U32) -> Bool: match args: case Args{W.Handle{ns,_},children}: Bool.and(U32.is_eq(ns,namespace),namespaces(~S,children,namespace)) def reorder_command(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,enabled:Bool,+args:Args) -> Deferred.Command,R,A.Notice>>: match enabled: case False{}:Deferred.Raw{world=>apply(~S,~C,~R,~E,~d,world,args)} case True{}:Deferred.Observed{trace=>world=>apply_traced(~S,~C,~R,~E,~d,trace,args,apply_result(~S,~C,~R,~E,~d,world,args))} def reordered_stage(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,+args:Args,result:TraceFacts.FactResult,tx:X.Tx,R,A.Notice>,A.Notice>) -> X.Tx,R,A.Notice>,A.Notice>: match args tx: case Args{parent,children} X.Tx{world,undo,commands,events,trace}:X.Tx{world,undo,commands,events,TraceRel.reorder(~S,trace,descriptor(~S,d),TraceFacts.StageOperation{},parent,children,result)} def reorder_staged(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,+args:Args,tx:X.Tx,R,A.Notice>,A.Notice>) -> X.Tx,R,A.Notice>,A.Notice>: match tx: case X.Tx{world,undo,commands,events,+trace}:reordered_stage(~S,~C,~R,~E,~d,args,TraceFacts.Attempted{},X.Tx{world,undo,reorder_command(~S,~C,~R,~E,~d,TraceFacts.enabled(trace),args) <> commands,events,trace}) def queued(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,same:Bool,tx:X.Tx,R,A.Notice>,A.Notice>,args:Args) -> X.Tx,R,A.Notice>,A.Notice> & A.QueueStatus: match same: case False{}: (reordered_stage(~S,~C,~R,~E,~d,args,TraceFacts.Refused{"MissingEntity"},tx),A.MissingEntity{}) case True{}: (reorder_staged(~S,~C,~R,~E,~d,args,tx),A.Queued{}) def namespace_join(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,observed:W.World,R,A.Notice> & U32,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,+args:Args) -> X.Tx,R,A.Notice>,A.Notice> & A.QueueStatus: (world,namespace) = observed queued(~S,~C,~R,~E,~d,same_world(~S,args,namespace),X.Tx{world,undo,commands,events,trace},args) def queue(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,tx:X.Tx,R,A.Notice>,A.Notice>,args:Args) -> X.Tx,R,A.Notice>,A.Notice> & A.QueueStatus: match tx: case X.Tx{world,undo,commands,events,trace}: namespace_join(~S,~C,~R,~E,~d,W.namespace(~S,~A.Store,~R,~A.Notice,world),undo,commands,events,trace,args) def body(~S:Data,-H:Type,caps:Writable,args:Args,owner:H) -> H & A.QueueStatus: match caps: case Writable{Cap.Request{reorder}}: reorder(owner,args) def invoke(~S:Data,~C:Type,~R:Type,~E:Data,~d:G.HierarchyDescriptor,~Out:Type,~client:@-H:Type -> Writable -> Args -> H -> H & Out,args:Args,tx:X.Tx,R,A.Notice>,A.Notice>) -> X.Tx,R,A.Notice>,A.Notice> & Out: client(X.Tx,R,A.Notice>,A.Notice>,Writable{Cap.Request{queue(~S,~C,~R,~E,~d)}},args,tx)