restore_public.bend
bendvy-ecs-core@0.1.0.4 · 70 lines · 10 definitions
1import Base
2import bendvy-ecs-core@0.1.0.4/change_tick.bend as Tick
3import bendvy-ecs-core@0.1.0.4/restore_data.bend as R
4import bendvy-ecs-core@0.1.0.4/restore_schema.bend as Schema
5import bendvy-ecs-core@0.1.0.4/restore_prepare.bend as Prepare
6import bendvy-ecs-core@0.1.0.4/restore_apply.bend as Apply
7import bendvy-ecs-core@0.1.0.4/world.bend as W
8import bendvy-ecs-core@0.1.0.4/snapshot_data.bend as Snapshot
9import bendvy-ecs-core@0.1.0.4/decode_data.bend as D
10
11
12
13
14
15
16
17def prepare(~S:Data,~C:Type,~Resources:Type,~E:Data,~State:Type,~Custom:Data,~stage:State -> R.Position -> D.Field -> State & R.Dispatch<Custom>,~release:State -> State,~clear:C -> W.Handle<S> -> C & List<&2,R.Change<S>>,~insert:C -> Resources -> State -> List<&2,R.Binding<S>> -> Tick.Tick -> (C & Resources) & State,~Recovery:Type,~commit:State -> State,~takeRecovery:State -> State & Recovery,schema:Schema.Schema<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery>,state:State,input:Snapshot.Snapshot) -> R.Prepared<State,Custom>:
18 Prepare.prepare(~State,~Custom,~stage,~release,state,input)
19
20
21
22
23
24
25
26
27def apply(~S:Data,~C:Type,~Resources:Type,~E:Data,~State:Type,~Custom:Data,~stage:State -> R.Position -> D.Field -> State & R.Dispatch<Custom>,~release:State -> State,~clear:C -> W.Handle<S> -> C & List<&2,R.Change<S>>,~insert:C -> Resources -> State -> List<&2,R.Binding<S>> -> Tick.Tick -> (C & Resources) & State,~Recovery:Type,~commit:State -> State,~takeRecovery:State -> State & Recovery,~emit:R.Change<S> -> E,schema:Schema.Schema<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery>,world:W.World<S,C,Resources,E>,prepared:R.Prepared<State,Custom>) -> Apply.ApplyResult<S,C,Resources,E,State,Custom>:
28 Apply.apply(~S,~C,~Resources,~E,~State,~Custom,~release,~clear,~insert,~emit,world,prepared)
29
30
31
32
33def restore(~S:Data,~C:Type,~Resources:Type,~E:Data,~State:Type,~Custom:Data,~stage:State -> R.Position -> D.Field -> State & R.Dispatch<Custom>,~release:State -> State,~clear:C -> W.Handle<S> -> C & List<&2,R.Change<S>>,~insert:C -> Resources -> State -> List<&2,R.Binding<S>> -> Tick.Tick -> (C & Resources) & State,~Recovery:Type,~commit:State -> State,~takeRecovery:State -> State & Recovery,~emit:R.Change<S> -> E,+schema:Schema.Schema<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery>,world:W.World<S,C,Resources,E>,state:State,input:Snapshot.Snapshot) -> Apply.ApplyResult<S,C,Resources,E,State,Custom>:
34 apply(~S,~C,~Resources,~E,~State,~Custom,~stage,~release,~clear,~insert,~Recovery,~commit,~takeRecovery,~emit,schema,world,prepare(~S,~C,~Resources,~E,~State,~Custom,~stage,~release,~clear,~insert,~Recovery,~commit,~takeRecovery,schema,state,input))
35
36
37
38type Outcome<S:Data,Custom:Data> is Data:
39 Restored{bindings:List<&2,R.Binding<S>>}
40 RestoreRejected{input:Snapshot.Snapshot,error:R.Error<Custom>}
41
42
43
44type Owner<-S:Data,-C:Type,-Resources:Type,-State:Type,-Custom:Data,-stage:State -> R.Position -> D.Field -> State & R.Dispatch<Custom>,-release:State -> State,-clear:C -> W.Handle<S> -> C & List<&2,R.Change<S>>,-insert:C -> Resources -> State -> List<&2,R.Binding<S>> -> Tick.Tick -> (C & Resources) & State,-Recovery:Type,-commit:State -> State,-takeRecovery:State -> State & Recovery> is Type:
45 Owner{schema:Schema.Schema<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery>,state:State}
46
47
48def owned(~S:Data,~C:Type,~Resources:Type,~E:Data,~State:Type,~Custom:Data,~stage:State -> R.Position -> D.Field -> State & R.Dispatch<Custom>,~release:State -> State,~clear:C -> W.Handle<S> -> C & List<&2,R.Change<S>>,~insert:C -> Resources -> State -> List<&2,R.Binding<S>> -> Tick.Tick -> (C & Resources) & State,~Recovery:Type,~commit:State -> State,~takeRecovery:State -> State & Recovery,schema:Schema.Schema<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery>,state:State) -> Owner<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery>:
49 Owner{schema,state}
50def recovered(~S:Data,~C:Type,~Resources:Type,~E:Data,~State:Type,~Custom:Data,~stage:State -> R.Position -> D.Field -> State & R.Dispatch<Custom>,~release:State -> State,~clear:C -> W.Handle<S> -> C & List<&2,R.Change<S>>,~insert:C -> Resources -> State -> List<&2,R.Binding<S>> -> Tick.Tick -> (C & Resources) & State,~Recovery:Type,~commit:State -> State,~takeRecovery:State -> State & Recovery,schema:Schema.Schema<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery>,world:W.World<S,C,Resources,E>,outcome:Outcome<S,Custom>,observed:State & Recovery) -> W.World<S,C,Resources,E> & (Owner<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery> & (Outcome<S,Custom> & Recovery)):
51 match observed:
52 case (state,recovery):(world,(Owner{schema,state},(outcome,recovery)))
53def run_returned(~S:Data,~C:Type,~Resources:Type,~E:Data,~State:Type,~Custom:Data,~stage:State -> R.Position -> D.Field -> State & R.Dispatch<Custom>,~release:State -> State,~clear:C -> W.Handle<S> -> C & List<&2,R.Change<S>>,~insert:C -> Resources -> State -> List<&2,R.Binding<S>> -> Tick.Tick -> (C & Resources) & State,~Recovery:Type,~commit:State -> State,~takeRecovery:State -> State & Recovery,schema:Schema.Schema<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery>,result:Apply.ApplyResult<S,C,Resources,E,State,Custom>) -> W.World<S,C,Resources,E> & (Owner<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery> & (Outcome<S,Custom> & Recovery)):
54 match result:
55 case Apply.Applied{world,state,bindings}:recovered(~S,~C,~Resources,~E,~State,~Custom,~stage,~release,~clear,~insert,~Recovery,~commit,~takeRecovery,schema,world,Restored{bindings},takeRecovery(commit(state)))
56 case Apply.Refused{world,state,input,error}:recovered(~S,~C,~Resources,~E,~State,~Custom,~stage,~release,~clear,~insert,~Recovery,~commit,~takeRecovery,schema,world,RestoreRejected{input,error},takeRecovery(release(state)))
57
58
59
60
61
62def run(~S:Data,~C:Type,~Resources:Type,~E:Data,~State:Type,~Custom:Data,~stage:State -> R.Position -> D.Field -> State & R.Dispatch<Custom>,~release:State -> State,~clear:C -> W.Handle<S> -> C & List<&2,R.Change<S>>,~insert:C -> Resources -> State -> List<&2,R.Binding<S>> -> Tick.Tick -> (C & Resources) & State,~Recovery:Type,~commit:State -> State,~takeRecovery:State -> State & Recovery,~emit:R.Change<S> -> E,owner:Owner<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery>,world:W.World<S,C,Resources,E>,input:Snapshot.Snapshot) -> W.World<S,C,Resources,E> & (Owner<S,C,Resources,State,Custom,stage,release,clear,insert,Recovery,commit,takeRecovery> & (Outcome<S,Custom> & Recovery)):
63 match owner:
64 case Owner{+schema,state}:run_returned(~S,~C,~Resources,~E,~State,~Custom,~stage,~release,~clear,~insert,~Recovery,~commit,~takeRecovery,schema,restore(~S,~C,~Resources,~E,~State,~Custom,~stage,~release,~clear,~insert,~Recovery,~commit,~takeRecovery,~emit,schema,world,state,input))
65
66
67
68
69def dispose_recovery(~Recovery:Type,recovery:Recovery) -> Unit:
70 Unit{}