Skip to content

Commit

Permalink
Merge pull request #67 from samchon/features/props
Browse files Browse the repository at this point in the history
Add new generic argument. `Remote`.
  • Loading branch information
samchon authored Apr 28, 2024
2 parents acc78d0 + 51d1671 commit 9f23125
Show file tree
Hide file tree
Showing 36 changed files with 156 additions and 91 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tgrid",
"version": "0.10.3",
"version": "0.11.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"exports": {
Expand Down
8 changes: 6 additions & 2 deletions src/components/Communicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ import { Invoke } from "./Invoke";
* - {@link sendData}
*
* @template Provider Type of features provided for remote system.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export abstract class Communicator<Provider> {
export abstract class Communicator<
Provider extends object | null | undefined,
Remote extends object | null,
> {
/**
* @hidden
*/
Expand Down Expand Up @@ -205,7 +209,7 @@ export abstract class Communicator<Provider> {
* @return A Driver for the RFC.
*/
public getDriver<
Controller extends object,
Controller extends NonNullable<Remote> = NonNullable<Remote>,
UseParametric extends boolean = false,
>(): Driver<Controller, UseParametric> {
return this.driver_ as Driver<Controller, UseParametric>;
Expand Down
4 changes: 3 additions & 1 deletion src/protocols/internal/AcceptorBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import { Communicator } from "../../components/Communicator";
*
* @template Header Type of the header containing initial data.
* @template Provider Type of features provided for the remote system.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export abstract class AcceptorBase<
Header,
Provider extends object | null,
> extends Communicator<Provider | null | undefined> {
Remote extends object | null,
> extends Communicator<Provider | undefined, Remote> {
/**
* @hidden
*/
Expand Down
4 changes: 3 additions & 1 deletion src/protocols/internal/ConnectorBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import { Communicator } from "../../components/Communicator";
*
* @template Header Type of the header containing initial data.
* @template Provider Type of additional features provided for the remote system.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export abstract class ConnectorBase<
Header,
Provider extends object | null,
> extends Communicator<Provider> {
Remote extends object | null,
> extends Communicator<Provider, Remote> {
/**
* @hidden
*/
Expand Down
24 changes: 15 additions & 9 deletions src/protocols/web/WebAcceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ import { IWebCommunicator } from "./internal/IWebCommunicator";
*
* @template Header Type of the header containing initial data.
* @template Provider Type of features provided for the remote system.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export class WebAcceptor<Header, Provider extends object | null>
extends AcceptorBase<Header, Provider>
export class WebAcceptor<
Header,
Provider extends object | null,
Remote extends object | null,
>
extends AcceptorBase<Header, Provider, Remote>
implements IWebCommunicator
{
/**
Expand All @@ -53,22 +58,23 @@ export class WebAcceptor<Header, Provider extends object | null>
/* ----------------------------------------------------------------
CONSTRUCTORS
---------------------------------------------------------------- */
public static upgrade<Header, Provider extends object | null>(
public static upgrade<
Header,
Provider extends object | null,
Remote extends object | null,
>(
request: http.IncomingMessage,
socket: WebSocket,
handler?: (acceptor: WebAcceptor<Header, Provider>) => Promise<any>,
handler?: (acceptor: WebAcceptor<Header, Provider, Remote>) => Promise<any>,
): void {
socket.once("message", async (data: WebSocket.Data) => {
// @todo: custom code is required
if (typeof data !== "string") socket.close();
else
try {
const wrapper: IHeaderWrapper<Header> = JSON.parse(data as string);
const acceptor: WebAcceptor<Header, Provider> = new WebAcceptor(
request,
socket,
wrapper.header,
);
const acceptor: WebAcceptor<Header, Provider, Remote> =
new WebAcceptor(request, socket, wrapper.header);
if (handler !== undefined) await handler(acceptor);
} catch (exp) {
socket.close();
Expand Down
9 changes: 7 additions & 2 deletions src/protocols/web/WebConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ import { WebSocketPolyfill } from "./internal/WebSocketPolyfill";
*
* @template Header Type of the header containing initial data.
* @template Provider Type of features provided for the remote system.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export class WebConnector<Header, Provider extends object | null>
extends ConnectorBase<Header, Provider>
export class WebConnector<
Header,
Provider extends object | null,
Remote extends object | null,
>
extends ConnectorBase<Header, Provider, Remote>
implements IWebCommunicator
{
/**
Expand Down
10 changes: 7 additions & 3 deletions src/protocols/web/WebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ import { WebAcceptor } from "./WebAcceptor";
*
* @template Header Type of header containing initialization data like activation.
* @template Provider Type of features provided for the remote systems.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export class WebServer<Header, Provider extends object | null>
implements IServer<WebServer.State>
export class WebServer<
Header,
Provider extends object | null,
Remote extends object | null,
> implements IServer<WebServer.State>
{
/**
* @hidden
Expand Down Expand Up @@ -109,7 +113,7 @@ export class WebServer<Header, Provider extends object | null>
*/
public async open(
port: number,
handler: (acceptor: WebAcceptor<Header, Provider>) => Promise<void>,
handler: (acceptor: WebAcceptor<Header, Provider, Remote>) => Promise<void>,
): Promise<void> {
//----
// PRELIMINARIES
Expand Down
19 changes: 14 additions & 5 deletions src/protocols/workers/SharedWorkerAcceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ import { IWorkerSystem } from "./internal/IWorkerSystem";
*
* @template Header Type of the header containing initial data.
* @template Provider Type of features provided for the remote system.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export class SharedWorkerAcceptor<Header, Provider extends object | null>
extends AcceptorBase<Header, Provider>
export class SharedWorkerAcceptor<
Header,
Provider extends object | null,
Remote extends object | null,
>
extends AcceptorBase<Header, Provider, Remote>
implements IWorkerSystem
{
/**
Expand All @@ -52,11 +57,15 @@ export class SharedWorkerAcceptor<Header, Provider extends object | null>
/**
* @internal
*/
public static create<Header, Provider extends object | null>(
public static create<
Header,
Provider extends object | null,
Remote extends object | null,
>(
port: MessagePort,
header: Header,
eraser: () => void,
): SharedWorkerAcceptor<Header, Provider> {
): SharedWorkerAcceptor<Header, Provider, Remote> {
return new SharedWorkerAcceptor(port, header, eraser);
}

Expand Down Expand Up @@ -112,7 +121,7 @@ export class SharedWorkerAcceptor<Header, Provider extends object | null>
/**
* @inheritDoc
*/
public async accept(provider: Provider | null = null): Promise<void> {
public async accept(provider: Provider): Promise<void> {
// TEST CONDITION
if (this.state_ !== SharedWorkerAcceptor.State.NONE)
throw new DomainError(
Expand Down
9 changes: 7 additions & 2 deletions src/protocols/workers/SharedWorkerConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ import { WebWorkerCompiler } from "./internal/WebWorkerCompiler";
*
* @template Header Type of the header containing initial data.
* @template Provider Type of features provided for the remote system.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export class SharedWorkerConnector<Header, Provider extends object | null>
extends ConnectorBase<Header, Provider>
export class SharedWorkerConnector<
Header,
Provider extends object | null,
Remote extends object | null,
>
extends ConnectorBase<Header, Provider, Remote>
implements IWorkerSystem
{
/**
Expand Down
16 changes: 10 additions & 6 deletions src/protocols/workers/SharedWorkerServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ import { SharedWorkerAcceptor } from "./SharedWorkerAcceptor";
*
* @template Header Type of the header containing initial data.
* @template Provider Type of features provided for the remote system.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export class SharedWorkerServer<Header, Provider extends object | null>
implements IServer<SharedWorkerServer.State>
export class SharedWorkerServer<
Header,
Provider extends object | null,
Remote extends object | null,
> implements IServer<SharedWorkerServer.State>
{
/**
* @hidden
Expand All @@ -46,7 +50,7 @@ export class SharedWorkerServer<Header, Provider extends object | null>
/**
* @hidden
*/
private acceptors_: HashSet<SharedWorkerAcceptor<Header, Provider>>;
private acceptors_: HashSet<SharedWorkerAcceptor<Header, Provider, Remote>>;

/* ----------------------------------------------------------------
CONSTRUCTOR
Expand Down Expand Up @@ -75,7 +79,7 @@ export class SharedWorkerServer<Header, Provider extends object | null>
*/
public async open(
handler: (
acceptor: SharedWorkerAcceptor<Header, Provider>,
acceptor: SharedWorkerAcceptor<Header, Provider, Remote>,
) => Promise<void>,
): Promise<void> {
// TEST CONDITION
Expand Down Expand Up @@ -130,15 +134,15 @@ export class SharedWorkerServer<Header, Provider extends object | null>
*/
private _Handle_connect(
port: MessagePort,
handler: (acceptor: SharedWorkerAcceptor<Header, Provider>) => any,
handler: (acceptor: SharedWorkerAcceptor<Header, Provider, Remote>) => any,
): void {
port.onmessage = once((evt) => {
// ARGUMENTS
const wrapper: IHeaderWrapper<Header> = JSON.parse(evt.data);

// CREATE ACCEPTOR
/* eslint-disable */
let acceptor: SharedWorkerAcceptor<Header, Provider>;
let acceptor: SharedWorkerAcceptor<Header, Provider, Remote>;
acceptor = SharedWorkerAcceptor.create(port, wrapper.header, () => {
this.acceptors_.erase(acceptor);
});
Expand Down
9 changes: 7 additions & 2 deletions src/protocols/workers/WorkerConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ import { WebWorkerCompiler } from "./internal/WebWorkerCompiler";
*
* @template Header Type of header containing initialization data like activation.
* @template Provider Type of features provided for remote system.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export class WorkerConnector<Header, Provider extends object | null>
extends ConnectorBase<Header, Provider>
export class WorkerConnector<
Header,
Provider extends object | null,
Remote extends object | null,
>
extends ConnectorBase<Header, Provider, Remote>
implements IWorkerSystem
{
/**
Expand Down
9 changes: 7 additions & 2 deletions src/protocols/workers/WorkerServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ import { ThreadPort } from "./internal/threads/ThreadPort";
*
* @template Header Type of header containing initialization data like activation.
* @template Provider Type of features provided for remote system.
* @template Remote Type of features supported by remote system, used for {@link getDriver} function.
* @author Jeongho Nam - https://github.com/samchon
*/
export class WorkerServer<Header, Provider extends object | null>
extends Communicator<Provider | undefined>
export class WorkerServer<
Header,
Provider extends object | null,
Remote extends object | null,
>
extends Communicator<Provider | undefined, Remote>
implements IWorkerSystem, IServer<WorkerServer.State>
{
/**
Expand Down
9 changes: 3 additions & 6 deletions test/browser/shared-worker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import { ICalculator } from "../controllers/ICalculator";
import { complete } from "./internal";

window.onload = async () => {
const worker: SharedWorkerConnector<null, null> = new SharedWorkerConnector(
null,
null,
);
const worker: SharedWorkerConnector<null, null, ICalculator> =
new SharedWorkerConnector(null, null);

// TEST RE-USABILITY
for (let i: number = 0; i < 5; ++i) {
await worker.connect("shared-worker-server.js");

await ICalculator.main(worker.getDriver<ICalculator>());
await ICalculator.main(worker.getDriver());
await worker.close();
}
complete();
Expand Down
2 changes: 1 addition & 1 deletion test/browser/shared-worker-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Calculator } from "../providers/Calculator";

/// chrome://inspect/#workers
async function main(): Promise<void> {
const server: SharedWorkerServer<object, Calculator> =
const server: SharedWorkerServer<object, Calculator, null> =
new SharedWorkerServer();
await server.open(async (acceptor) => {
console.log(acceptor.header);
Expand Down
5 changes: 4 additions & 1 deletion test/browser/web-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { complete } from "./internal";

window.onload = async () => {
for (let i: number = 0; i < 5; ++i) {
const connector: WebConnector<null, null> = new WebConnector(null, null);
const connector: WebConnector<null, null, ICalculator> = new WebConnector(
null,
null,
);
await connector.connect("ws://127.0.0.1:10489");

const driver: Driver<ICalculator> = connector.getDriver();
Expand Down
2 changes: 1 addition & 1 deletion test/browser/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WebServer } from "tgrid";
import { Calculator } from "../providers/Calculator";

async function main(): Promise<void> {
const server: WebServer<object, Calculator> = new WebServer();
const server: WebServer<object, Calculator, null> = new WebServer();
let index: number = 0;

await server.open(10489, async (acceptor) => {
Expand Down
8 changes: 5 additions & 3 deletions test/browser/worker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ async function get_source(): Promise<string> {
}

window.onload = async () => {
const worker: WorkerConnector<null, null> = new WorkerConnector(null, null);
const worker: WorkerConnector<null, null, ICalculator> = new WorkerConnector(
null,
null,
);

for (let i: number = 0; i < 5; ++i) {
await worker.compile(await get_source());

await ICalculator.main(worker.getDriver<ICalculator>());
await ICalculator.main(worker.getDriver());
await worker.close();
}
complete();
Expand Down
2 changes: 1 addition & 1 deletion test/browser/worker-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WorkerServer } from "tgrid";
import { Calculator } from "../providers/Calculator";

async function main(): Promise<void> {
const server: WorkerServer<object, Calculator> = new WorkerServer();
const server: WorkerServer<object, Calculator, null> = new WorkerServer();
await server.open(new Calculator());
}
main().catch((exp) => {
Expand Down
5 changes: 4 additions & 1 deletion test/node/components/test_pseudo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { InvalidArgument } from "tstl";
import { ICalculator } from "../../controllers/ICalculator";
import { Calculator } from "../../providers/Calculator";

class PseudoCommunicator<Provider> extends Communicator<Provider> {
class PseudoCommunicator<Provider extends object | null> extends Communicator<
Provider,
ICalculator
> {
private sender_: (invoke: Invoke) => void;

public constructor(sender: (invoke: Invoke) => void, provider: Provider) {
Expand Down
Loading

0 comments on commit 9f23125

Please sign in to comment.