import * as JsFunction from "./JsFunction.js"; /** * The type of connection between the server and the client. */ export declare enum ConnectionMode { /** * A message can be sent from one endpoint to another without prior * arrangement. For example, HTTP is a connectionless protocol. */ Connectionless = "Connectionless", /** * A session or connection is established before data can be transmitted. For * example, TCP is a connection-oriented protocol. */ ConnectionOriented = "ConnectionOriented" } /** * How data is distributed to nodes in a network. */ export declare enum OperationMode { /** * A single message is sent to every node in a network. This is a one-to-all * transmission. */ Broadcast = "Broadcast", /** * A single message is sent to a subset of nodes in a network. This is a * one-to-many transmission. */ Multicast = "Multicast", /** * A single message is sent to a single node. This is a one-to-one * transmission. */ Unicast = "Unicast" } /** * How data is transmitted over a network. */ export declare enum TransmissionMode { /** * Either side may transmit data at any time. This is a 2-way communication. */ Duplex = "Duplex", /** * Only one side can transmit data at a time. This is a 2-way communication. */ HalfDuplex = "HalfDuplex", /** * Only the sender can transmit data. This is a 1-way communication. */ Simplex = "Simplex" } export interface DataType { } /** * Constrains the input and output types of a procedure to a specific data type. * A container type is necessary due to the absence of partial type inference in * TypeScript. */ export declare const DataType: () => DataType; interface Subprotocol { connectionMode: ConnectionMode; operationMode: OperationMode; transmissionMode: TransmissionMode; } export type { Subprotocol as t }; /** * The Transporter protocol is type agnostic. In order to provide type-safety a * subprotocol is required. The subprotocol restricts what types may be included * in function IO. For example, if the data type of a subprotocol is JSON then * only JSON data types may be input or output from remote functions. * * In addition, Transporter can perform recursive RPC if certain subprotocol and * network conditions are met. Recursive RPC means functions or proxies may be * included in function IO. This is an interesting concept because it allows * state between processes to be held on the call stack. For example, recursive * RPC allows Observables to be used for pub-sub. * * In order to use recursive RPC your subprotocol must be connection-oriented, * and bidirectional. If those conditions are met then the call signature for * remote functions will allow functions or proxies as input or output. It turns * out that these types of connections are common in the browser, making the * application of Transporter in the browser especially interesting. */ declare const Subprotocol: ({ connectionMode, dataType: _dataType, operationMode, transmissionMode }: { connectionMode: Connection; dataType: DataType; operationMode: Operation; transmissionMode: Transmission; }) => Subprotocol, "yes" extends Bidirectional ? Promise> : void>; /** * Creates a new `Subprotocol`. */ export declare const init: ({ connectionMode, dataType: _dataType, operationMode, transmissionMode }: { connectionMode: Connection; dataType: DataType; operationMode: Operation; transmissionMode: Transmission; }) => Subprotocol, "yes" extends Bidirectional ? Promise> : void>; /** * Returns `true` if the subprotocol is bidirectional. The subprotocol is * considered bidirectional if its operation mode is unicast and its * transmission mode is duplex or half-duplex. */ export declare function isBidirectional(protocol: Subprotocol): boolean; /** * Produces type `"yes"` if the protocol is bidirectional or type `"no"` if it * is not bidirectional. If the connection is not bidirectional then procedures * must return `void`. */ type Bidirectional = Operation extends OperationMode.Broadcast | OperationMode.Multicast ? "no" : Transmission extends TransmissionMode.Simplex ? "no" : "yes"; /** * Defines a procedure who's arguments are both covariant and contravariant. */ type Procedure = JsFunction.Bivariant<(...args: RecursiveIo[]) => Promise>>; interface IoArray extends Array> { } interface IoMapKV extends Map, RecursiveIo> { } interface IoMapK extends Map, T2> { } interface IoMapV extends Map> { } interface IoRecord extends Record> { } interface IoSet extends Set> { } /** * Let `T` be the subprotocol. Recursively maps `T` to allow functions or * proxies as input and output. * * Note that this mapping only works for built in JavaScript types, such as * arrays, records (plain objects), sets, and maps. Therefore, if you have * custom container types, those types will not be mapped over to allow * recursive RPC. For example, if you had a `Tree` type Transporter would not * know how to map over that type. * * Support for custom container types may be possible using a concept similar to * type classes in fp-ts. However, when I attempted this the TypeScript compiler * complained about excessive recursion (though the type checking did worked). */ type RecursiveIo = (T extends Set ? T extends U ? IoSet : Set : T extends Map ? T extends T1 ? T extends T2 ? IoMapKV : IoMapK : T extends T2 ? IoMapV : Map : T extends Array ? T extends U ? IoArray : Array : T extends Record ? T extends U ? IoRecord : Record : T) | Procedure; /** * Uses information about the subprotocol to determine if recursive RPC should * be enabled or not. */ type Io = Connection extends ConnectionMode.ConnectionOriented ? Operation extends OperationMode.Unicast ? Transmission extends TransmissionMode.Duplex | TransmissionMode.HalfDuplex ? RecursiveIo : T : T : T; //# sourceMappingURL=Subprotocol.d.ts.map