Skip to content

Commit

Permalink
Implemented connectable port
Browse files Browse the repository at this point in the history
  • Loading branch information
Kagamihara Nadeshiko committed Sep 18, 2023
1 parent d4fb1b3 commit 3e3a533
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 15 additions & 1 deletion src/core/port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type {
Absent,
MultiReadWrite,
ReadWrite,
Variable
Variable,
Read
} from "./internal";
import {Trigger, Log} from "./internal";

Expand Down Expand Up @@ -59,6 +60,15 @@ export abstract class Port<T> extends Trigger {
}
}

export class ConnectablePort<T> implements Read<T> {
public get = (): Absent => (undefined);
public getPort = (): IOPort<T> => (this.port);

constructor (
public port: IOPort<T>
) {}
}

/**
* 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
Expand Down Expand Up @@ -103,6 +113,10 @@ export abstract class IOPort<T> extends Port<T> {
}
}

public asConnectable(): ConnectablePort<T> {
return new ConnectablePort(this);
}

/**
* Only the holder of the key may obtain a writable port.
* @param key
Expand Down
12 changes: 6 additions & 6 deletions src/core/reactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
Startup,
Shutdown,
WritableMultiPort,
Dummy
Dummy, ConnectablePort
} from "./internal";
import {v4 as uuidv4} from "uuid";
import {Bank} from "./bank";
Expand Down Expand Up @@ -474,20 +474,20 @@ export abstract class Reactor extends Component {
* @param dst
*/

public connect<R, S extends R>(src: IOPort<S>, dst: IOPort<R>): void;
public connect<R, S extends R>(src: ConnectablePort<S>, dst: ConnectablePort<R>): void;
public connect<A extends T, R, T, S extends R>(
src: CallerPort<A, R>,
dst: CalleePort<T, S>
): void;
public connect<A extends T, R, T, S extends R>(
...[src, dst]:
| [IOPort<S>, IOPort<R>]
| [ConnectablePort<S>, ConnectablePort<R>]
| [CallerPort<A, R>, CalleePort<T, S>]
): 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."
Expand Down Expand Up @@ -1850,7 +1850,7 @@ interface UtilityFunctions {

export interface MutationSandbox extends ReactionSandbox {
connect: {
<R, S extends R>(src: IOPort<S>, dst: IOPort<R>): void;
<R, S extends R>(src: ConnectablePort<S>, dst: ConnectablePort<R>): void;
<A extends T, R, T, S extends R>(
src: CallerPort<A, R>,
dst: CalleePort<T, S>
Expand Down

0 comments on commit 3e3a533

Please sign in to comment.