286 lines
9.9 KiB
TypeScript
286 lines
9.9 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 decodeAssertion(d: _.TypedDecoder<_ptr>): Assertion | undefined {let result; result = d.next(); return result;}
|
|
|
|
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 decodeHandle(d: _.TypedDecoder<_ptr>): Handle | undefined {let result; result = d.nextSignedInteger(); return result;}
|
|
|
|
export function isEvent(v: any): v is Event {
|
|
return _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && (
|
|
(
|
|
(
|
|
_.is(v.label, $assert) && ((v.length === 2) && isAssertion(v[0]) && isHandle(v[1]))
|
|
) ||
|
|
(_.is(v.label, $retract) && ((v.length === 1) && isHandle(v[0]))) ||
|
|
(_.is(v.label, $message) && ((v.length === 1) && isAssertion(v[0]))) ||
|
|
(_.is(v.label, $sync) && ((v.length === 1) && _.isPointer(v[0])))
|
|
)
|
|
);
|
|
}
|
|
|
|
export function asEvent(v: any): Event {
|
|
if (!isEvent(v)) {throw new TypeError(`Invalid Event: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function decodeEvent(d: _.TypedDecoder<_ptr>): Event | undefined {
|
|
let result;
|
|
if (d.openRecord()) {
|
|
let _tmp0, _tmp1: any;
|
|
_tmp0 = d.next();
|
|
_tmp1 = d.mark();
|
|
if (_.is(_tmp0, $assert)) {
|
|
let _tmp2, _tmp3, _tmp4: any;
|
|
_tmp3 = decodeAssertion(d);
|
|
if (_tmp3 !== void 0) {
|
|
_tmp4 = decodeHandle(d);
|
|
if (_tmp4 !== void 0) {if (d.closeCompound()) _tmp2 = [_tmp3, _tmp4];};
|
|
};
|
|
if (_tmp2 !== void 0) result = _.Record<(typeof $assert), [Assertion, Handle]>(_tmp0 as any, _tmp2 as any);
|
|
};
|
|
if (result === void 0) {
|
|
d.restoreMark(_tmp1);
|
|
if (_.is(_tmp0, $retract)) {
|
|
let _tmp5, _tmp6: any;
|
|
_tmp6 = decodeHandle(d);
|
|
if (_tmp6 !== void 0) {if (d.closeCompound()) _tmp5 = [_tmp6];};
|
|
if (_tmp5 !== void 0) result = _.Record<(typeof $retract), [Handle]>(_tmp0 as any, _tmp5 as any);
|
|
};
|
|
if (result === void 0) {
|
|
d.restoreMark(_tmp1);
|
|
if (_.is(_tmp0, $message)) {
|
|
let _tmp7, _tmp8: any;
|
|
_tmp8 = decodeAssertion(d);
|
|
if (_tmp8 !== void 0) {if (d.closeCompound()) _tmp7 = [_tmp8];};
|
|
if (_tmp7 !== void 0) result = _.Record<(typeof $message), [Assertion]>(_tmp0 as any, _tmp7 as any);
|
|
};
|
|
if (result === void 0) {
|
|
d.restoreMark(_tmp1);
|
|
if (_.is(_tmp0, $sync)) {
|
|
let _tmp9, _tmp10: any;
|
|
_tmp10 = _decodePtr(d);
|
|
if (_tmp10 !== void 0) {if (d.closeCompound()) _tmp9 = [_tmp10];};
|
|
if (_tmp9 !== void 0) result = _.Record<(typeof $sync), [_ptr]>(_tmp0 as any, _tmp9 as any);
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
return result;
|
|
}
|
|
|
|
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 decodeOid(d: _.TypedDecoder<_ptr>): Oid | undefined {let result; result = d.nextSignedInteger(); return result;}
|
|
|
|
export function isTurn(v: any): v is Turn {
|
|
return (
|
|
_.Array.isArray(v) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
(v.length >= 0) &&
|
|
v.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 decodeTurn(d: _.TypedDecoder<_ptr>): Turn | undefined {
|
|
let result;
|
|
if (d.openSequence()) {
|
|
let _tmp11: any;
|
|
{
|
|
let vN: Array<[Oid, Event]> | undefined = [];
|
|
while (!d.closeCompound()) {
|
|
let _tmp12, _tmp13: any;
|
|
_tmp11 = void 0;
|
|
if (d.openSequence()) {
|
|
_tmp12 = decodeOid(d);
|
|
if (_tmp12 !== void 0) {
|
|
_tmp13 = decodeEvent(d);
|
|
if (_tmp13 !== void 0) {if (d.closeCompound()) _tmp11 = [_tmp12, _tmp13];};
|
|
};
|
|
};
|
|
if (_tmp11 === void 0) {vN = void 0; break;};
|
|
vN.push(_tmp11);
|
|
};
|
|
result = vN;
|
|
};
|
|
};
|
|
return result;
|
|
}
|
|
|
|
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 decodeAssert(d: _.TypedDecoder<_ptr>): Assert | undefined {
|
|
let result;
|
|
if (d.openRecord()) {
|
|
let _tmp14: any;
|
|
_tmp14 = _.asLiteral(d.nextSymbol(), $assert);
|
|
if (_tmp14 !== void 0) {
|
|
let _tmp15, _tmp16, _tmp17: any;
|
|
_tmp16 = decodeAssertion(d);
|
|
if (_tmp16 !== void 0) {
|
|
_tmp17 = decodeHandle(d);
|
|
if (_tmp17 !== void 0) {if (d.closeCompound()) _tmp15 = [_tmp16, _tmp17];};
|
|
};
|
|
if (_tmp15 !== void 0) result = _.Record<(typeof $assert), [Assertion, Handle]>(_tmp14 as any, _tmp15 as any);
|
|
};
|
|
};
|
|
return result;
|
|
}
|
|
|
|
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 decodeRetract(d: _.TypedDecoder<_ptr>): Retract | undefined {
|
|
let result;
|
|
if (d.openRecord()) {
|
|
let _tmp18: any;
|
|
_tmp18 = _.asLiteral(d.nextSymbol(), $retract);
|
|
if (_tmp18 !== void 0) {
|
|
let _tmp19, _tmp20: any;
|
|
_tmp20 = decodeHandle(d);
|
|
if (_tmp20 !== void 0) {if (d.closeCompound()) _tmp19 = [_tmp20];};
|
|
if (_tmp19 !== void 0) result = _.Record<(typeof $retract), [Handle]>(_tmp18 as any, _tmp19 as any);
|
|
};
|
|
};
|
|
return result;
|
|
}
|
|
|
|
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 decodeMessage(d: _.TypedDecoder<_ptr>): Message | undefined {
|
|
let result;
|
|
if (d.openRecord()) {
|
|
let _tmp21: any;
|
|
_tmp21 = _.asLiteral(d.nextSymbol(), $message);
|
|
if (_tmp21 !== void 0) {
|
|
let _tmp22, _tmp23: any;
|
|
_tmp23 = decodeAssertion(d);
|
|
if (_tmp23 !== void 0) {if (d.closeCompound()) _tmp22 = [_tmp23];};
|
|
if (_tmp22 !== void 0) result = _.Record<(typeof $message), [Assertion]>(_tmp21 as any, _tmp22 as any);
|
|
};
|
|
};
|
|
return result;
|
|
}
|
|
|
|
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;};
|
|
}
|
|
|
|
export function decodeSync(d: _.TypedDecoder<_ptr>): Sync | undefined {
|
|
let result;
|
|
if (d.openRecord()) {
|
|
let _tmp24: any;
|
|
_tmp24 = _.asLiteral(d.nextSymbol(), $sync);
|
|
if (_tmp24 !== void 0) {
|
|
let _tmp25, _tmp26: any;
|
|
_tmp26 = _decodePtr(d);
|
|
if (_tmp26 !== void 0) {if (d.closeCompound()) _tmp25 = [_tmp26];};
|
|
if (_tmp25 !== void 0) result = _.Record<(typeof $sync), [_ptr]>(_tmp24 as any, _tmp25 as any);
|
|
};
|
|
};
|
|
return result;
|
|
}
|
|
|
|
export const _decodePtr = (d: _.TypedDecoder<_ptr>) => {let result; result = _i_Protocol.decodeWireRef(d); return result;};
|
|
|