import Base import relation_failure_runtime.bend as K import internal/relation_failures/domains.bend as D import world.bend as W import system.bend as Sy import event_runtime.bend as Ev # Supplemental owned Bend contract: pinned TS has no public unregister method. # Release stream membership only after actual successful Sy.dispose. type Disposal -> K.Input -> Sy.Outcome,Error,O>> is Type: ReaderDisposed{runtime: K.Runtime} ReaderDisposalRejected{runtime: K.Runtime,reader: K.Reader} def keep(found: Bool,id: U32,rest: List<&2,U32>) -> List<&2,U32>: match found: case True{}: rest case False{}: id <> rest def remove(ids: List<&2,U32>,+wanted: U32) -> List<&2,U32>: match ids: case []: [] case +id <> rest: keep(U32.is_eq(id,wanted),id,remove(rest,wanted)) def release(~S: Data,~E: Data,domains: List<&2,D.Domain>,+id: U32) -> List<&2,D.Domain>: match domains: case []: [] case D.Domain{key,queue,ids} <> rest: D.Domain{key,queue,remove(ids,id)} <> release(~S,~E,rest,id) def completed(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> K.Input -> Sy.Outcome,Error,O>,result: Sy.Disposal,Error,O,runner>,keys: List<&2,D.DescriptorKey>,domains: List<&2,D.Domain>,positions: List<&2,Ev.Position>,+id: U32,+tick: Nat,frameStart: Nat,boundary: Nat,capacity: Nat,+nextReader: U32) -> Disposal: match result: case Sy.Disposed{world}: ReaderDisposed{K.Runtime{world,release(~S,~E,domains,id),Ev.update(positions,id,tick,True{}),tick,frameStart,boundary,capacity,nextReader}} case Sy.DisposalRejected{world,registry}: ReaderDisposalRejected{K.Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader},K.Reader{registry,keys,id}} def identified(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> K.Input -> Sy.Outcome,Error,O>,runtime: K.Runtime,identity: Sy.Registry,Error,O,runner> & U32,keys: List<&2,D.DescriptorKey>) -> Disposal: match runtime: case K.Runtime{world,domains,positions,tick,frameStart,boundary,capacity,nextReader}: (registry,+id) = identity completed(~S,~C,~R,~E,~A,~Error,~O,~runner,Sy.dispose(~S,~C,~R,~E,~K.Input,~Error,~O,~runner,registry,world),keys,domains,positions,id,tick,frameStart,boundary,capacity,nextReader) def dispose(~S: Data,~C: Type,~R: Type,~E: Data,~A: Type,~Error: Data,~O: Type,~runner: W.World -> K.Input -> Sy.Outcome,Error,O>,runtime: K.Runtime,reader: K.Reader) -> Disposal: match reader: case K.Reader{registry,keys,id}: identified(~S,~C,~R,~E,~A,~Error,~O,~runner,runtime,(registry,id),keys)