1 import Base 2 import ./machine.bend as M 3 4 # Bevy NextState::set_if_different preserves an existing forced same target. 5 # Equality is the same closed erased V comparator used by apply/conditions. 6 def forced_pending_generic(-V:Data,same:Bool,prior:V,value:V) -> M.Pending: 7 match same: 8 case True{}:M.Queued{prior,False{}} 9 case False{}:M.Queued{value,True{}} 10 def forced_pending(~V:Data,same:Bool,prior:V,value:V) -> M.Pending: 11 forced_pending_generic(V,same,prior,value) 12 13 def conditional_pending_generic(-V:Data,same:V -> V -> Bool,pending:M.Pending,+value:V) -> M.Pending: 14 match pending: 15 case M.Queued{+prior,False{}}:forced_pending_generic(V,same(prior,value),prior,value) 16 case _:M.Queued{value,True{}} 17 def conditional_pending(~V:Data,~same:V -> V -> Bool,pending:M.Pending,+value:V) -> M.Pending: 18 conditional_pending_generic(V,same,pending,value) 19 20 def conditional_generic(-S:Data,-K:Data,-V:Data,same:V -> V -> Bool,slot:M.Slot,value:V) -> M.Slot: 21 match slot: 22 case M.Missing{}:M.Missing{} 23 case M.Present{current,pending,previous,changed}:M.Present{current,conditional_pending_generic(V,same,pending,value),previous,changed} 24 def conditional(~S:Data,~K:Data,~V:Data,~same:V -> V -> Bool,slot:M.Slot,value:V) -> M.Slot: 25 conditional_generic(S,K,V,same,slot,value) 26 27 def queue_generic(-S:Data,-K:Data,-V:Data,same:V -> V -> Bool,slot:M.Slot,value:V,skipSame:Bool) -> M.Slot: 28 match skipSame: 29 case False{}:M.queue_generic(S,K,V,slot,value,False{}) 30 case True{}:conditional_generic(S,K,V,same,slot,value) 31 def queue(~S:Data,~K:Data,~V:Data,~same:V -> V -> Bool,slot:M.Slot,value:V,skipSame:Bool) -> M.Slot: 32 queue_generic(S,K,V,same,slot,value,skipSame)