capture_query.bend
bendvy-ecs-core@0.1.0.1 · 51 lines · 0 definitions
1import Base
2import bendvy-ecs-core@0.1.0.1/change_tick.bend as Tick
3import bendvy-ecs-core@0.1.0.1/world.bend as W
4import bendvy-ecs-core@0.1.0.1/compose.bend as Q
5import bendvy-ecs-core@0.1.0.1/transaction.bend as T
6import bendvy-ecs-core@0.1.0.1/commands.bend as Cmd
7
8# Ordinary query selection and access stay in Q.Frame. The affine accumulator is
9# outside the transaction journal and survives each entered row, including error.
10type Executed<World:Type,L:Type,Error:Data,Out:Data> is Type:
11 Executed{world:World,capture:L,outcome:T.Outcome<Error>,values:List<&2,Out>}
12def prepend(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,value:Out,observed:L & (T.Tx<W.World<S,C,R,E>,E> & (T.Outcome<Q.Error<U>> & List<&2,Out>))) -> L & (T.Tx<W.World<S,C,R,E>,E> & (T.Outcome<Q.Error<U>> & List<&2,Out>)):
13 match observed:
14 case (capture,(tx,(status,values))):(capture,(tx,(status,value <> values)))
15def completed(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,next:L -> T.Tx<W.World<S,C,R,E>,E> -> L & (T.Tx<W.World<S,C,R,E>,E> & (T.Outcome<Q.Error<U>> & List<&2,Out>)),capture:L,tx:T.Tx<W.World<S,C,R,E>,E>,value:Out,status:T.Outcome<Q.Error<U>>) -> L & (T.Tx<W.World<S,C,R,E>,E> & (T.Outcome<Q.Error<U>> & List<&2,Out>)):
16 match status:
17 case T.Success{}:prepend(~S,~C,~R,~E,~L,~U,~Out,value,next(capture,tx))
18 case T.Failure{error}:(capture,(tx,(T.Failure{error},[])))
19def body_returned(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,next:L -> T.Tx<W.World<S,C,R,E>,E> -> L & (T.Tx<W.World<S,C,R,E>,E> & (T.Outcome<Q.Error<U>> & List<&2,Out>)),observed:Q.Frame<S,C,R,E> & (L & (T.Outcome<U> & Out))) -> L & (T.Tx<W.World<S,C,R,E>,E> & (T.Outcome<Q.Error<U>> & List<&2,Out>)):
20 match observed:
21 case (Q.Frame{tx,_,internal,_},(capture,(user,value))):completed(~S,~C,~R,~E,~L,~U,~Out,next,capture,tx,value,Q.outcome(~U,internal,user))
22def selected(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,~Ops:Type -> Type,~A:Data,~plan:Q.Plan<S,C,R,E,Ops>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),args:A,capture:L,next:L -> T.Tx<W.World<S,C,R,E>,E> -> L & (T.Tx<W.World<S,C,R,E>,E> & (T.Outcome<Q.Error<U>> & List<&2,Out>)),observed:Q.Frame<S,C,R,E> & Bool) -> L & (T.Tx<W.World<S,C,R,E>,E> & (T.Outcome<Q.Error<U>> & List<&2,Out>)):
23 match observed:
24 case (Q.Frame{tx,_,_,_},False{}):next(capture,tx)
25 case (frame,True{}):body_returned(~S,~C,~R,~E,~L,~U,~Out,next,body(Q.Frame<S,C,R,E>,Q.plan_caps(~S,~C,~R,~E,~Ops,plan),capture,args,frame))
26def run_handles(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,~Ops:Type -> Type,~A:Data,~plan:Q.Plan<S,C,R,E,Ops>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),handles:List<&2,W.Handle<S>>,+args:A,+cursor:Maybe<&2,Tick.Tick>,capture:L,tx:T.Tx<W.World<S,C,R,E>,E>) -> L & (T.Tx<W.World<S,C,R,E>,E> & (T.Outcome<Q.Error<U>> & List<&2,Out>)):
27 match handles:
28 case []:(capture,(tx,(T.Success{},[])))
29 case handle <> rest:selected(~S,~C,~R,~E,~L,~U,~Out,~Ops,~A,~plan,~body,args,capture,local => transaction => run_handles(~S,~C,~R,~E,~L,~U,~Out,~Ops,~A,~plan,~body,rest,args,cursor,local,transaction),Q.plan_select(~S,~C,~R,~E,~Ops,plan)(Q.Frame{tx,handle,None{},cursor}))
30def returned(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,capture:L,values:List<&2,Out>,result:T.Returned<W.World<S,C,R,E>,Q.Error<U>>) -> Executed<W.World<S,C,R,E>,L,Q.Error<U>,Out>:
31 match result:
32 case T.Returned{world,T.Success{}}:Executed{world,capture,T.Success{},values}
33 case T.Returned{world,T.Failure{error}}:Executed{world,capture,T.Failure{error},[]}
34def finished(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,observed:L & (T.Tx<W.World<S,C,R,E>,E> & (T.Outcome<Q.Error<U>> & List<&2,Out>))) -> Executed<W.World<S,C,R,E>,L,Q.Error<U>,Out>:
35 match observed:
36 case (capture,(tx,(status,values))):returned(~S,~C,~R,~E,~L,~U,~Out,capture,values,Cmd.tx_finish(~S,~C,~R,~E,~Q.Error<U>,tx,status))
37def scope_finished(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,owned:Bool,result:Executed<W.World<S,C,R,E>,L,Q.Error<U>,Out>) -> Executed<W.World<S,C,R,E>,L,Q.Error<U>,Out>:
38 match result:
39 case Executed{world,capture,outcome,values}:Executed{W.finish_run(~S,~C,~R,~E,owned,world),capture,outcome,values}
40def reserved_inner(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,~Ops:Type -> Type,~A:Data,~plan:Q.Plan<S,C,R,E,Ops>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),args:A,cursor:Maybe<&2,Tick.Tick>,capture:L,result:W.World<S,C,R,E> & List<&2,W.Handle<S>>) -> Executed<W.World<S,C,R,E>,L,Q.Error<U>,Out>:
41 (world,handles) = result
42 finished(~S,~C,~R,~E,~L,~U,~Out,run_handles(~S,~C,~R,~E,~L,~U,~Out,~Ops,~A,~plan,~body,handles,args,cursor,capture,T.begin(~W.World<S,C,R,E>,~E,world)))
43def reserved_scoped(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,~Ops:Type -> Type,~A:Data,~plan:Q.Plan<S,C,R,E,Ops>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),args:A,cursor:Maybe<&2,Tick.Tick>,capture:L,handles:List<&2,W.Handle<S>>,admitted:W.World<S,C,R,E> & Bool) -> Executed<W.World<S,C,R,E>,L,Q.Error<U>,Out>:
44 (world,owned)=admitted
45 scope_finished(~S,~C,~R,~E,~L,~U,~Out,owned,reserved_inner(~S,~C,~R,~E,~L,~U,~Out,~Ops,~A,~plan,~body,args,cursor,capture,(world,handles)))
46def reserved(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,~Ops:Type -> Type,~A:Data,~plan:Q.Plan<S,C,R,E,Ops>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),args:A,cursor:Maybe<&2,Tick.Tick>,capture:L,result:W.World<S,C,R,E> & List<&2,W.Handle<S>>) -> Executed<W.World<S,C,R,E>,L,Q.Error<U>,Out>:
47 (world,handles)=result
48 reserved_scoped(~S,~C,~R,~E,~L,~U,~Out,~Ops,~A,~plan,~body,args,cursor,capture,handles,W.ensure_run(~S,~C,~R,~E,world))
49
50def each_since(~S:Data,~C:Type,~R:Type,~E:Data,~L:Type,~U:Data,~Out:Data,~Ops:Type -> Type,~A:Data,~plan:Q.Plan<S,C,R,E,Ops>,~body:@-H:Type -> @-caps:Ops(H) -> L -> A -> H -> H & (L & (T.Outcome<U> & Out)),args:A,cursor:Maybe<&2,Tick.Tick>,capture:L,world:W.World<S,C,R,E>) -> Executed<W.World<S,C,R,E>,L,Q.Error<U>,Out>:
51 reserved(~S,~C,~R,~E,~L,~U,~Out,~Ops,~A,~plan,~body,args,cursor,capture,W.handles(~S,~C,~R,~E,world))