Skip to content

Commit

Permalink
refactor: Switch from using objects with function keys to classes tha…
Browse files Browse the repository at this point in the history
…t generate such a structure for TypeScript registry
  • Loading branch information
pojntfx committed Jan 6, 2024
1 parent 93c1d2e commit 75b4329
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 63 deletions.
13 changes: 7 additions & 6 deletions ts/ltsrpc-example-closures-callee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { ILocalContext, Registry } from "./index";
let clients = 0;

const registry = new Registry(
{
Iterate: async (
new (class {
// eslint-disable-next-line class-methods-use-this
async Iterate(
ctx: ILocalContext,
length: number,
onIteration: (i: number, b: string) => Promise<string>
): Promise<number> => {
): Promise<number> {
for (let i = 0; i < length; i++) {
// eslint-disable-next-line no-await-in-loop
const rv = await onIteration(i, "This is from the callee");
Expand All @@ -22,9 +23,9 @@ const registry = new Registry(
}

return length;
},
},
{},
}
})(),
new (class {})(),
{
onClientConnect: () => {
clients++;
Expand Down
13 changes: 8 additions & 5 deletions ts/ltsrpc-example-closures-caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import { IRemoteContext, Registry } from "./index";
let clients = 0;

const registry = new Registry(
{},
{
Iterate: async (
new (class {})(),
new (class {
// eslint-disable-next-line class-methods-use-this
async Iterate(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ctx: IRemoteContext,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
length: number,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onIteration: (i: number, b: string) => Promise<string>
): Promise<number> => 0,
},
): Promise<number> {
return 0;
}
})(),
{
onClientConnect: () => {
clients++;
Expand Down
19 changes: 11 additions & 8 deletions ts/ltsrpc-example-tcp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import { ILocalContext, IRemoteContext, Registry } from "./index";
let clients = 0;

const registry = new Registry(
{
Println: async (ctx: ILocalContext, msg: string) => {
new (class {
// eslint-disable-next-line class-methods-use-this
async Println(ctx: ILocalContext, msg: string) {
console.log("Printing message", msg, "for remote with ID", ctx.remoteID);

console.log(msg);
},
},
{
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Increment: async (ctx: IRemoteContext, delta: number): Promise<number> => 0,
},
}
})(),
new (class {
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
async Increment(ctx: IRemoteContext, delta: number): Promise<number> {
return 0;
}
})(),
{
onClientConnect: () => {
clients++;
Expand Down
23 changes: 12 additions & 11 deletions ts/ltsrpc-example-tcp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ import { Socket, createServer } from "net";
import { ILocalContext, IRemoteContext, Registry } from "./index";

let clients = 0;
let counter = 0;

const registry = new Registry(
{
Increment: async (ctx: ILocalContext, delta: number): Promise<number> => {
new (class {
private counter = 0;

async Increment(ctx: ILocalContext, delta: number): Promise<number> {
console.log(
"Incrementing counter by",
delta,
"for remote with ID",
ctx.remoteID
);

counter += delta;
this.counter += delta;

return counter;
},
},
{
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Println: async (ctx: IRemoteContext, msg: string) => {},
},
return this.counter;
}
})(),
new (class {
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
async Println(ctx: IRemoteContext, msg: string) {}
})(),
{
onClientConnect: () => {
clients++;
Expand Down
19 changes: 11 additions & 8 deletions ts/ltsrpc-example-websocket-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import { ILocalContext, IRemoteContext, Registry } from "./index";
let clients = 0;

const registry = new Registry(
{
Println: async (ctx: ILocalContext, msg: string) => {
new (class {
// eslint-disable-next-line class-methods-use-this
async Println(ctx: ILocalContext, msg: string) {
console.log("Printing message", msg, "for remote with ID", ctx.remoteID);

console.log(msg);
},
},
{
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Increment: async (ctx: IRemoteContext, delta: number): Promise<number> => 0,
},
}
})(),
new (class {
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
async Increment(ctx: IRemoteContext, delta: number): Promise<number> {
return 0;
}
})(),
{
onClientConnect: () => {
clients++;
Expand Down
23 changes: 12 additions & 11 deletions ts/ltsrpc-example-websocket-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ import { WebSocketServer } from "ws";
import { ILocalContext, IRemoteContext, Registry } from "./index";

let clients = 0;
let counter = 0;

const registry = new Registry(
{
Increment: async (ctx: ILocalContext, delta: number): Promise<number> => {
new (class {
private counter = 0;

async Increment(ctx: ILocalContext, delta: number): Promise<number> {
console.log(
"Incrementing counter by",
delta,
"for remote with ID",
ctx.remoteID
);

counter += delta;
this.counter += delta;

return counter;
},
},
{
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Println: async (ctx: IRemoteContext, msg: string) => {},
},
return this.counter;
}
})(),
new (class {
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
async Println(ctx: IRemoteContext, msg: string) {}
})(),
{
onClientConnect: () => {
clients++;
Expand Down
36 changes: 22 additions & 14 deletions ts/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,18 @@ import {

export const ErrorCallCancelled = "call timed out";

const constructorFunctionName = "constructor";

export interface ILocalContext {
remoteID: string;
}

export interface ILocal {
[k: string]: (ctx: ILocalContext, ...rest: any) => Promise<any>;
}

export type IRemoteContext =
| undefined
| {
signal?: AbortSignal;
};

export interface IRemote {
[k: string]: (ctx: IRemoteContext, ...rest: any) => Promise<any>;
}

interface ICallResponse {
value?: any;
err: string;
Expand Down Expand Up @@ -123,7 +117,7 @@ export interface IOptions {
onClientDisconnect?: (remoteID: string) => void;
}

export class Registry<L extends ILocal, R extends IRemote> {
export class Registry<L, R> {
private local: L;

private remotes: {
Expand All @@ -132,8 +126,15 @@ export class Registry<L extends ILocal, R extends IRemote> {

constructor(local: L, private remote: R, private options?: IOptions) {
const l: L = {} as L;
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const functionName in local) {
// eslint-disable-next-line no-restricted-syntax
for (const functionName of Object.getOwnPropertyNames(
Object.getPrototypeOf(local)
)) {
if (functionName === constructorFunctionName) {
// eslint-disable-next-line no-continue
continue;
}

(l as any)[functionName] = (...args: any[]) =>
(local as any)[functionName](...args);
}
Expand Down Expand Up @@ -161,9 +162,16 @@ export class Registry<L extends ILocal, R extends IRemote> {
) => {
const broker = new EventEmitter();

const r = { ...this.remote };
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const functionName in r) {
const r: R = {} as R;
// eslint-disable-next-line no-restricted-syntax
for (const functionName of Object.getOwnPropertyNames(
Object.getPrototypeOf(this.remote)
)) {
if (functionName === constructorFunctionName) {
// eslint-disable-next-line no-continue
continue;
}

(r as any)[functionName] = async (ctx: IRemoteContext, ...rest: any[]) =>
new Promise((res, rej) => {
if (ctx?.signal?.aborted) {
Expand Down

0 comments on commit 75b4329

Please sign in to comment.