Options
All
  • Public
  • Public/Protected
  • All
Menu

The SuperRPC is the central piece. An instance must be created on both sides.

Objects, functions or classes can be registered on the "host" side (see registerHostObject, registerHostClass) and then functions/properties can be called from the "client" side (see getProxyObject, getProxyClass).

The RPC service is symmetric, so depending on the use-case (and the channel), both side can be "host" and "client" at the same time.

The constructor needs a function to generate unique IDs for objects. In order to have no dependencies this needs to be passed in. For convenience the examples use nanoid.

Hierarchy

  • SuperRPC

Index

Constructors

  • new SuperRPC(objectIdGenerator: () => string): SuperRPC
  • Parameters

    • objectIdGenerator: () => string

      A function to generate a unique ID for an object.

      When sending an object to the other side that can not be serialized, we generate an ID and send that instead. The other side creates a proxy object that represents the remote object.

        • (): string
        • Returns string

    Returns SuperRPC

Properties

asyncCallbacks: Map<string, PromiseCallbacks> = ...
callId: number = 0
channel: RPCChannel
currentContext: any

Stores the current "context" object that is passed to the callback of the RPCChannel.receive function.

hostClassRegistry: Map<string, HostRegistryEntry<ClassDescriptor>> = ...
hostFunctionRegistry: Map<string, HostRegistryEntry<FunctionDescriptor<FunctionReturnBehavior>>> = ...
hostObjectRegistry: Map<string, HostRegistryEntry<ObjectDescriptor>> = ...
proxyClassRegistry: Map<string, AnyConstructor> = ...
proxyObjectRegistry: ProxyObjectRegistry = ...
remoteClassDescriptors?: ClassDescriptors
remoteDescriptorsCallbacks?: PromiseCallbacks
remoteFunctionDescriptors?: FunctionDescriptors
remoteObjectDescriptors?: ObjectDescriptors

Methods

  • addMarker(message: RPC_Message): void
  • checkMarker(message: RPC_Message): boolean
  • getOrCreateProxyInstance(objId: string, classId: string, props: any, replyChannel: RPCChannel): any
  • Gets or creates a proxy "class" that will serve multiple purposes.

    • Static functions/properties on the class are proxied the same way as on a regular "host" object
    • If specified the constructor actually constructs an instance of the registered host class on the other side and the returned instance will represent the remote instance, with the specified functions/properties working on its prototype as expected.
    • If an instance of the registered host class is being sent from the other side, an instance of this proxy class will be created and passed on this side.

    Parameters

    • classId: string

    Returns AnyConstructor

  • getProxyFunction(objId: string): any
  • getProxyObject(objId: string): any
  • messageReceived(message: RPC_Message, replyChannel?: RPCChannel, context?: any): void
  • Register a class in the service.

    When an instance of this class is passed to the other side, only the "readonlyProperties" are sent (see ClassDescriptor). Functions and proxied properties are generated there and those call back to the original object.

    Even the constructor can be proxied.

    Note: static functions/properties act as if the class was a normal host object.

    Parameters

    • classId: string

      An ID to identify the class on the client side.

    • classCtor: AnyConstructor

      The class itself (its constructor function)

    • descriptor: ClassDescriptor

      What properties/functions to expose

    Returns void

  • registerHostObject(objId: string, target: object, descriptor: ObjectDescriptor): void
  • Register an object in the service to be called remotely.

    Parameters

    • objId: string

      An ID that the "client" side uses to identify this object.

    • target: object

      The target object

    • descriptor: ObjectDescriptor

      Describes which functions/properties to expose

    Returns void

  • requestRemoteDescriptors(): boolean | Promise<void>
  • Send a request to get the descriptors for the registered host objects from the other side. Uses synchronous communication if possible and returns true/false based on if the descriptors were received. If sync is not available, it uses async messaging and returns a Promise.

    Returns boolean | Promise<void>

  • sendAsync(message: RPC_Message, channel?: RPCChannel): void
  • sendAsyncIfPossible(message: RPC_Message, channel?: RPCChannel): any
  • sendObjectDied(objId: string, replyChannel?: RPCChannel): void
  • sendRemoteDescriptors(replyChannel?: RPCChannel): void
  • Send the descriptors for the registered host objects to the other side. If possible, the message is sent synchronously. This is a "push" style message, for "pull" see requestRemoteDescriptors.

    Parameters

    Returns void

  • sendSync(message: RPC_Message, channel?: RPCChannel): any
  • sendSyncIfPossible(message: RPC_Message, channel?: RPCChannel): any
  • setRemoteDescriptors(response: RPC_DescriptorsResultMessage): boolean
  • Parameters

    • response: RPC_DescriptorsResultMessage

    Returns boolean

Generated using TypeDoc