Flow instance type constructed by flowClass.
Handler input type.
Handler output type.
Flow class whose own prototype defines the method.
Name of a public method that takes Context first.
Optional codecs and RPC settings. Omitted codecs use JSON.
import {
Attribute,
StepList,
declareRPCMethod,
gracefulComplete,
stringCodec,
type Context,
type Flow,
type PersistenceSchema,
type RPCResult,
type Step,
type StepDecision,
} from "@superdurable/dex";
const orderStatus = new Attribute("order-status", stringCodec);
class RecordOrderStep implements Step<string> {
public readonly inputCodec = stringCodec;
public getStepType(): string {
return "RecordOrderStep";
}
public execute(context: Context, status: string): StepDecision {
orderStatus.set(context, status);
return gracefulComplete(status);
}
}
export class OrderFlow implements Flow<string> {
public getFlowType(): string {
return "OrderFlow";
}
public getSteps() {
return StepList.startStep(new RecordOrderStep());
}
public getPersistenceSchema(): PersistenceSchema {
return { attributes: [orderStatus] };
}
public getOrderStatus(context: Context): RPCResult<string> {
return { output: orderStatus.get(context) };
}
public updateOrderStatus(context: Context, status: string): RPCResult<string> {
orderStatus.set(context, status);
return { output: status };
}
}
declareRPCMethod(OrderFlow, "getOrderStatus", { outputCodec: stringCodec });
declareRPCMethod(OrderFlow, "updateOrderStatus", {
inputCodec: stringCodec,
outputCodec: stringCodec,
lockAttributes: [orderStatus.lock()],
});
// Later, on a Client whose Registry contains `orderFlow = new OrderFlow()`:
// const status = await client.invokeRPC(orderFlow.getOrderStatus, "order-42");
Declares a Flow class method as an RPC handler without decorator syntax.
This function is the decorator-free form of rpc. Use it when the runtime that loads the Flow module cannot transform decorators, such as Node.js type stripping with
node --experimental-transform-types. Call it once per RPC method at module top level, right after the class declaration and before constructing the Registry. Pass the class itself and the method name as a string literal. A Registry reads declarations when it is constructed, so a declaration made after a Registry is built has no effect on that Registry.The declaration is stored exactly like
@rpc(options)on the same method. Registry construction then validates it with the same checks and the same errors: duplicate RPC names, locks on unregistered Attributes, and invalid state loads. Client calls such asinvokeRPC(flow.describe, ...)work the same way for both forms.Declare a method through the class that defines it. The declaration belongs to the method function, so a method declared on a base class is an RPC of every subclass that inherits it, as
@rpcon the base method would be. Naming a subclass for a method it only inherits throws, because that would also change the base class and every sibling subclass. To give one subclass its own RPC, override the method in that subclass and declare the override.The handler receives Context and optional input, then returns RPCResult or void. A method with only a Context parameter and a void return is a procedure. Default-parameter methods report
Function.lengthwithout those parameters, so passinputCodecwhen a trailing input uses a default.A method can be declared once. This function throws when the method is already declared by an earlier call or by
@rpc. A Flow may mix both forms on different methods.Declare the handler as a class method. Instance fields, such as arrow functions, are not on the class prototype and cannot be RPC handlers.