UNPKG

@k8ts/instruments

Version:

A collection of utilities and core components for k8ts.

112 lines 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseNode = void 0; const doddle_1 = require("doddle"); const immutable_1 = require("immutable"); const reference_1 = require("../reference"); const dependencies_1 = require("./dependencies"); class BaseNode { _entity; _eqProxy = {}; get kind() { return this.key.kind; } get _asNode() { return this; } constructor(_entity) { this._entity = _entity; } get shortFqn() { return `${this.kind.name}/${this.name}`; } get root() { return this.ancestors.at(-1).pull() ?? this; } get name() { return this.key.name; } get isRoot() { return this.parent === null && this._entity.kind.name !== "PodTemplate"; } hashCode() { return (0, immutable_1.hash)(this._eqProxy); } equals(other) { if (reference_1.ForwardRef.is(other)) { return other.equals(this); } return this === other; } ancestors = (0, doddle_1.seq)(() => { const seen = (0, immutable_1.Set)(); const recurse = function* (from) { const parent = from.parent; if (parent && !seen.has(parent)) { seen.add(parent); yield parent; yield* recurse(parent); } }; return (0, doddle_1.seq)(recurse.bind(null, this._asNode)); }).cache(); descendants = (0, doddle_1.seq)(() => { const self = this; let seen = (0, immutable_1.Set)(); const recurse = function* (from) { for (const kid of from.kids) { if (seen.has(kid)) { continue; } seen = seen.add(kid); yield kid; yield* recurse(kid); } }; return (0, doddle_1.seq)(recurse.bind(null, self._asNode)); }).cache(); isParentOf(other) { if (reference_1.ForwardRef.is(other)) { return other.isChildOf(this._asNode); } return (0, immutable_1.Set)(this.descendants).has(other); } isChildOf(other) { if (reference_1.ForwardRef.is(other)) { return other.isParentOf(this._asNode); } return this.equals(other) || (0, immutable_1.Set)(this.ancestors).has(other._asNode); } hasRelationTo(other) { return this.recursiveRelations.some(x => x.needed.equals(other)).pull(); } recursiveRelationsSubtree = (0, doddle_1.seq)(() => { const self = this; return (0, doddle_1.seq)(function* () { for (const child of [self, ...self.descendants]) { yield* child.recursiveRelations; } }); }); recursiveRelations = (0, doddle_1.seq)(() => { let resources = (0, immutable_1.Set)(); const recurseIntoDependency = function* (root) { yield root; if (resources.has(root.needed)) { return; } resources = resources.add(root.needed); const ownDeps = root.needed.relations; for (const needsEdge of ownDeps) { yield* recurseIntoDependency(needsEdge); } }; return (0, doddle_1.seq)(recurseIntoDependency.bind(null, new dependencies_1.Relation("self", this._asNode))) .after(() => { resources = (0, immutable_1.Set)(); }) .cache(); }); } exports.BaseNode = BaseNode; //# sourceMappingURL=base-node.js.map