novy-syndicate/src/protocol.ts

50 lines
1.6 KiB
TypeScript

import { Attenuation, decodeAttenuation } from './gen/sturdy.js';
import * as IO from './gen/protocol.js';
import { decodeRef, Ref } from './actor.js';
import { TypedDecoder } from '@preserves/core';
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 decodeWireRef(d: TypedDecoder<WireRef>): WireRef | undefined {
if (d.openSequence()) {
switch (d.nextSignedInteger()) {
case 0: {
const oid = d.nextSignedInteger();
if (oid !== void 0) {
if (d.closeCompound()) {
return myRef(oid);
}
}
break;
}
case 1: {
const oid = d.nextSignedInteger();
if (oid !== void 0) {
const attenuation = d.withPointerDecoder(decodeRef, d => decodeAttenuation(d));
if (attenuation !== void 0) {
if (d.closeCompound()) {
return yourRef(oid, attenuation);
}
}
}
break;
}
default:
break;
}
}
return void 0;
}