No need to spawn a relay

This commit is contained in:
Tony Garnock-Jones 2021-12-12 23:54:35 +01:00
parent a9a3a8a66d
commit f24dfb53d5
1 changed files with 16 additions and 28 deletions

View File

@ -118,6 +118,9 @@ export interface RelayOptions {
setup(r: Relay): void;
debug?: boolean;
trustPeer?: boolean;
initialOid?: IO.Oid;
initialRef?: Ref;
nextLocalOid?: IO.Oid;
}
export class Relay {
@ -130,6 +133,7 @@ export class Relay {
readonly outboundAssertions = new IdentityMap<Handle, Array<WireSymbol>>();
readonly exported = new Membrane();
readonly imported = new Membrane();
readonly peer: Ref | null;
nextLocalOid: IO.Oid = 0;
pendingTurn: IO.Turn<WireRef> = [];
debug: boolean;
@ -148,6 +152,18 @@ export class Relay {
this.facet.preventInertCheck();
options.setup(this);
if (options.initialRef !== void 0) {
this.rewriteRefOut(options.initialRef, false, []);
}
this.peer = (options.initialOid !== void 0)
? this.rewriteRefIn(WireRef.mine(options.initialOid), [])
: null;
if (options.nextLocalOid !== void 0) {
this.nextLocalOid = (options.nextLocalOid === 0) ? 1 : options.nextLocalOid;
}
}
rewriteOut(assertion: Assertion, transient: boolean): [Value<WireRef>, Array<WireSymbol>]
@ -301,31 +317,3 @@ export class Relay {
}
}
}
export interface RelayActorOptions extends RelayOptions {
initialOid?: IO.Oid;
initialRef?: Ref;
nextLocalOid?: IO.Oid;
}
export function spawnRelay(options: RelayActorOptions & {initialOid: IO.Oid}): Promise<Ref>;
export function spawnRelay(options: Omit<RelayActorOptions, 'initialOid'>): Promise<null>;
export function spawnRelay(options: RelayActorOptions): Promise<Ref | null>
{
return new Promise(resolve => {
Turn.active.spawn(() => {
const relay = new Relay(options);
if (options.initialRef !== void 0) {
relay.rewriteRefOut(options.initialRef, false, []);
}
if (options.initialOid !== void 0) {
resolve(relay.rewriteRefIn(WireRef.mine(options.initialOid), []));
} else {
resolve(null);
}
if (options.nextLocalOid !== void 0) {
relay.nextLocalOid = (options.nextLocalOid === 0) ? 1 : options.nextLocalOid;
}
});
});
}