UNPKG

@carlosv2/glue

Version:

Dependency injection library that stays out of the way

38 lines (37 loc) 1.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RunningContext = void 0; const error_1 = require("../error"); class RunningContext { constructor(container, stack = []) { this.container = container; this.stack = stack; } getContainer() { return this.container; } isFrameInStack(type, id) { for (const frame of this.stack) { if (frame.type === type && frame.id === id) { return true; } } return false; } getRepresentation() { return this.stack.map(frame => `${frame.type}(${frame.id})`).join(' -> '); } pushFrame(type, id) { if (this.isFrameInStack(type, id)) { throw new error_1.DiError(`Circular dependency detected: ${this.getRepresentation()} -> ${id}`); } return new RunningContext(this.container, [...this.stack, { type, id }]); } pushParameterFrame(id) { return this.pushFrame('parameter', id); } pushServiceFrame(id) { return this.pushFrame('service', id); } } exports.RunningContext = RunningContext;