@k8ts/instruments
Version:
A collection of utilities and core components for k8ts.
113 lines • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ForwardRef = void 0;
const immutable_1 = require("immutable");
const error_1 = require("../error");
var ForwardRef;
(function (ForwardRef) {
function is(x) {
return "__reference_props__" in x;
}
ForwardRef.is = is;
function make(props) {
const core = new Core(props);
return new Proxy(core, new Handler(core));
}
ForwardRef.make = make;
class Core {
#props;
constructor(props) {
this.#props = props;
}
equals(other) {
const resolved = this.#props.resolver.pull();
if (ForwardRef.is(other)) {
return other.equals(resolved);
}
if ("equals" in resolved) {
return resolved.equals(other);
}
return resolved === other;
}
hashCode() {
return (0, immutable_1.hash)(immutable_1.List.of(this.#props.key, this.#props.origin));
}
get __reference_props__() {
return this.#props;
}
}
ForwardRef.Core = Core;
class Handler {
_core;
get _props() {
return this._core.__reference_props__;
}
constructor(_core) {
this._core = _core;
}
get(target, prop) {
const { _props, _core } = this;
if (Reflect.has(_core, prop)) {
return Reflect.get(_core, prop);
}
const resource = _props.resolver.pull();
const result = Reflect.get(resource, prop);
if (typeof result === "function") {
return result.bind(resource);
}
return result;
}
getPrototypeOf(target) {
const { _props } = this;
return _props.class.prototype;
}
has(target, prop) {
const { _props, _core } = this;
if (Reflect.has(_core, prop)) {
return true;
}
const resource = _props.resolver.pull();
return Reflect.has(resource, prop);
}
getOwnPropertyDescriptor(_, p) {
const { _core, _props } = this;
const desc = Object.getOwnPropertyDescriptor(_core, p);
if (desc) {
desc.configurable = false;
return desc;
}
const resource = _props.resolver.pull();
const resourceDesc = Object.getOwnPropertyDescriptor(resource, p);
return resourceDesc;
}
get referant() {
return this._core.__reference_props__.key.string;
}
#createImmutableError(action) {
return new error_1.ProxyOperationError(`Tried to ${action} a forward reference to ${this.referant}, but it cannot be modified.`);
}
defineProperty(_, property, desc) {
throw this.#createImmutableError(`define property ${String(property)} on`);
}
deleteProperty(_, p) {
throw this.#createImmutableError(`delete property ${String(p)} from`);
}
preventExtensions() {
return true;
}
isExtensible() {
return false;
}
set(_, p) {
throw this.#createImmutableError(`set property ${String(p)} on`);
}
ownKeys() {
const pulled = this._props.resolver.pull();
return [...Reflect.ownKeys(this._core), ...Reflect.ownKeys(pulled)];
}
setPrototypeOf() {
throw this.#createImmutableError(`set the prototype of`);
}
}
})(ForwardRef || (exports.ForwardRef = ForwardRef = {}));
//# sourceMappingURL=forward-ref.js.map