1 import Base 2 import ./world.bend as W 3 import ./system.bend as Sys 4 import ./event_runtime.bend as Ev 5 import ./ordinary_event_declaration.bend as Event 6 import ./ordinary_app_access.bend as Access 7 import ./ordinary_query_declaration.bend as Query 8 # Sole retained operational Registry; debug never reads an event payload or Reader. 9 type Owner is Type: 10 Owner{declaration:Event.Declaration,registry:Registry,debug:Bool} 11 def query_mode(mode:Event.Mode) -> Query.Mode: 12 match mode: 13 case Event.Read{}:Query.Read{} 14 case Event.Write{}:Query.Write{} 15 def enabled(~S:Data,~E:Data,~Registry:Type,owner:Owner,flag:Bool) -> Owner: 16 match owner: 17 case Owner{declaration,registry,_}:Owner{declaration,registry,flag} 18 def bind(~S:Data,~E:Data,~Registry:Type,declaration:Event.Declaration,registry:Registry) -> Owner:Owner{declaration,registry,False{}} 19 def described(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> A -> Sys.Outcome,Error,O>,declaration:Event.Declaration,registry:Sys.Registry,debug:Bool) -> Owner> & Maybe<&2,Access.Entry>: 20 match declaration registry: 21 case Event.Declaration{Event.Domain{+key},+mode} Sys.Registry{+namespace,+id,+name,+access,+cursor}:(Owner{Event.Declaration{Event.Domain{key},mode},Sys.Registry{namespace,id,name,access,cursor},debug},Some{Access.Entry{Access.Events{},key,[Access.Use{namespace,id,name,query_mode(mode),Access.role(query_mode(mode))}]}}) 22 def snapshot(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> A -> Sys.Outcome,Error,O>,owner:Owner>) -> Owner> & Maybe<&2,Access.Entry>: 23 match owner: 24 case Owner{declaration,registry,False{}}:(Owner{declaration,registry,False{}},None{}) 25 case Owner{declaration,registry,True{}}:described(~S,~C,~R,~E,~A,~Error,~O,~runner,declaration,registry,True{}) 26 27 type WriterResult -> A -> Sys.Outcome,Error,O>> is Type: 28 Completed{owner:Owner>,outcome:Sys.Outcome,Error,O>} 29 Rejected{owner:Owner>,world:W.World,args:A} 30 def writer_returned(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> A -> Sys.Outcome,Error,O>,declaration:Event.Declaration,debug:Bool,result:Sys.RunResult) -> WriterResult: 31 match result: 32 case Sys.Completed{registry,outcome}:Completed{Owner{declaration,registry,debug},outcome} 33 case Sys.RegistrationRejected{registry,world,args}:Rejected{Owner{declaration,registry,debug},world,args} 34 def run_writer(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> A -> Sys.Outcome,Error,O>,owner:Owner>,world:W.World,args:A) -> WriterResult: 35 match owner: 36 case Owner{declaration,registry,debug}:writer_returned(~S,~C,~R,~E,~A,~Error,~O,~runner,declaration,debug,Sys.run(~S,~C,~R,~E,~A,~Error,~O,~runner,registry,world,args)) 37 38 type ReaderResult -> Ev.Input -> Sys.Outcome,Error,O>> is Type: 39 Ran{runtime:Ev.Runtime,reader:Ev.Reader,owner:Owner,Error,O,runner>>,completion:Ev.Completion} 40 def reader_returned(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,O>,declaration:Event.Declaration,debug:Bool,result:Ev.RunResult) -> ReaderResult: 41 match result: 42 case Ev.Ran{runtime,reader,registry,completion}:Ran{runtime,reader,Owner{declaration,registry,debug},completion} 43 def run_reader(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,O>,owner:Owner,Error,O,runner>>,runtime:Ev.Runtime,reader:Ev.Reader,args:A) -> ReaderResult: 44 match owner: 45 case Owner{declaration,registry,debug}:reader_returned(~S,~C,~R,~E,~A,~Error,~O,~runner,declaration,debug,Ev.run(~S,~C,~R,~E,~A,~Error,~O,~runner,runtime,reader,registry,args)) 46 47 type WriterRegistered -> A -> Sys.Outcome,Error,O>> is Type: 48 WriterRegistered{world:W.World,owner:Owner>} 49 WriterFailed{world:W.World} 50 def writer_registered(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> A -> Sys.Outcome,Error,O>,declaration:Event.Declaration,result:Sys.RegistrationResult) -> WriterRegistered: 51 match result: 52 case Sys.RegistrationFailed{world}:WriterFailed{world} 53 case Sys.Registered{world,registry}:WriterRegistered{world,Owner{declaration,registry,False{}}} 54 def register_writer(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> A -> Sys.Outcome,Error,O>,world:W.World,+declaration:Event.Declaration,name:String) -> WriterRegistered: 55 writer_registered(~S,~C,~R,~E,~A,~Error,~O,~runner,declaration,Event.register_writer(~S,~C,~R,~E,~A,~Error,~O,~runner,world,declaration,name)) 56 57 type ReaderRegistered -> Ev.Input -> Sys.Outcome,Error,O>> is Type: 58 ReaderRegistered{runtime:Ev.Runtime,reader:Ev.Reader,owner:Owner,Error,O,runner>>} 59 ReaderFailed{runtime:Ev.Runtime} 60 def reader_registered(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,O>,declaration:Event.Declaration,result:Ev.Registered) -> ReaderRegistered: 61 match result: 62 case Ev.RegisterFailed{runtime}:ReaderFailed{runtime} 63 case Ev.Registered{runtime,reader,registry}:ReaderRegistered{runtime,reader,Owner{declaration,registry,False{}}} 64 def register_reader(~S:Data,~C:Type,~R:Type,~E:Data,~A:Type,~Error:Data,~O:Type,~runner:W.World -> Ev.Input -> Sys.Outcome,Error,O>,runtime:Ev.Runtime,+declaration:Event.Declaration,name:String) -> ReaderRegistered: 65 reader_registered(~S,~C,~R,~E,~A,~Error,~O,~runner,declaration,Event.register_reader(~S,~C,~R,~E,~A,~Error,~O,~runner,runtime,declaration,name))