133 lines
4.1 KiB
TypeScript
133 lines
4.1 KiB
TypeScript
import * as _ from "@preserves/core";
|
|
import * as _i_Protocol from "../protocol";
|
|
|
|
export const $assert = Symbol.for("assert");
|
|
export const $message = Symbol.for("message");
|
|
export const $retract = Symbol.for("retract");
|
|
export const $sync = Symbol.for("sync");
|
|
|
|
export type Assertion = _val;
|
|
|
|
export type Handle = number;
|
|
|
|
export type Event = (Assert | Retract | Message | Sync);
|
|
|
|
export type Oid = number;
|
|
|
|
export type Turn = Array<[Oid, Event]>;
|
|
|
|
export const Assert = _.Record.makeConstructor<{"assertion": Assertion, "handle": Handle}, _ptr>()($assert, ["assertion","handle"]);
|
|
|
|
export type Assert = _.Record<(typeof $assert), [Assertion, Handle], _ptr>;
|
|
|
|
export const Retract = _.Record.makeConstructor<{"handle": Handle}, _ptr>()($retract, ["handle"]);
|
|
|
|
export type Retract = _.Record<(typeof $retract), [Handle], _ptr>;
|
|
|
|
export const Message = _.Record.makeConstructor<{"body": Assertion}, _ptr>()($message, ["body"]);
|
|
|
|
export type Message = _.Record<(typeof $message), [Assertion], _ptr>;
|
|
|
|
export const Sync = _.Record.makeConstructor<{"peer": _ptr}, _ptr>()($sync, ["peer"]);
|
|
|
|
export type Sync = _.Record<(typeof $sync), [_ptr], _ptr>;
|
|
|
|
export type _ptr = _i_Protocol.WireRef;
|
|
|
|
export type _val = _.Value<_ptr>;
|
|
|
|
|
|
export function isAssertion(v: any): v is Assertion {return true;}
|
|
|
|
export function asAssertion(v: any): Assertion {
|
|
if (!isAssertion(v)) {throw new TypeError(`Invalid Assertion: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isHandle(v: any): v is Handle {return typeof v === 'number';}
|
|
|
|
export function asHandle(v: any): Handle {
|
|
if (!isHandle(v)) {throw new TypeError(`Invalid Handle: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isEvent(v: any): v is Event {return (isAssert(v) || isRetract(v) || isMessage(v) || isSync(v));}
|
|
|
|
export function asEvent(v: any): Event {
|
|
if (!isEvent(v)) {throw new TypeError(`Invalid Event: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isOid(v: any): v is Oid {return typeof v === 'number';}
|
|
|
|
export function asOid(v: any): Oid {
|
|
if (!isOid(v)) {throw new TypeError(`Invalid Oid: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isTurn(v: any): v is Turn {
|
|
return (
|
|
_.Array.isArray(v) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
(v.length >= 0) &&
|
|
v.slice(0).every(v => (
|
|
(
|
|
_.Array.isArray(v) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
(v.length === 2) &&
|
|
isOid(v[0]) &&
|
|
isEvent(v[1])
|
|
)
|
|
))
|
|
);
|
|
}
|
|
|
|
export function asTurn(v: any): Turn {
|
|
if (!isTurn(v)) {throw new TypeError(`Invalid Turn: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isAssert(v: any): v is Assert {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $assert) &&
|
|
((v.length === 2) && isAssertion(v[0]) && isHandle(v[1]))
|
|
);
|
|
}
|
|
|
|
export function asAssert(v: any): Assert {
|
|
if (!isAssert(v)) {throw new TypeError(`Invalid Assert: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isRetract(v: any): v is Retract {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $retract) &&
|
|
((v.length === 1) && isHandle(v[0]))
|
|
);
|
|
}
|
|
|
|
export function asRetract(v: any): Retract {
|
|
if (!isRetract(v)) {throw new TypeError(`Invalid Retract: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isMessage(v: any): v is Message {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $message) &&
|
|
((v.length === 1) && isAssertion(v[0]))
|
|
);
|
|
}
|
|
|
|
export function asMessage(v: any): Message {
|
|
if (!isMessage(v)) {throw new TypeError(`Invalid Message: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isSync(v: any): v is Sync {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $sync) &&
|
|
((v.length === 1) && _.isPointer(v[0]))
|
|
);
|
|
}
|
|
|
|
export function asSync(v: any): Sync {
|
|
if (!isSync(v)) {throw new TypeError(`Invalid Sync: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|