machine_queue.bend

bendvy-ecs-core@0.1.0.3 · 32 lines · 8 definitions

1import Base
2import bendvy-ecs-core@0.1.0.3/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.
6def forced_pending_generic(-V:Data,same:Bool,prior:V,value:V) -> M.Pending<V>:
7  match same:
8    case True{}:M.Queued{prior,False{}}
9    case False{}:M.Queued{value,True{}}
10def forced_pending(~V:Data,same:Bool,prior:V,value:V) -> M.Pending<V>:
11  forced_pending_generic(V,same,prior,value)
12
13def conditional_pending_generic(-V:Data,same:V -> V -> Bool,pending:M.Pending<V>,+value:V) -> M.Pending<V>:
14  match pending:
15    case M.Queued{+prior,False{}}:forced_pending_generic(V,same(prior,value),prior,value)
16    case _:M.Queued{value,True{}}
17def conditional_pending(~V:Data,~same:V -> V -> Bool,pending:M.Pending<V>,+value:V) -> M.Pending<V>:
18  conditional_pending_generic(V,same,pending,value)
19
20def conditional_generic(-S:Data,-K:Data,-V:Data,same:V -> V -> Bool,slot:M.Slot<S,K,V>,value:V) -> M.Slot<S,K,V>:
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}
24def conditional(~S:Data,~K:Data,~V:Data,~same:V -> V -> Bool,slot:M.Slot<S,K,V>,value:V) -> M.Slot<S,K,V>:
25  conditional_generic(S,K,V,same,slot,value)
26
27def queue_generic(-S:Data,-K:Data,-V:Data,same:V -> V -> Bool,slot:M.Slot<S,K,V>,value:V,skipSame:Bool) -> M.Slot<S,K,V>:
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)
31def queue(~S:Data,~K:Data,~V:Data,~same:V -> V -> Bool,slot:M.Slot<S,K,V>,value:V,skipSame:Bool) -> M.Slot<S,K,V>:
32  queue_generic(S,K,V,same,slot,value,skipSame)