execution_save_metadata.bend
bendvy-ecs-core@0.1.0.3 · 45 lines · 12 definitions
1import Base
2import bendvy-ecs-core@0.1.0.3/world.bend as W
3import bendvy-ecs-core@0.1.0.3/entity_allocator.bend as A
4import bendvy-ecs-core@0.1.0.3/entity_key.bend as Key
5import bendvy-ecs-core@0.1.0.3/clock_state.bend as Clock
6import bendvy-ecs-core@0.1.0.3/execution_lifecycle_notices.bend as Notice
7import bendvy-ecs-core@0.1.0.3/deferred_command.bend as Deferred
8
9type Allocator is Data:
10 Allocator{next:U32,high:U32,capacity:U32,depth:Nat,slots:List<&2,A.Slot>,free:List<&2,U32>,order:List<&2,Key.Key>}
11type Lifecycle<-S:Data> is Data:
12 Lifecycle{namespace:U32,channels:List<&2,Notice.Channel<S>>,nextReader:U32,active:List<&2,Notice.Active>}
13type Metadata<-S:Data> is Data:
14 Metadata{namespace:U32,allocator:Allocator,clock:Clock.State,lifecycle:Lifecycle<S>}
15
16def right_slots(left:Array<A.Slot>,leftValues:List<&2,A.Slot>,result:Array<A.Slot> & List<&2,A.Slot>) -> Array<A.Slot> & List<&2,A.Slot>:
17 match result:
18 case (right,rightValues):(ANode{left,right},List.append(&2,A.Slot,leftValues,rightValues))
19def joined_slots(leftResult:Array<A.Slot> & List<&2,A.Slot>,rightResult:Array<A.Slot> & List<&2,A.Slot>) -> Array<A.Slot> & List<&2,A.Slot>:
20 match leftResult:
21 case (left,leftValues):right_slots(left,leftValues,rightResult)
22def slots(values:Array<A.Slot>) -> Array<A.Slot> & List<&2,A.Slot>:
23 match values:
24 case ALeaf{+value}:(ALeaf{value},[value])
25 case ANode{left,right}:joined_slots(slots(left),slots(right))
26def allocator_slots(+next:U32,+high:U32,+capacity:U32,+depth:Nat,+free:List<&2,U32>,+order:List<&2,Key.Key>,result:Array<A.Slot> & List<&2,A.Slot>) -> A.Allocator & Allocator:
27 match result:
28 case (actual,values):(A.Allocator{next,high,actual,capacity,depth,free,order},Allocator{next,high,capacity,depth,values,free,order})
29def allocator(actual:A.Allocator) -> A.Allocator & Allocator:
30 match actual:
31 case A.Allocator{next,high,actual,capacity,depth,free,order}:allocator_slots(next,high,capacity,depth,free,order,slots(actual))
32def lifecycle(~S:Data,actual:Notice.Journal<S>) -> Notice.Journal<S> & Lifecycle<S>:
33 match actual:
34 case Notice.Journal{+namespace,+channels,+nextReader,+active}:(Notice.Journal{namespace,channels,nextReader,active},Lifecycle{namespace,channels,nextReader,active})
35def captured_lifecycle(~S:Data,~C:Type,~R:Type,~E:Data,+namespace:U32,actual:A.Allocator,metadata:Allocator,store:C,resource:R,events:List<&2,E>,pending:List<Deferred.Command<W.World<S,C,R,E>>>,registrations:List<&2,W.RegistrationMeta>,nextSystemId:U32,+clock:Clock.State,result:Notice.Journal<S> & Lifecycle<S>) -> W.World<S,C,R,E> & Metadata<S>:
36 match result:
37 case (journal,lifecycle):(W.World{namespace,actual,store,resource,events,pending,registrations,nextSystemId,clock,journal},Metadata{namespace,metadata,clock,lifecycle})
38def captured_allocator(~S:Data,~C:Type,~R:Type,~E:Data,namespace:U32,store:C,resource:R,events:List<&2,E>,pending:List<Deferred.Command<W.World<S,C,R,E>>>,registrations:List<&2,W.RegistrationMeta>,nextSystemId:U32,clock:Clock.State,journal:Notice.Journal<S>,result:A.Allocator & Allocator) -> W.World<S,C,R,E> & Metadata<S>:
39 match result:
40 case (actual,metadata):captured_lifecycle(~S,~C,~R,~E,namespace,actual,metadata,store,resource,events,pending,registrations,nextSystemId,clock,lifecycle(~S,journal))
41
42
43def capture(~S:Data,~C:Type,~R:Type,~E:Data,world:W.World<S,C,R,E>) -> W.World<S,C,R,E> & Metadata<S>:
44 match world:
45 case W.World{namespace,actual,store,resource,events,pending,registrations,nextSystemId,clock,journal}:captured_allocator(~S,~C,~R,~E,namespace,store,resource,events,pending,registrations,nextSystemId,clock,journal,allocator(actual))