novy-syndicate/src/transport/protocol.ts

43 lines
1.4 KiB
TypeScript
Raw Normal View History

import { Attenuation, toAttenuation } from '../gen/sturdy.js';
import * as IO from '../gen/protocol.js';
import { Assertion, Ref } from '../runtime/actor.js';
2021-03-23 11:18:57 +00:00
import { mapPointers } from '@preserves/core';
import { pointerNotAllowed } from './sturdy.js';
2021-03-02 08:50:23 +00:00
export type WireSymbol = { oid: IO.Oid, ref: Ref, count: number };
2021-03-03 14:45:35 +00:00
export type WireRef =
| { loc: "mine", oid: IO.Oid }
| { loc: "your", oid: IO.Oid, attenuation: Attenuation };
2021-03-03 14:45:35 +00:00
export function myRef(oid: IO.Oid): WireRef & { loc: "mine" } {
2021-03-03 14:45:35 +00:00
return { loc: 'mine', oid };
}
export function yourRef(oid: IO.Oid, attenuation: Attenuation): WireRef & { loc: "your" } {
2021-03-03 14:45:35 +00:00
return { loc: 'your', oid, attenuation };
}
2021-03-12 19:49:18 +00:00
2021-03-23 11:18:57 +00:00
export function toWireRef(v: IO._val): WireRef | undefined {
if (Array.isArray(v) && v.length >= 2) {
switch (v[0]) {
2021-03-12 19:49:18 +00:00
case 0: {
2021-03-23 11:18:57 +00:00
const oid = v[1];
if (typeof oid === 'number') return myRef(oid);
2021-03-12 19:49:18 +00:00
break;
}
case 1: {
2021-03-23 11:18:57 +00:00
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);
2021-03-12 19:49:18 +00:00
}
break;
}
default:
break;
}
}
return void 0;
}