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