Class WorkerConnector<Header, Provider, Remote>

Worker Connector.

The WorkerConnector is a communicator class, which creates an Worker instance and interacts with it through RPC (Remote Procedure Call). In other words, WorkerConnector considers the Worker instance as a remote server accepting only one client; WorkerServer.

You can create an Worker instance with connect or compile method. The connect method just opens an existing JS (or TS) file, and compile method writes a temporary JS (TS) file, and connects to it. Anyway, the Worker instanced program must open the WorkerServer.

By the way, don't forget closing the worker to clean up the resources. If the closing be performed by WorkerServer, you can wait the worker server closing through the join method.

Also, when declaring this WorkerConnector type, you've to define three generic arguments; Header, Provider and Remote. Those generic arguments must be same with the ones defined in the target WorkerServer class (Provider and Remote must be reversed).

For reference, the first Header type repersents an initial data from the remote client after the connection. I recommend utilize it as an activation tool for security enhancement. The second generic argument Provider represents a provider from client to server, and the other Remote means a provider from the remote server to client.

Jeongho Nam - https://github.com/samchon

Type Parameters

  • Header

    Type of the header containing initial data.

  • Provider extends object | null

    Type of features provided for the remote server.

  • Remote extends object | null

    Type of features supported by remote server.

Hierarchy

Implements

  • IWorkerSystem

Constructors

  • Initializer Constructor.

    For reference, you're planning to run a bundled JavaScript file, and you're using the NodeJS environment, you can't use the "thread" mode. You've to use the "process" mode instead.

    Type Parameters

    • Header
    • Provider extends null | object
    • Remote extends null | object

    Parameters

    • header: Header

      An object containing initialization data like activation.

    • provider: Provider

      An object providing features for remote system.

    • Optionaltype: "process" | "thread"

      You can specify the worker mode when NodeJS. Default is "process".

    Returns WorkerConnector<Header, Provider, Remote>

Accessors

  • get state(): WebSocketConnector.State
  • Get state.

    Get current state of connection state with the worker server.

    List of values are such like below:

    • NONE: This instance is newly created, but did nothing yet.
    • CONNECTING: The connect method is on running.
    • OPEN: The connection is online.
    • CLOSING: The close method is on running.
    • CLOSED: The connection is offline.

    Returns WebSocketConnector.State

Methods

  • Compile server and connect to there.

    The compile method tries compile JS source code, creates Worker instance with that code connects to the Worker. To complete the compilation and connection, the Worker program must open that server using the WorkerServer.open() method.

    Note that, after your business has been completed, you've to close the Worker using close() or WorkerServer.close(). If you don't close that, vulnerable memory usage and communication channel would not be destroyed and it may cause the memory leak.

    Parameters

    Returns Promise<void>

  • Connect to server.

    The connect() method tries to create an Worker instance and connect to the Worker. To complete the connection, the Worker program must open that server using the WorkerServer.open() method.

    Note that, after your business has been completed, you've to close the Worker using close() or WorkerServer.close(). If you don't close that, vulnerable memory usage and communication channel would not be destroyed and it may cause the memory leak.

    Parameters

    Returns Promise<void>

  • Destory the communicator.

    A destory function must be called when the network communication has been closed. It would destroy all function calls in the remote system (by Driver<Controller>), which are not returned yet.

    The error instance would be thrown to those function calls. If the disconnection is abnormal, then write the detailed reason why into the error instance.

    Parameters

    • Optionalerror: Error

      An error instance to be thrown to the unreturned functions.

    Returns Promise<void>

  • Get Driver for RFC (Remote Function Call).

    The Controller is an interface who defines provided functions from the remote system. The Driver is an object who makes to call remote functions, defined in the Controller and provided by Provider in the remote system, possible.

    In other words, calling a functions in the Driver<Controller>, it means to call a matched function in the remote system's Provider object.

    • Controller: Definition only
    • Driver: Remote Function Call

    Type Parameters

    • Controller extends object = NonNullable<Remote>

      An interface for provided features (functions & objects) from the remote system (Provider).

    • UseParametric extends boolean = false

      Whether to convert type of function parameters to be compatible with their pritimive.

    Returns Driver<Controller, UseParametric>

    A Driver for the RFC.

  • Get current Provider.

    Get an object providing features (functions & objects) for remote system. The remote system would call the features (Provider) by using its Driver<Controller>.

    Returns Provider

    Current Provider object

  • Join connection.

    Wait until the connection to be closed.

    Returns Promise<void>

  • Join connection or timeout.

    Wait until the connection to be clsoed until timeout.

    Parameters

    • ms: number

      The maximum milliseconds for joining.

    Returns Promise<boolean>

    Whether awaken by disconnection or timeout.

  • Join connection or time expiration.

    Wait until the connection to be closed until time expiration.

    Parameters

    • at: Date

      The maximum time point to join.

    Returns Promise<boolean>

    Whether awaken by disconnection or time expiration.

  • Data Reply Function.

    A function should be called when data has come from the remote system.

    When you receive a message from the remote system, then parse the message with your special protocol and covert it to be an Invoke object. After the conversion, call this method.

    Parameters

    • invoke: Invoke

      Structured data converted by your special protocol.

    Returns void