47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import * as _ from "@preserves/core";
|
|
import * as _i_Actor from "../actor";
|
|
|
|
export const $Observe = Symbol.for("Observe");
|
|
|
|
export const Observe = _.Record.makeConstructor<{"label": symbol, "observer": _ptr}, _ptr>()($Observe, ["label","observer"]);
|
|
|
|
export type Observe = _.Record<(typeof $Observe), [symbol, _ptr], _ptr>;
|
|
|
|
export type _ptr = _i_Actor.Ref;
|
|
|
|
export type _val = _.Value<_ptr>;
|
|
|
|
|
|
export function isObserve(v: any): v is Observe {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $Observe) &&
|
|
((v.length === 2) && typeof v[0] === 'symbol' && _.isPointer(v[1]))
|
|
);
|
|
}
|
|
|
|
export function asObserve(v: any): Observe {
|
|
if (!isObserve(v)) {throw new TypeError(`Invalid Observe: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function decodeObserve(d: _.TypedDecoder<_ptr>): Observe | undefined {
|
|
let result;
|
|
if (d.openRecord()) {
|
|
let _tmp0: any;
|
|
_tmp0 = _.asLiteral(d.nextSymbol(), $Observe);
|
|
if (_tmp0 !== void 0) {
|
|
let _tmp1, _tmp2, _tmp3: any;
|
|
_tmp2 = d.nextSymbol();
|
|
if (_tmp2 !== void 0) {
|
|
_tmp3 = _decodePtr(d);
|
|
if (_tmp3 !== void 0) {if (d.closeCompound()) _tmp1 = [_tmp2, _tmp3];};
|
|
};
|
|
if (_tmp1 !== void 0) result = _.Record<(typeof $Observe), [symbol, _ptr]>(_tmp0 as any, _tmp1 as any);
|
|
};
|
|
};
|
|
return result;
|
|
}
|
|
|
|
export const _decodePtr = (d: _.TypedDecoder<_ptr>) => {let result; result = _i_Actor.decodeRef(d); return result;};
|
|
|