ordinary_owned_event.bend
bendvy-ecs-core@0.1.0.4 · 78 lines · 16 definitions
1import Base
2import bendvy-ecs-core@0.1.0.4/world.bend as W
3import bendvy-ecs-core@0.1.0.4/system.bend as Sys
4import bendvy-ecs-core@0.1.0.4/transaction.bend as T
5import bendvy-ecs-core@0.1.0.4/commands.bend as Cmd
6import bendvy-ecs-core@0.1.0.4/owned_event_storage.bend as Storage
7
8
9
10
11
12type Domain<-S:Data,-P:Type> is Data:
13 Domain{key:String}
14
15
16def domain(~S:Data,~P:Type,key:String) -> Domain<S,P>:Domain{key}
17
18def name(~S:Data,~P:Type,domain:Domain<S,P>) -> String:
19 match domain:
20 case Domain{key}:key
21
22
23def access(~S:Data,~P:Type,domain:Domain<S,P>,suffix:String) -> String:
24 match domain:
25 case Domain{key}:String.concat([key,suffix])
26
27
28type Frame<World:Type,P:Type> is Type:
29 Frame{tx:T.Tx<World,Nat>,newest:List<P>}
30
31
32type Write<-S:Data,-P:Type,-identity:Domain<S,P>,-H:Type> is Type:
33 Write{emit:H -> P -> H}
34
35
36def emit_field(~S:Data,~P:Type,~identity:Domain<S,P>,~H:Type,grant:Write<S,P,identity,H>) -> H -> P -> H:
37 match grant:
38 case Write{emit}:emit
39
40
41
42def emit(~S:Data,~P:Type,~identity:Domain<S,P>,~H:Type,~grant:Write<S,P,identity,H>,owner:H,payload:P) -> H:
43 emit_field(~S,~P,~identity,~H,grant)(owner,payload)
44def staged(~World:Type,~P:Type,owner:Frame<World,P>,payload:P) -> Frame<World,P>:
45 match owner:
46 case Frame{tx,newest}:Frame{tx,payload <> newest}
47
48
49
50type Reply<Error:Data,Out:Type,P:Type> is Type:
51 Reply{outcome:T.Outcome<Error>,output:Out,recovered:List<P>}
52
53def finished(~S:Data,~C:Type,~R:Type,~P:Type,~Error:Data,~Out:Type,newest:List<P>,output:Out,result:T.Returned<W.World<S,C,Storage.Resources<R,P>,Nat>,Error>) -> Sys.Outcome<W.World<S,C,Storage.Resources<R,P>,Nat>,Unit,Reply<Error,Out,P>>:
54 match result:
55 case T.Returned{world,T.Success{}}:Sys.Succeeded{Storage.commit_owned(~S,~C,~R,~P,world,List.reverse(&1,P,newest)),Reply{T.Success{},output,[]}}
56 case T.Returned{world,T.Failure{error}}:Sys.Succeeded{world,Reply{T.Failure{error},output,List.reverse(&1,P,newest)}}
57def returned(~S:Data,~C:Type,~R:Type,~P:Type,~Error:Data,~Out:Type,result:Frame<W.World<S,C,Storage.Resources<R,P>,Nat>,P> & (T.Outcome<Error> & Out)) -> Sys.Outcome<W.World<S,C,Storage.Resources<R,P>,Nat>,Unit,Reply<Error,Out,P>>:
58 match result:
59 case (Frame{tx,newest},(outcome,output)):finished(~S,~C,~R,~P,~Error,~Out,newest,output,T.finish(~W.World<S,C,Storage.Resources<R,P>,Nat>,~Nat,~Error,~Cmd.enqueue(~S,~C,~Storage.Resources<R,P>,~Nat),~Cmd.publish(~S,~C,~Storage.Resources<R,P>,~Nat),tx,outcome))
60
61
62
63def runner(~S:Data,~C:Type,~R:Type,~P:Type,~identity:Domain<S,P>,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-grant:Write<S,P,identity,H> -> Args -> H -> H & (T.Outcome<Error> & Out),world:W.World<S,C,Storage.Resources<R,P>,Nat>,args:Args) -> Sys.Outcome<W.World<S,C,Storage.Resources<R,P>,Nat>,Unit,Reply<Error,Out,P>>:
64 returned(~S,~C,~R,~P,~Error,~Out,body(Frame<W.World<S,C,Storage.Resources<R,P>,Nat>,P>,Write{staged(~W.World<S,C,Storage.Resources<R,P>,Nat>,~P)},args,Frame{T.begin(~W.World<S,C,Storage.Resources<R,P>,Nat>,~Nat,world),[]}))
65
66
67def Registry(~S:Data,~C:Type,~R:Type,~P:Type,~identity:Domain<S,P>,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-grant:Write<S,P,identity,H> -> Args -> H -> H & (T.Outcome<Error> & Out)) -> Type:
68 Sys.Registry<S,C,Storage.Resources<R,P>,Nat,Args,Unit,Reply<Error,Out,P>,runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body)>
69
70
71def define(~S:Data,~C:Type,~R:Type,~P:Type,~identity:Domain<S,P>,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-grant:Write<S,P,identity,H> -> Args -> H -> H & (T.Outcome<Error> & Out),world:W.World<S,C,Storage.Resources<R,P>,Nat>,name:String) -> Sys.RegistrationResult<S,C,Storage.Resources<R,P>,Nat,Args,Unit,Reply<Error,Out,P>,runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body)>:
72 Sys.register(~S,~C,~Storage.Resources<R,P>,~Nat,~Args,~Unit,~Reply<Error,Out,P>,~runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body),world,name,[access(~S,~P,identity,":write")])
73
74
75
76
77def run(~S:Data,~C:Type,~R:Type,~P:Type,~identity:Domain<S,P>,~Args:Type,~Error:Data,~Out:Type,~body:@-H:Type -> @-grant:Write<S,P,identity,H> -> Args -> H -> H & (T.Outcome<Error> & Out),registry:Sys.Registry<S,C,Storage.Resources<R,P>,Nat,Args,Unit,Reply<Error,Out,P>,runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body)>,world:W.World<S,C,Storage.Resources<R,P>,Nat>,args:Args) -> Sys.RunResult<S,C,Storage.Resources<R,P>,Nat,Args,Unit,Reply<Error,Out,P>,runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body)>:
78 Sys.run(~S,~C,~Storage.Resources<R,P>,~Nat,~Args,~Unit,~Reply<Error,Out,P>,~runner(~S,~C,~R,~P,~identity,~Args,~Error,~Out,~body),registry,world,args)