79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
import * as _ from "@preserves/core";
|
|
import * as _i_Pointer from "../actor";
|
|
|
|
export const $BoxState = Symbol.for("BoxState");
|
|
export const $Observe = Symbol.for("Observe");
|
|
export const $SetBox = Symbol.for("SetBox");
|
|
|
|
export const BoxState = _.Record.makeConstructor<{"_field0": number}, _ptr>()($BoxState, ["_field0"]);
|
|
|
|
export type BoxState = _.Record<(typeof $BoxState), [number], _ptr>;
|
|
|
|
export const SetBox = _.Record.makeConstructor<{"_field0": number}, _ptr>()($SetBox, ["_field0"]);
|
|
|
|
export type SetBox = _.Record<(typeof $SetBox), [number], _ptr>;
|
|
|
|
export type BoxCap = (BoxState | _.Record<(typeof $Observe), [(typeof $SetBox), _ptr], _ptr>);
|
|
|
|
export type ClientCap = (SetBox | _.Record<(typeof $Observe), [(typeof $BoxState), _ptr], _ptr>);
|
|
|
|
export type _ptr = _i_Pointer.Ref;
|
|
|
|
export type _val = _.Value<_ptr>;
|
|
|
|
|
|
export function isBoxState(v: any): v is BoxState {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $BoxState) &&
|
|
((v.length === 1) && typeof v[0] === 'number')
|
|
);
|
|
}
|
|
|
|
export function asBoxState(v: any): BoxState {
|
|
if (!isBoxState(v)) {throw new TypeError(`Invalid BoxState: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isSetBox(v: any): v is SetBox {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $SetBox) &&
|
|
((v.length === 1) && typeof v[0] === 'number')
|
|
);
|
|
}
|
|
|
|
export function asSetBox(v: any): SetBox {
|
|
if (!isSetBox(v)) {throw new TypeError(`Invalid SetBox: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isBoxCap(v: any): v is BoxCap {
|
|
return (
|
|
isBoxState(v) ||
|
|
(
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $Observe) &&
|
|
((v.length === 2) && _.is(v[0], $SetBox) && _.isPointer(v[1]))
|
|
)
|
|
);
|
|
}
|
|
|
|
export function asBoxCap(v: any): BoxCap {
|
|
if (!isBoxCap(v)) {throw new TypeError(`Invalid BoxCap: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isClientCap(v: any): v is ClientCap {
|
|
return (
|
|
isSetBox(v) ||
|
|
(
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $Observe) &&
|
|
((v.length === 2) && _.is(v[0], $BoxState) && _.isPointer(v[1]))
|
|
)
|
|
);
|
|
}
|
|
|
|
export function asClientCap(v: any): ClientCap {
|
|
if (!isClientCap(v)) {throw new TypeError(`Invalid ClientCap: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|