samchon / tgrid

TypeScript RPC (Remote Procedure Call) for WebSocket and Worker protocols
https://tgrid.com/
MIT License
146 stars 19 forks source link

Basic Component - Driver #4

Closed samchon closed 5 years ago

samchon commented 5 years ago

Outline

A proxy object, being a bridge between Communicator and Controller, making RFC (Remote Function Call) possible.

sequence

The Driver class would be 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 would mean to call a matched function in the remote system's Provider object.

Definition

To make RFC (Remote Function Call) possible, the Driver class must extend the Proxy class implementing Controller functions and delivering the function calls to the Communicator class.

namespace tgrid.components
{
    export class Driver<Controller extends object>
        extends Proxy<Promisify<Controller>>
    {
        public constructor(communicator: CommunicatorBase);
    }

    // Functions must be converted to return Promise<T> value.
    // sum(x: number, y: number): number -->> sum(x: number, y: number): Promise<number>
    type Promisify<Controller extends object>;
}

Also, calling functions in the remote system would be processed not in synchronous level. They would be processed in the asynchronous level. Thus, the Driver class should convert functions in the Controller to be return Promise typed value.

// Can be a "Controller"
interface ICalculator
{
    plus(x: number, y: number): number;
    minus(x: number, y: number): number;
    multiplies(x: number, y: number): number;
    divides(x: number, y: number): number;
}

// Return type T must be converted to the Promise<T>
type Promisify<ICalculator> = 
{
    plus(x: number, y: number): Promise<number>;
    minus(x: number, y: number): Promise<number>;
    multiplies(x: number, y: number): Promise<number>;
    divides(x: number, y: number): Promise<number>;
};
samchon commented 5 years ago

It would better to let the Driver to convert the Controller to be readonly.