import Base import ./restore_data.bend as R import ./snapshot_leaf.bend as Leaf import ./ordinary_declaration.bend as Identity import ./persistence_declaration.bend as Decl import ./decode_data.bend as D import ./column.bend as Col import ./world.bend as W import ./owner_product.bend as Product # Called only after complete staging and capacity admission. Old payloads leave # scope here; no inverse, destructor or finalizer callback is executed. def component_cleared(~S:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,declaration:Decl.Constructed,+handle:W.Handle,observed:Col.SwapOutcome) -> Leaf.Component & List<&2,R.Change>: match observed: case Col.Accepted{column,Some{owner}}:(Leaf.Component{column,declaration},[R.Removed{handle,Identity.key(~S,~P,identity)}]) case Col.Accepted{column,None{}}:(Leaf.Component{column,declaration},[]) case Col.Rejected{column,_}:(Leaf.Component{column,declaration},[]) def component(~S:Data,~P:Type,~identity:Identity.Declaration,~project:P -> P & D.Raw,owner:Leaf.Component,+handle:W.Handle) -> Leaf.Component & List<&2,R.Change>: match owner: case Leaf.Component{column,declaration}:component_cleared(~S,~P,~identity,~project,declaration,handle,Col.clear(~S,~P,column,W.handle_index(~S,handle))) def transient_cleared(~S:Data,~P:Type,~identity:Identity.Declaration,declaration:Decl.Transient,+handle:W.Handle,observed:Col.SwapOutcome) -> Leaf.TransientComponent & List<&2,R.Change>: match observed: case Col.Accepted{column,Some{owner}}:(Leaf.TransientComponent{column,declaration},[R.Removed{handle,Identity.key(~S,~P,identity)}]) case Col.Accepted{column,None{}}:(Leaf.TransientComponent{column,declaration},[]) case Col.Rejected{column,_}:(Leaf.TransientComponent{column,declaration},[]) def transient_component_owned(~S:Data,~P:Type,~identity:Identity.Declaration,owner:Leaf.TransientComponent,+handle:W.Handle) -> Leaf.TransientComponent & List<&2,R.Change>: match owner: case Leaf.TransientComponent{column,declaration}:transient_cleared(~S,~P,~identity,declaration,handle,Col.clear(~S,~P,column,W.handle_index(~S,handle))) def transient_component(~S:Data,~P:Type,~identity:Identity.Declaration,owner:Leaf.TransientComponent,handle:W.Handle) -> Leaf.TransientComponent & List<&2,R.Change>: transient_component_owned(~S,~P,~identity,owner,handle) def unit(~S:Data,owner:Unit,handle:W.Handle) -> Unit & List<&2,R.Change>:(owner,[]) def product_join(~S:Data,~LC:Type,~RC:Type,left:LC,events:List<&2,R.Change>,observed:RC & List<&2,R.Change>) -> Product.Owner & List<&2,R.Change>: match observed: case (right,more):(Product.Owner{left,right},List.append(&2,R.Change,events,more)) def product_left(~S:Data,~LC:Type,~RC:Type,~right:RC -> W.Handle -> RC & List<&2,R.Change>,owner:RC,handle:W.Handle,observed:LC & List<&2,R.Change>) -> Product.Owner & List<&2,R.Change>: match observed: case (left,events):product_join(~S,~LC,~RC,left,events,right(owner,handle)) def product(~S:Data,~LC:Type,~RC:Type,~left:LC -> W.Handle -> LC & List<&2,R.Change>,~right:RC -> W.Handle -> RC & List<&2,R.Change>,owner:Product.Owner,+handle:W.Handle) -> Product.Owner & List<&2,R.Change>: match owner: case Product.Owner{a,b}:product_left(~S,~LC,~RC,~right,b,handle,left(a,handle))