@@ -30,6 +30,7 @@ import {
3030import { callProcedure } from './procedures' ;
3131import type { Reducers } from './reducers' ;
3232import {
33+ type AliasViews ,
3334 type AuthCtx ,
3435 type JsonObject ,
3536 type JwtClaims ,
@@ -195,18 +196,21 @@ export const ReducerCtxImpl = class ReducerCtx<
195196 timestamp : Timestamp ;
196197 connectionId : ConnectionId | null ;
197198 db : DbView < SchemaDef > ;
199+ as : AliasViews < SchemaDef > ;
198200
199201 constructor (
200202 sender : Identity ,
201203 timestamp : Timestamp ,
202204 connectionId : ConnectionId | null ,
203- dbView : DbView < any >
205+ dbView : DbView < any > ,
206+ asViews : object = { }
204207 ) {
205208 Object . seal ( this ) ;
206209 this . sender = sender ;
207210 this . timestamp = timestamp ;
208211 this . connectionId = connectionId ;
209212 this . db = dbView as unknown as DbView < SchemaDef > ;
213+ this . as = asViews as AliasViews < SchemaDef > ;
210214 }
211215
212216 /** Reset the `ReducerCtx` to be used for a new transaction */
@@ -215,7 +219,8 @@ export const ReducerCtxImpl = class ReducerCtx<
215219 sender : Identity ,
216220 timestamp : Timestamp ,
217221 connectionId : ConnectionId | null ,
218- dbView ?: DbView < any >
222+ dbView ?: DbView < any > ,
223+ asViews ?: object
219224 ) {
220225 me . sender = sender ;
221226 me . timestamp = timestamp ;
@@ -225,6 +230,9 @@ export const ReducerCtxImpl = class ReducerCtx<
225230 if ( dbView !== undefined ) {
226231 me . db = dbView ;
227232 }
233+ if ( asViews !== undefined ) {
234+ me . as = asViews as AliasViews < any > ;
235+ }
228236 }
229237
230238 get databaseIdentity ( ) {
@@ -309,6 +317,7 @@ export const makeHooks = (schema: SchemaInner): ModuleHooks =>
309317class ModuleHooksImpl implements ModuleHooks {
310318 #schema: SchemaInner ;
311319 #dbView_: DbView < any > | undefined ;
320+ #consumerAs_: object | undefined ;
312321 #reducerArgsDeserializers;
313322 #consumerReducerCount: number ;
314323 #flatMounts: FlatMountDispatch [ ] ;
@@ -366,6 +375,13 @@ class ModuleHooksImpl implements ModuleHooks {
366375 ) ) ;
367376 }
368377
378+ get #consumerAs( ) {
379+ return ( this . #consumerAs_ ??= buildAliasCtxMap (
380+ this . #reducerCtx,
381+ this . #schema. mountedDispatchInfos
382+ ) ) ;
383+ }
384+
369385 __describe_module__ ( ) {
370386 const writer = new BinaryWriter ( 128 ) ;
371387 RawModuleDef . serialize (
@@ -397,10 +413,12 @@ class ModuleHooksImpl implements ModuleHooks {
397413
398414 let fn : ( ( ...args : any [ ] ) => any ) | undefined ;
399415 let dbView : DbView < any > ;
416+ let asViews : object ;
400417
401418 if ( reducerId < this . #consumerReducerCount) {
402419 fn = this . #schema. reducers [ reducerId ] ;
403420 dbView = this . #dbView;
421+ asViews = this . #consumerAs;
404422 } else {
405423 let offset = this . #consumerReducerCount;
406424 for ( let i = 0 ; i < this . #flatMounts. length ; i ++ ) {
@@ -415,6 +433,7 @@ class ModuleHooksImpl implements ModuleHooks {
415433 if ( fn === undefined ) {
416434 throw new RangeError ( `unknown reducerId ${ reducerId } ` ) ;
417435 }
436+ asViews = { } ;
418437 }
419438
420439 const ctx = this . #reducerCtx;
@@ -423,7 +442,8 @@ class ModuleHooksImpl implements ModuleHooks {
423442 senderIdentity ,
424443 new Timestamp ( timestamp ) ,
425444 ConnectionId . nullIfZero ( new ConnectionId ( connId ) ) ,
426- dbView !
445+ dbView ! ,
446+ asViews !
427447 ) ;
428448 callUserFunction ( fn , ctx , args ) ;
429449 }
@@ -515,6 +535,36 @@ function buildDbViewForDispatch(dispatch: MountedDispatchInfo): object {
515535 return freeze ( Object . fromEntries ( [ ...tableEntries , ...subNsEntries ] ) ) ;
516536}
517537
538+ function buildAliasCtx (
539+ parent : InstanceType < typeof ReducerCtxImpl > ,
540+ dispatch : MountedDispatchInfo
541+ ) : object {
542+ const nsDb = buildDbViewForDispatch ( dispatch ) ;
543+ const subAs = buildAliasCtxMap ( parent , dispatch . subDispatches ) ;
544+ return {
545+ get sender ( ) { return parent . sender ; } ,
546+ get databaseIdentity ( ) { return parent . databaseIdentity ; } ,
547+ get identity ( ) { return parent . identity ; } ,
548+ get timestamp ( ) { return parent . timestamp ; } ,
549+ get connectionId ( ) { return parent . connectionId ; } ,
550+ get senderAuth ( ) { return parent . senderAuth ; } ,
551+ get random ( ) { return parent . random ; } ,
552+ newUuidV4 ( ) { return parent . newUuidV4 ( ) ; } ,
553+ newUuidV7 ( ) { return parent . newUuidV7 ( ) ; } ,
554+ db : nsDb ,
555+ as : subAs ,
556+ } ;
557+ }
558+
559+ function buildAliasCtxMap (
560+ parent : InstanceType < typeof ReducerCtxImpl > ,
561+ dispatches : MountedDispatchInfo [ ]
562+ ) : object {
563+ return freeze (
564+ Object . fromEntries ( dispatches . map ( d => [ d . namespace , buildAliasCtx ( parent , d ) ] ) )
565+ ) ;
566+ }
567+
518568function makeTableView (
519569 typespace : Typespace ,
520570 table : RawTableDefV10
0 commit comments