import Base import ./world.bend as W import ./system.bend as Sys import ./event_runtime.bend as Ev import ./ordinary_event_declaration.bend as Event import ./ordinary_app_access.bend as Access import ./ordinary_query_declaration.bend as Query # Sole retained operational Registry; debug never reads an event payload or Reader. type Owner is Type: Owner{declaration:Event.Declaration,registry:Registry,debug:Bool} def query_mode(mode:Event.Mode) -> Query.Mode: match mode: case Event.Read{}:Query.Read{} case Event.Write{}:Query.Write{} def enabled(~S:Data,~E:Data,~Registry:Type,owner:Owner,flag:Bool) -> Owner: match owner: case Owner{declaration,registry,_}:Owner{declaration,registry,flag} def bind(~S:Data,~E:Data,~Registry:Type,declaration:Event.Declaration,registry:Registry) -> Owner:Owner{declaration,registry,False{}} 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>: match declaration registry: 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))}]}}) 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>: match owner: case Owner{declaration,registry,False{}}:(Owner{declaration,registry,False{}},None{}) case Owner{declaration,registry,True{}}:described(~S,~C,~R,~E,~A,~Error,~O,~runner,declaration,registry,True{}) type WriterResult -> A -> Sys.Outcome,Error,O>> is Type: Completed{owner:Owner>,outcome:Sys.Outcome,Error,O>} Rejected{owner:Owner>,world:W.World,args:A} 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: match result: case Sys.Completed{registry,outcome}:Completed{Owner{declaration,registry,debug},outcome} case Sys.RegistrationRejected{registry,world,args}:Rejected{Owner{declaration,registry,debug},world,args} 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: match owner: 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)) type ReaderResult -> Ev.Input -> Sys.Outcome,Error,O>> is Type: Ran{runtime:Ev.Runtime,reader:Ev.Reader,owner:Owner,Error,O,runner>>,completion:Ev.Completion} 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: match result: case Ev.Ran{runtime,reader,registry,completion}:Ran{runtime,reader,Owner{declaration,registry,debug},completion} 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: match owner: 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)) type WriterRegistered -> A -> Sys.Outcome,Error,O>> is Type: WriterRegistered{world:W.World,owner:Owner>} WriterFailed{world:W.World} 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: match result: case Sys.RegistrationFailed{world}:WriterFailed{world} case Sys.Registered{world,registry}:WriterRegistered{world,Owner{declaration,registry,False{}}} 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: 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)) type ReaderRegistered -> Ev.Input -> Sys.Outcome,Error,O>> is Type: ReaderRegistered{runtime:Ev.Runtime,reader:Ev.Reader,owner:Owner,Error,O,runner>>} ReaderFailed{runtime:Ev.Runtime} 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: match result: case Ev.RegisterFailed{runtime}:ReaderFailed{runtime} case Ev.Registered{runtime,reader,registry}:ReaderRegistered{runtime,reader,Owner{declaration,registry,False{}}} 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: 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))