samchon / tgrid

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

[RFC] Returned object type should be primitive #21

Closed samchon closed 5 years ago

samchon commented 5 years ago

TGrid has adopted JSON structure on network communication.

Therefore, when calling a remote function returning object type, the returned object always be primitive. Prototype would be JS primitive Object and all methods would be removed. I think such characteristic should be reflected to the Driver<Controller> through the meta programming.

Although a Controller has defined its functions to return non-primitive object, Driver should convert the returned object type to be primitive. As you can see from the below example code, functions defined in the Feature class return object type; Report and Member.

However, when wrap the Feature class into the Driver type, those functions are all converted to return the primitive object. The interface IReport returned by Driver<Feature> does not have any method. Remote functions returning Member type are converted to return IMember type, which is derived from the Member.toJSON() method.

/* ----------------------------------------------------------------
    ORIGINAL TYPES
---------------------------------------------------------------- */
class Feature
{
    public getReport(): Report;
    public getMembers(): Member[];

    public insertMember(id: number, name: string): Member;
    public findMember(key: number | string): Member | null;
}

class Report implements IReport
{
    public code: string;
    public title: string;
    public content: string;

    public archive(): void;
    public publish(): void;
}

class Member implements IJsonable<IMember>
{
    private id_: number;
    private name_: string;

    public login(password: string): void;
    public update(name: string): void;
    public toJSON(): IMember
}

/* ----------------------------------------------------------------
    PRIMITIVE TYPES
---------------------------------------------------------------- */
type Driver<Feature> = 
{
    getReport(): Promise<IReport>;
    getMembers(): Promise<IMember[]>;

    insertMember(id: number, name: string): Promise<IMember>;
    findMember(key: number | string): Promise<IMember | null>;
}

interface IReport
{
    code: string;
    title: string;
    content: string;
}

interface IMember
{
    id: number;
    name: string;
}
samchon commented 5 years ago

Converting functions to return primitive objects, through Driver<Controller>, has implemented. You can enjoy the benefit since the v0.2.1 patch. From now on, the Driver<Feature> type, you can see from the above example code, can be come true.

However, I think that the mission has not been completed yet. To avoid domain error of TypeScript compiler, I'd to implement the primitive converter to adapt detour method allowing vulnerable and duplicated code. In such reason, I've published an issue on the TypeScript repository and this issue would be closed after the issue has been resolved.

samchon commented 5 years ago

TypeScript says it's the regular spec. Therefore, I consider that this issue has been fixed.