From 3e3a5336df20deb58cb3b453fd1560946ac44e7f Mon Sep 17 00:00:00 2001 From: Kagamihara Nadeshiko Date: Mon, 18 Sep 2023 15:25:05 -0700 Subject: [PATCH] Implemented connectable port --- src/core/port.ts | 16 +++++++++++++++- src/core/reactor.ts | 12 ++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/core/port.ts b/src/core/port.ts index e6206e821..a177fb8b9 100644 --- a/src/core/port.ts +++ b/src/core/port.ts @@ -7,7 +7,8 @@ import type { Absent, MultiReadWrite, ReadWrite, - Variable + Variable, + Read } from "./internal"; import {Trigger, Log} from "./internal"; @@ -59,6 +60,15 @@ export abstract class Port extends Trigger { } } +export class ConnectablePort implements Read { + public get = (): Absent => (undefined); + public getPort = (): IOPort => (this.port); + + constructor ( + public port: IOPort + ) {} +} + /** * Abstract class for a writable port. It is intended as a wrapper for a * regular port. In addition to a get method, it also has a set method and @@ -103,6 +113,10 @@ export abstract class IOPort extends Port { } } + public asConnectable(): ConnectablePort { + return new ConnectablePort(this); + } + /** * Only the holder of the key may obtain a writable port. * @param key diff --git a/src/core/reactor.ts b/src/core/reactor.ts index 9b8b92c11..3ca843dfa 100644 --- a/src/core/reactor.ts +++ b/src/core/reactor.ts @@ -42,7 +42,7 @@ import { Startup, Shutdown, WritableMultiPort, - Dummy + Dummy, ConnectablePort } from "./internal"; import {v4 as uuidv4} from "uuid"; import {Bank} from "./bank"; @@ -474,20 +474,20 @@ export abstract class Reactor extends Component { * @param dst */ - public connect(src: IOPort, dst: IOPort): void; + public connect(src: ConnectablePort, dst: ConnectablePort): void; public connect( src: CallerPort, dst: CalleePort ): void; public connect( ...[src, dst]: - | [IOPort, IOPort] + | [ConnectablePort, ConnectablePort] | [CallerPort, CalleePort] ): void { if (src instanceof CallerPort && dst instanceof CalleePort) { this.reactor._connectCall(src, dst); - } else if (src instanceof IOPort && dst instanceof IOPort) { - this.reactor._connect(src, dst); + } else if (src instanceof ConnectablePort && dst instanceof ConnectablePort) { + this.reactor._connect(src.getPort(), dst.getPort()); } else { throw Error( "Logically unreachable code: src and dst type mismatch, Caller(ee) port cannot be connected to IOPort." @@ -1850,7 +1850,7 @@ interface UtilityFunctions { export interface MutationSandbox extends ReactionSandbox { connect: { - (src: IOPort, dst: IOPort): void; + (src: ConnectablePort, dst: ConnectablePort): void; ( src: CallerPort, dst: CalleePort