ordinary_owned_event_family_resource.bend

bendvy-ecs-core@0.1.0.1 · 36 lines · 0 definitions

1import Base
2import bendvy-ecs-core@0.1.0.1/change_tick.bend as Tick
3import bendvy-ecs-core@0.1.0.1/world.bend as W
4import bendvy-ecs-core@0.1.0.1/compose.bend as Q
5import bendvy-ecs-core@0.1.0.1/capabilities.bend as Cap
6import bendvy-ecs-core@0.1.0.1/ordinary_query_declaration.bend as Decl
7import bendvy-ecs-core@0.1.0.1/ordinary_schema.bend as Schema
8import bendvy-ecs-core@0.1.0.1/owner_product.bend as Product
9import bendvy-ecs-core@0.1.0.1/resource_field.bend as Field
10import bendvy-ecs-core@0.1.0.1/transaction.bend as T
11# Only the user resource is writable; the affine event log is a retained sibling.
12def lens(~R:Type,~Logs:Type) -> Product.Lens<Product.Owner<R,Logs>,R,Logs>:
13  Product.lens(~R,~Logs,~Product.Left{})
14def read_returned(~S:Data,~C:Type,~R:Type,~Logs:Type,~E:Data,~V:Data,handle:W.Handle<S>,error:Maybe<&2,W.WorldError>,cursor:Maybe<&2,Tick.Tick>,result:T.Tx<W.World<S,C,Product.Owner<R,Logs>,E>,E> & V) -> Q.Frame<S,C,Product.Owner<R,Logs>,E> & V:
15  match result:
16    case (tx,value):(Q.Frame{tx,handle,error,cursor},value)
17# Advanced opaque-row read of the user resource R in Product.Owner<R,Logs>.
18# The selected field projection returns its owner and detached V; log siblings
19# remain retained. Raw schema lens/view callbacks are trusted setup.
20def read(~S:Data,~C:Type,~R:Type,~Logs:Type,~E:Data,~V:Data,~view:R -> R & V,owner:Q.Frame<S,C,Product.Owner<R,Logs>,E>) -> Q.Frame<S,C,Product.Owner<R,Logs>,E> & V:
21  match owner:
22    case Q.Frame{tx,handle,error,cursor}:read_returned(~S,~C,~R,~Logs,~E,~V,handle,error,cursor,Cap.value_read(~T.Tx<W.World<S,C,Product.Owner<R,Logs>,E>,E>,~V,~Field.read(~S,~C,~Product.Owner<R,Logs>,~E,~R,~Logs,~V,~lens(~R,~Logs),~view),tx))
23# Advanced transactional replacement of only user resource R, retaining
24# affine Logs as siblings. Consumes replacement and returns the row owner;
25# journal rollback restores the displaced resource, without exposing log writes.
26def set(~S:Data,~C:Type,~R:Type,~Logs:Type,~E:Data,owner:Q.Frame<S,C,Product.Owner<R,Logs>,E>,replacement:R) -> Q.Frame<S,C,Product.Owner<R,Logs>,E>:
27  match owner:
28    case Q.Frame{tx,handle,error,cursor}:Q.Frame{Field.tx_set(~S,~C,~Product.Owner<R,Logs>,~E,~R,~Logs,~lens(~R,~Logs),tx,replacement),handle,error,cursor}
29def selected(~S:Data,~C:Type,~R:Type,~Logs:Type,~E:Data,owner:Q.Frame<S,C,Product.Owner<R,Logs>,E>) -> Q.Frame<S,C,Product.Owner<R,Logs>,E> & Bool:
30  (owner,True{})
31# Declare user-resource read/replacement using its ordinary schema identity.
32# Selection is unconditional and access metadata derives from that identity.
33# The typed field grant retains affine log owners; constructing this declaration
34# neither reads resources nor publishes events.
35def write(~S:Data,~C:Type,~R:Type,~Logs:Type,~E:Data,~V:Data,~identity:Schema.Resource<S,R>,~view:R -> R & V) -> Decl.Declaration<S,Q.Frame<S,C,Product.Owner<R,Logs>,E>,Cap.ValueWrite<Q.Frame<S,C,Product.Owner<R,Logs>,E>,R,V>>:
36  Decl.Declaration{Cap.ValueWrite{read(~S,~C,~R,~Logs,~E,~V,~view),set(~S,~C,~R,~Logs,~E)},selected(~S,~C,~R,~Logs,~E),[Decl.Clause{Schema.resource_key(~S,~R,identity),Decl.Write{}}]}