UNPKG

1.58 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.Fakes = void 0;
12const IocLookupException_1 = require("../Exceptions/IocLookupException");
13/**
14 * Manages the container fakes
15 */
16class Fakes {
17 constructor(container) {
18 this.container = container;
19 /**
20 * Registered fakes
21 */
22 this.list = new Map();
23 }
24 /**
25 * Register a fake for a given namespace
26 */
27 register(namespace, callback) {
28 this.list.set(namespace, { callback });
29 return this;
30 }
31 /**
32 * Find if namespace has a fake registered
33 */
34 has(namespace) {
35 return this.list.has(namespace);
36 }
37 /**
38 * Clear all fakes
39 */
40 clear() {
41 return this.list.clear();
42 }
43 /**
44 * Delete fake for a given namespace
45 */
46 delete(namespace) {
47 return this.list.delete(namespace);
48 }
49 /**
50 * Resolve the fake for a given namespace. An exception is raised if
51 * not fake is defined
52 */
53 resolve(namespace, originalValue) {
54 const fake = this.list.get(namespace);
55 if (!fake) {
56 throw IocLookupException_1.IocLookupException.missingFake(namespace);
57 }
58 fake.cachedValue = fake.cachedValue ?? fake.callback(this.container, originalValue);
59 return fake.cachedValue;
60 }
61}
62exports.Fakes = Fakes;