import Base import ./machine.bend as M import ./machine_stream_queue.bend as Queue import ./execution_stream_channel.bend as Channel import ./execution_composer_layout.bend as Layout import ./change_tick.bend as Tick # Public machine constructors retain their existing shape. Message sequence, # namespace checks, reading and two-generation retention use the shared channel. type Store<-V:Data> is Data: Store{queue:Queue.Queue>,olderThrough:Nat,maintenance:Maybe<&2,Tick.Tick>} type Reader<-S:Data,-K:Data,-V:Data,-origin:Layout.Layout,-route:Layout.Route> is Type: Reader{namespace:U32,id:U32,cursor:Nat} type Reading<-S:Data,-K:Data,-V:Data,-origin:Layout.Layout,-route:Layout.Route> is Type: Read{reader:Reader,events:List<&2,M.Transition>,missed:Nat,tick:Tick.Tick} Refused{reader:Reader} def channel(~V:Data,store:Store) -> Channel.Store>: match store: case Store{queue,olderThrough,maintenance}:Channel.Store{queue,olderThrough,maintenance} def stored(~V:Data,store:Channel.Store>) -> Store: match store: case Channel.Store{queue,olderThrough,maintenance}:Store{queue,olderThrough,maintenance} def empty(~V:Data) -> Store:stored(~V,Channel.empty(~M.Transition)) def publish(~V:Data,store:Store,sequence:Nat,event:M.Transition) -> Store: stored(~V,Channel.publish(~M.Transition,channel(~V,store),sequence,event)) def maintained(~V:Data,+sequence:Nat,tick:Tick.Tick,olderThrough:Nat,queue:Queue.Queue>) -> Store: stored(~V,Channel.maintained(~M.Transition,sequence,tick,olderThrough,queue)) def maintain(~V:Data,sequence:Nat,tick:Tick.Tick,store:Store) -> Store: stored(~V,Channel.maintain(~M.Transition,sequence,tick,channel(~V,store))) def missed(~V:Data,queue:Queue.Queue>,cursor:Nat) -> Nat: Channel.missed(~M.Transition,queue,cursor) def returned(~S:Data,~K:Data,~V:Data,~origin:Layout.Layout,~route:Layout.Route,reader:Reader,result:Channel.Store> & Channel.Reading>) -> Store & Reading: match reader result: case reader, (store,Channel.Refused{}):(stored(~V,store),Refused{reader}) case Reader{namespace,id,_}, (store,Channel.Read{cursor,events,missed,tick}):(stored(~V,store),Read{Reader{namespace,id,cursor},events,missed,tick}) def reading(~S:Data,~K:Data,~V:Data,~origin:Layout.Layout,~route:Layout.Route,+sequence:Nat,tick:Tick.Tick,reader:Reader,store:Store) -> Store & Reading: match reader: case Reader{namespace,id,+cursor}:returned(~S,~K,~V,~origin,~route,Reader{namespace,id,cursor},Channel.reading(~M.Transition,sequence,tick,cursor,channel(~V,store))) def checked(~S:Data,~K:Data,~V:Data,~origin:Layout.Layout,~route:Layout.Route,allowed:Bool,sequence:Nat,tick:Tick.Tick,reader:Reader,store:Store) -> Store & Reading: match reader: case Reader{namespace,id,+cursor}:returned(~S,~K,~V,~origin,~route,Reader{namespace,id,cursor},Channel.checked(~M.Transition,allowed,sequence,tick,cursor,channel(~V,store))) def read(~S:Data,~K:Data,~V:Data,~origin:Layout.Layout,~route:Layout.Route,namespace:Maybe<&2,U32>,sequence:Nat,tick:Tick.Tick,reader:Reader,store:Store) -> Store & Reading: match reader: case Reader{+actual,id,+cursor}:returned(~S,~K,~V,~origin,~route,Reader{actual,id,cursor},Channel.read(~M.Transition,namespace,actual,sequence,tick,cursor,channel(~V,store)))