|
| 1 | +import { AlgebraicType, ProductType } from '../lib/algebraic_type'; |
| 2 | +import BinaryReader from '../lib/binary_reader'; |
| 3 | +import BinaryWriter from '../lib/binary_writer'; |
| 4 | +import type { ConnectionId } from '../lib/connection_id'; |
| 5 | +import type { Identity } from '../lib/identity'; |
| 6 | +import type { Timestamp } from '../lib/timestamp'; |
| 7 | +import type { ParamsObj } from './reducers'; |
| 8 | +import { MODULE_DEF, type UntypedSchemaDef } from './schema'; |
| 9 | +import type { Infer, InferTypeOfRow, TypeBuilder } from './type_builders'; |
| 10 | +import { bsatnBaseSize } from './util'; |
| 11 | + |
| 12 | +export type ProcedureFn< |
| 13 | + S extends UntypedSchemaDef, |
| 14 | + Params extends ParamsObj, |
| 15 | + Ret extends TypeBuilder<any, any>, |
| 16 | +> = (ctx: ProcedureCtx<S>, args: InferTypeOfRow<Params>) => Infer<Ret>; |
| 17 | + |
| 18 | +export type ProcedureCtx<S extends UntypedSchemaDef> = Readonly<{ |
| 19 | + sender: Identity; |
| 20 | + timestamp: Timestamp; |
| 21 | + connectionId: ConnectionId | null; |
| 22 | +}>; |
| 23 | + |
| 24 | +export function procedure< |
| 25 | + S extends UntypedSchemaDef, |
| 26 | + Params extends ParamsObj, |
| 27 | + Ret extends TypeBuilder<any, any>, |
| 28 | +>(name: string, params: Params, ret: Ret, fn: ProcedureFn<S, Params, Ret>) { |
| 29 | + const paramsType: ProductType = { |
| 30 | + elements: Object.entries(params).map(([n, c]) => ({ |
| 31 | + name: n, |
| 32 | + algebraicType: c.algebraicType, |
| 33 | + })), |
| 34 | + }; |
| 35 | + const returnType = ret.algebraicType; |
| 36 | + |
| 37 | + MODULE_DEF.miscExports.push({ |
| 38 | + tag: 'Procedure', |
| 39 | + value: { |
| 40 | + name, |
| 41 | + params: paramsType, |
| 42 | + returnType, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + PROCEDURES.push({ |
| 47 | + fn, |
| 48 | + paramsType, |
| 49 | + returnType, |
| 50 | + returnTypeBaseSize: bsatnBaseSize(MODULE_DEF.typespace, returnType), |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +const PROCEDURES: Array<{ |
| 55 | + fn: ProcedureFn<any, any, any>; |
| 56 | + paramsType: ProductType; |
| 57 | + returnType: AlgebraicType; |
| 58 | + returnTypeBaseSize: number; |
| 59 | +}> = []; |
| 60 | + |
| 61 | +export function callProcedure( |
| 62 | + id: number, |
| 63 | + sender: Identity, |
| 64 | + connectionId: ConnectionId | null, |
| 65 | + timestamp: Timestamp, |
| 66 | + argsBuf: Uint8Array |
| 67 | +): Uint8Array { |
| 68 | + const { fn, paramsType, returnType, returnTypeBaseSize } = PROCEDURES[id]; |
| 69 | + const args = ProductType.deserializeValue( |
| 70 | + new BinaryReader(argsBuf), |
| 71 | + paramsType, |
| 72 | + MODULE_DEF.typespace |
| 73 | + ); |
| 74 | + const ret = fn(Object.freeze({ sender, connectionId, timestamp }), args); |
| 75 | + const retBuf = new BinaryWriter(returnTypeBaseSize); |
| 76 | + AlgebraicType.serializeValue(retBuf, returnType, ret, MODULE_DEF.typespace); |
| 77 | + return retBuf.getBuffer(); |
| 78 | +} |
0 commit comments