UNPKG

1.56 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/fold
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.Bindings = void 0;
12const IocLookupException_1 = require("../Exceptions/IocLookupException");
13/**
14 * Manages the IoC container bindings
15 */
16class Bindings {
17 constructor(container) {
18 this.container = container;
19 /**
20 * Registered bindings
21 */
22 this.list = new Map();
23 }
24 /**
25 * Find if namespace is a binding
26 */
27 has(namespace) {
28 return this.list.has(namespace);
29 }
30 /**
31 * Define a binding
32 */
33 register(binding, callback, singleton) {
34 this.list.set(binding, { callback, singleton });
35 return this;
36 }
37 /**
38 * Resolve a binding. An exception is raised, if the binding is missing
39 */
40 resolve(binding) {
41 const bindingNode = this.list.get(binding);
42 if (!bindingNode) {
43 throw IocLookupException_1.IocLookupException.lookupFailed(binding);
44 }
45 let resolvedValue;
46 if (bindingNode.singleton) {
47 bindingNode.cachedValue = bindingNode.cachedValue ?? bindingNode.callback(this.container);
48 resolvedValue = bindingNode.cachedValue;
49 }
50 else {
51 resolvedValue = bindingNode.callback(this.container);
52 }
53 return resolvedValue;
54 }
55}
56exports.Bindings = Bindings;