this repo has no description
at main 49 lines 1.6 kB view raw
1"use strict"; 2Object.defineProperty(exports, "__esModule", { value: true }); 3exports.inject = exports.ObjectGraph = void 0; 4const optional_1 = require("../types/optional"); 5/* eslint-disable no-underscore-dangle */ 6class ObjectGraph { 7 constructor(name) { 8 this._members = {}; 9 this.name = name; 10 } 11 adding(type, member) { 12 const clone = this.clone(); 13 clone._members[type.name] = member; 14 return clone; 15 } 16 removing(type) { 17 const clone = this.clone(); 18 // eslint-disable-next-line @typescript-eslint/no-dynamic-delete 19 delete clone._members[type.name]; 20 return clone; 21 } 22 optional(type) { 23 return this._members[type.name]; 24 } 25 required(type) { 26 const member = this._members[type.name]; 27 if ((0, optional_1.isNothing)(member)) { 28 // eslint-disable-next-line @typescript-eslint/require-array-sort-compare 29 const candidates = Object.keys(this._members).sort().join(", "); 30 throw new Error(`No member with type ${type.name} found in ${this.name}. Candidates ${candidates}`); 31 } 32 return member; 33 } 34 clone() { 35 const ctor = this.constructor; 36 // eslint-disable-next-line new-cap 37 const clone = new ctor(this.name); 38 for (const [type, member] of Object.entries(this._members)) { 39 clone._members[type] = member; 40 } 41 return clone; 42 } 43} 44exports.ObjectGraph = ObjectGraph; 45function inject(type, objectGraph) { 46 return objectGraph.required(type); 47} 48exports.inject = inject; 49//# sourceMappingURL=object-graph.js.map