execution_lifetime_scope_resource_dispatch.bend

bendvy-ecs-core@0.1.0.3 · 51 lines · 12 definitions

1import Base
2import bendvy-ecs-core@0.1.0.3/execution_composer_layout.bend as Layout
3import bendvy-ecs-core@0.1.0.3/execution_resource_policy.bend as Policy
4import bendvy-ecs-core@0.1.0.3/execution_lifetime_scope_store.bend as Store
5import bendvy-ecs-core@0.1.0.3/execution_relation_resource_dispatch.bend as Route
6import bendvy-ecs-core@0.1.0.3/execution_relation_resource_publication.bend as Cast
7import bendvy-ecs-core@0.1.0.3/entity_allocator.bend as Allocator
8import bendvy-ecs-core@0.1.0.3/change_tick.bend as Tick
9import bendvy-ecs-core@0.1.0.3/lifecycle.bend as Life
10
11type Outcome<-S:Data> is Data:
12  Completed{result:Store.Outcome<S>}
13  Undeclared{request:Store.Request<S>}
14def Program(~S:Data,~resources:Layout.Layout) -> Type:
15  U32 -> U32 -> Allocator.Allocator -> Layout.resources(~S,resources) -> Layout.Route -> Store.Request<S> -> Tick.Tick -> Allocator.Allocator & (Layout.resources(~S,resources) & Outcome<S>)
16def empty(~S:Data,~resources:Layout.Layout,_:U32,_:U32,allocator:Allocator.Allocator,owners:Layout.resources(~S,resources),_:Layout.Route,request:Store.Request<S>,_:Tick.Tick) -> Allocator.Allocator & (Layout.resources(~S,resources) & Outcome<S>):
17  (allocator,(owners,Undeclared{request}))
18def refreshed(~S:Data,~key:String,~origin:Layout.Layout,allocator:Allocator.Allocator,stamp:Life.Stamp,result:Store.Outcome<S>,projected:Store.Store<S> & Store.View<S>) -> Allocator.Allocator & (Layout.ResourceSlot<S,Layout.Viewed<Store.Store<S>,Store.View<S>>,key,origin> & Outcome<S>):
19  match projected:
20    case (owner,cache):(allocator,(Layout.ResourceSlot{Layout.Viewed{owner,cache},stamp},Completed{result}))
21def changed(~S:Data,~key:String,~origin:Layout.Layout,written:Bool,tick:Tick.Tick,stamp:Life.Stamp,cache:Store.View<S>,allocator:Allocator.Allocator,owner:Store.Store<S>,result:Store.Outcome<S>) -> Allocator.Allocator & (Layout.ResourceSlot<S,Layout.Viewed<Store.Store<S>,Store.View<S>>,key,origin> & Outcome<S>):
22  match written:
23    case False{}:(allocator,(Layout.ResourceSlot{Layout.Viewed{owner,cache},stamp},Completed{result}))
24    case True{}:refreshed(~S,~key,~origin,allocator,Life.after_write(True{},True{},stamp,tick),result,Store.project(~S,owner))
25def returned(~S:Data,~key:String,~origin:Layout.Layout,tick:Tick.Tick,stamp:Life.Stamp,cache:Store.View<S>,result:Allocator.Allocator & (Store.Store<S> & Store.Outcome<S>)) -> Allocator.Allocator & (Layout.ResourceSlot<S,Layout.Viewed<Store.Store<S>,Store.View<S>>,key,origin> & Outcome<S>):
26  match result:
27    case (allocator,(owner,Store.Assignment{Store.Assigned{scope,handle}})):changed(~S,~key,~origin,True{},tick,stamp,cache,allocator,owner,Store.Assignment{Store.Assigned{scope,handle}})
28    case (allocator,(owner,Store.Assignment{Store.AssignmentRefused{scope,handle,reason}})):changed(~S,~key,~origin,False{},tick,stamp,cache,allocator,owner,Store.Assignment{Store.AssignmentRefused{scope,handle,reason}})
29    case (allocator,(owner,Store.Selection{snapshot})):changed(~S,~key,~origin,False{},tick,stamp,cache,allocator,owner,Store.Selection{snapshot})
30    case (allocator,(owner,Store.Forgotten{handle})):changed(~S,~key,~origin,True{},tick,stamp,cache,allocator,owner,Store.Forgotten{handle})
31    case (allocator,(owner,Store.Inspected{namespace})):changed(~S,~key,~origin,False{},tick,stamp,cache,allocator,owner,Store.Inspected{namespace})
32    case (allocator,(owner,Store.Refused{request})):changed(~S,~key,~origin,False{},tick,stamp,cache,allocator,owner,Store.Refused{request})
33def slot(~S:Data,~key:String,~origin:Layout.Layout,namespace:U32,allocator:Allocator.Allocator,value:Layout.ResourceSlot<S,Layout.Viewed<Store.Store<S>,Store.View<S>>,key,origin>,request:Store.Request<S>,tick:Tick.Tick) -> Allocator.Allocator & (Layout.ResourceSlot<S,Layout.Viewed<Store.Store<S>,Store.View<S>>,key,origin> & Outcome<S>):
34  match value:
35    case Layout.ResourceSlot{Layout.Viewed{owner,cache},stamp}:returned(~S,~key,~origin,tick,stamp,cache,Store.apply(~S,namespace,allocator,owner,request))
36def restored(~S:Data,~resources:Layout.Layout,~route:Layout.Route,~payload:{Layout.payload(resources,route) == Store.Store<S> : Type},~view:{Layout.view(resources,route) == Store.View<S> : Data},rest:Layout.resource_rest(~S,resources,route),result:Allocator.Allocator & (Layout.ResourceSlot<S,Layout.Viewed<Store.Store<S>,Store.View<S>>,Layout.key(resources,route),Layout.origin(resources,route)> & Outcome<S>)) -> Allocator.Allocator & (Layout.resources(~S,resources) & Outcome<S>):
37  match result:
38    case (allocator,(value,result)):(allocator,(Layout.resource_put(~S,resources,route,rest,Cast.slot(~S,~Layout.key(resources,route),~Layout.origin(resources,route),~Store.Store<S>,~Layout.payload(resources,route),~Store.View<S>,~Layout.view(resources,route),Equal.sym(Type,Layout.payload(resources,route),Store.Store<S>,payload),Equal.sym(Data,Layout.view(resources,route),Store.View<S>,view),value)),result))
39def taken(~S:Data,~resources:Layout.Layout,~route:Layout.Route,~payload:{Layout.payload(resources,route) == Store.Store<S> : Type},~view:{Layout.view(resources,route) == Store.View<S> : Data},namespace:U32,allocator:Allocator.Allocator,request:Store.Request<S>,tick:Tick.Tick,result:Layout.resource_rest(~S,resources,route) & Layout.ResourceSlot<S,Layout.stored(resources,route),Layout.key(resources,route),Layout.origin(resources,route)>) -> Allocator.Allocator & (Layout.resources(~S,resources) & Outcome<S>):
40  match result:
41    case (rest,value):restored(~S,~resources,~route,~payload,~view,rest,slot(~S,~Layout.key(resources,route),~Layout.origin(resources,route),namespace,allocator,Cast.slot(~S,~Layout.key(resources,route),~Layout.origin(resources,route),~Layout.payload(resources,route),~Store.Store<S>,~Layout.view(resources,route),~Store.View<S>,payload,view,value),request,tick))
42def namespaced(~S:Data,~resources:Layout.Layout,~route:Layout.Route,~payload:{Layout.payload(resources,route) == Store.Store<S> : Type},~view:{Layout.view(resources,route) == Store.View<S> : Data},same:Bool,namespace:U32,allocator:Allocator.Allocator,owners:Layout.resources(~S,resources),request:Store.Request<S>,tick:Tick.Tick) -> Allocator.Allocator & (Layout.resources(~S,resources) & Outcome<S>):
43  match same:
44    case False{}:(allocator,(owners,Completed{Store.Refused{request}}))
45    case True{}:taken(~S,~resources,~route,~payload,~view,namespace,allocator,request,tick,Layout.resource_take(~S,resources,route,owners))
46def selected(~S:Data,~resources:Layout.Layout,~route:Layout.Route,~payload:{Layout.payload(resources,route) == Store.Store<S> : Type},~view:{Layout.view(resources,route) == Store.View<S> : Data},matches:Bool,previous:Program(~S,~resources),+expected:U32,+actual:U32,allocator:Allocator.Allocator,owners:Layout.resources(~S,resources),requested:Layout.Route,request:Store.Request<S>,tick:Tick.Tick) -> Allocator.Allocator & (Layout.resources(~S,resources) & Outcome<S>):
47  match matches:
48    case False{}:previous(expected,actual,allocator,owners,requested,request,tick)
49    case True{}:namespaced(~S,~resources,~route,~payload,~view,U32.is_eq(expected,actual),actual,allocator,owners,request,tick)
50def extend(~S:Data,~resources:Layout.Layout,~route:Layout.Route,~valid:{Layout.grant_valid(~resources,~route) == True{} : Bool},~payload:{Layout.payload(resources,route) == Store.Store<S> : Type},~view:{Layout.view(resources,route) == Store.View<S> : Data},~protected:{Layout.policy(resources,route) == Policy.ProtectedLifetimeScope{S} : Policy.Policy},previous:Program(~S,~resources)) -> Program(~S,~resources):
51  expected => actual => allocator => owners => +requested => request => tick => selected(~S,~resources,~route,~payload,~view,Route.route_equal(requested,route),previous,expected,actual,allocator,owners,requested,request,tick)