novy-syndicate/src/transport/protocol.ts

43 lines
1.4 KiB
TypeScript

import { Attenuation, toAttenuation } from '../gen/sturdy.js';
import * as IO from '../gen/protocol.js';
import { Assertion, Ref } from '../runtime/actor.js';
import { mapPointers } from '@preserves/core';
import { pointerNotAllowed } from './sturdy.js';
export type WireSymbol = { oid: IO.Oid, ref: Ref, count: number };
export type WireRef =
| { loc: "mine", oid: IO.Oid }
| { loc: "your", oid: IO.Oid, attenuation: Attenuation };
export function myRef(oid: IO.Oid): WireRef & { loc: "mine" } {
return { loc: 'mine', oid };
}
export function yourRef(oid: IO.Oid, attenuation: Attenuation): WireRef & { loc: "your" } {
return { loc: 'your', oid, attenuation };
}
export function toWireRef(v: IO._val): WireRef | undefined {
if (Array.isArray(v) && v.length >= 2) {
switch (v[0]) {
case 0: {
const oid = v[1];
if (typeof oid === 'number') return myRef(oid);
break;
}
case 1: {
const oid = v[1];
if (typeof oid === 'number') {
const attenuation = toAttenuation(mapPointers(v.slice(2), pointerNotAllowed) as Array<Assertion>);
if (attenuation !== void 0) return yourRef(oid, attenuation);
}
break;
}
default:
break;
}
}
return void 0;
}