ordinary_app_linked_cleanup.bend

bendvy-ecs-core@0.1.0.0 · 63 lines · 0 definitions

1import Base
2import bendvy-ecs-core@0.1.0.0/deferred_command.bend as Deferred
3import bendvy-ecs-core@0.1.0.0/trace_facts.bend as TraceFacts
4import bendvy-ecs-core@0.1.0.0/ordinary_app_composer.bend as Build
5import bendvy-ecs-core@0.1.0.0/ordinary_app_recipe.bend as Tree
6import bendvy-ecs-core@0.1.0.0/ordinary_app_observation_data.bend as DTO
7import bendvy-ecs-core@0.1.0.0/ordinary_app_registration_fields.bend as Fields
8import bendvy-ecs-core@0.1.0.0/ordinary_app_relation.bend as Relation
9import bendvy-ecs-core@0.1.0.0/relation_failure_reader.bend as Ord
10import bendvy-ecs-core@0.1.0.0/relation_types.bend as G
11import bendvy-ecs-core@0.1.0.0/relation_commands.bend as A
12import bendvy-ecs-core@0.1.0.0/relation_cleanup.bend as Cleanup
13import bendvy-ecs-core@0.1.0.0/world.bend as W
14import bendvy-ecs-core@0.1.0.0/transaction.bend as T
15import bendvy-ecs-core@0.1.0.0/commands.bend as Cmd
16
17# Trusted setup captures component-binding-derived clear and declaration installs.
18# This wrapper cannot infer declarations from an arbitrary existing App recipe.
19type Installed<S:Data,C:Type,R:Type,E:Data,Owners:Type,Rows:Data,-observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,-fold:Owners -> Owners & List<&2,Fields.Fields<S>>,-declarations:List<&2,Ord.Declaration<S>>,-clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,-record:W.Handle<S> -> A.Notice<S,E>> is Type:
20  Installed{app:Build.Registered<S,Relation.World(~S,~C,~R,~E),Owners,Rows,observe,fold>}
21def descriptors(~S:Data,declarations:List<&2,Ord.Declaration<S>>) -> List<&2,G.Descriptor<S>>:
22  match declarations:
23    case []:[]
24    case Ord.Declaration{descriptor} <> rest:descriptor <> descriptors(~S,rest)
25def empty(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,app:Build.Registered<S,Relation.World(~S,~C,~R,~E),Owners,Rows,observe,fold>) -> Installed<S,C,R,E,Owners,Rows,observe,fold,[],clear,record>:
26  Installed{app}
27def install_relation(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,~declaration:Ord.Declaration<S>,installed:Installed<S,C,R,E,Owners,Rows,observe,fold,declarations,clear,record>) -> Installed<S,C,R,E,Owners,Relation.ObservedRows(~S,~Rows),Tree.observe_pair(~Relation.World(~S,~C,~R,~E),~Rows,~Relation.View<S>,~observe,~Relation.observe(~S,~C,~R,~E,~declaration,~Relation.builtin_row(~S))),fold,List.append(&2,Ord.Declaration<S>,declarations,[declaration]),clear,record>:
28  match installed:
29    case Installed{app}:Installed{Relation.install(~S,~C,~R,~E,~declaration,~Owners,~Rows,~observe,~fold,app)}
30def result(~S:Data,~C:Type,~R:Type,~E:Data,result:Cleanup.CleanupResult<S,C,R,E>) -> Relation.World(~S,~C,~R,~E):
31  match result:
32    case Cleanup.Complete{world}:world
33    case Cleanup.Incomplete{world,frames}:W.event_publish(~S,~A.Store<S,C>,~R,~A.Notice<S,E>,world,[A.CleanupPaused{frames}])
34def apply(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,world:Relation.World(~S,~C,~R,~E),handle:W.Handle<S>) -> Relation.World(~S,~C,~R,~E):
35  result(~S,~C,~R,~E,Cleanup.automatic(~S,~C,~R,~E,~clear,~record,world,handle,descriptors(~S,declarations)))
36def checked(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,allowed:Bool,world:Relation.World(~S,~C,~R,~E),undo:List<(Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E))>,commands:List<Deferred.Command<Relation.World(~S,~C,~R,~E)>>,events:List<&2,A.Notice<S,E>>,trace:TraceFacts.State,handle:W.Handle<S>) -> T.Tx<Relation.World(~S,~C,~R,~E),A.Notice<S,E>> & A.QueueStatus:
37  match allowed:
38    case False{}:(T.Tx{world,undo,commands,events,trace},A.MissingEntity{})
39    case True{}:(T.Tx{world,undo,Deferred.Raw{(world => apply(~S,~C,~R,~E,~declarations,~clear,~record,world,handle))} <> commands,events,trace},A.Queued{})
40def validated(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,handle:W.Handle<S>,undo:List<(Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E))>,commands:List<Deferred.Command<Relation.World(~S,~C,~R,~E)>>,events:List<&2,A.Notice<S,E>>,trace:TraceFacts.State,seen:Relation.World(~S,~C,~R,~E) & Bool) -> T.Tx<Relation.World(~S,~C,~R,~E),A.Notice<S,E>> & A.QueueStatus:
41  match seen:
42    case (world,allowed):checked(~S,~C,~R,~E,~declarations,~clear,~record,allowed,world,undo,commands,events,trace,handle)
43def queue(~S:Data,~C:Type,~R:Type,~E:Data,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,tx:T.Tx<Relation.World(~S,~C,~R,~E),A.Notice<S,E>>,+handle:W.Handle<S>) -> T.Tx<Relation.World(~S,~C,~R,~E),A.Notice<S,E>> & A.QueueStatus:
44  match tx:
45    case T.Tx{world,undo,commands,events,trace}:validated(~S,~C,~R,~E,~declarations,~clear,~record,handle,undo,commands,events,trace,W.valid(~S,~A.Store<S,C>,~R,~A.Notice<S,E>,world,handle))
46def returned(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,~Error:Data,owners:Owners,recipe:Tree.Recipe<Relation.World(~S,~C,~R,~E),Rows,observe>,debug:Bool,status:A.QueueStatus,seen:T.Returned<Relation.World(~S,~C,~R,~E),Error>) -> Installed<S,C,R,E,Owners,Rows,observe,fold,declarations,clear,record> & DTO.Product<A.QueueStatus,T.Outcome<Error>>:
47  match seen:
48    case T.Returned{world,outcome}:(Installed{Build.Registered{world,owners,recipe,debug}},DTO.Product{status,outcome})
49def queued(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,~Error:Data,owners:Owners,recipe:Tree.Recipe<Relation.World(~S,~C,~R,~E),Rows,observe>,debug:Bool,outcome:T.Outcome<Error>,seen:T.Tx<Relation.World(~S,~C,~R,~E),A.Notice<S,E>> & A.QueueStatus) -> Installed<S,C,R,E,Owners,Rows,observe,fold,declarations,clear,record> & DTO.Product<A.QueueStatus,T.Outcome<Error>>:
50  match seen:
51    case (tx,status):returned(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~declarations,~clear,~record,~Error,owners,recipe,debug,status,Cmd.tx_finish(~S,~A.Store<S,C>,~R,~A.Notice<S,E>,~Error,tx,outcome))
52def cleanup_app(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,~Error:Data,installed:Installed<S,C,R,E,Owners,Rows,observe,fold,declarations,clear,record>,handle:W.Handle<S>,outcome:T.Outcome<Error>) -> Installed<S,C,R,E,Owners,Rows,observe,fold,declarations,clear,record> & DTO.Product<A.QueueStatus,T.Outcome<Error>>:
53  match installed:
54    case Installed{Build.Registered{world,owners,recipe,debug}}:queued(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~declarations,~clear,~record,~Error,owners,recipe,debug,outcome,queue(~S,~C,~R,~E,~declarations,~clear,~record,T.begin(~Relation.World(~S,~C,~R,~E),~A.Notice<S,E>,world),handle))
55def barrier_app(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,installed:Installed<S,C,R,E,Owners,Rows,observe,fold,declarations,clear,record>) -> Installed<S,C,R,E,Owners,Rows,observe,fold,declarations,clear,record>:
56  match installed:
57    case Installed{Build.Registered{world,owners,recipe,debug}}:Installed{Build.Registered{W.barrier(~S,~A.Store<S,C>,~R,~A.Notice<S,E>,world),owners,recipe,debug}}
58def snapshot_returned(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,seen:Build.Registered<S,Relation.World(~S,~C,~R,~E),Owners,Rows,observe,fold> & Maybe<&2,DTO.Product<List<&2,DTO.Binding<S>>,DTO.Observation<Rows>>>) -> Installed<S,C,R,E,Owners,Rows,observe,fold,declarations,clear,record> & Maybe<&2,DTO.Product<List<&2,DTO.Binding<S>>,DTO.Observation<Rows>>>:
59  match seen:
60    case (app,view):(Installed{app},view)
61def snapshot_app(~S:Data,~C:Type,~R:Type,~E:Data,~Owners:Type,~Rows:Data,~observe:Relation.World(~S,~C,~R,~E) -> Relation.World(~S,~C,~R,~E) & DTO.Observation<Rows>,~fold:Owners -> Owners & List<&2,Fields.Fields<S>>,~declarations:List<&2,Ord.Declaration<S>>,~clear:C -> W.Handle<S> -> C & List<&2,A.Notice<S,E>>,~record:W.Handle<S> -> A.Notice<S,E>,installed:Installed<S,C,R,E,Owners,Rows,observe,fold,declarations,clear,record>,flag:Bool) -> Installed<S,C,R,E,Owners,Rows,observe,fold,declarations,clear,record> & Maybe<&2,DTO.Product<List<&2,DTO.Binding<S>>,DTO.Observation<Rows>>>:
62  match installed:
63    case Installed{app}:snapshot_returned(~S,~C,~R,~E,~Owners,~Rows,~observe,~fold,~declarations,~clear,~record,Build.snapshot(~S,~Relation.World(~S,~C,~R,~E),~Owners,~Rows,~observe,~fold,Build.enabled(~S,~Relation.World(~S,~C,~R,~E),~Owners,~Rows,~observe,~fold,app,flag)))