UNPKG

5.43 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.BindingToSyntax = void 0;
23var ERROR_MSGS = __importStar(require("../constants/error_msgs"));
24var literal_types_1 = require("../constants/literal_types");
25var binding_in_when_on_syntax_1 = require("./binding_in_when_on_syntax");
26var binding_when_on_syntax_1 = require("./binding_when_on_syntax");
27var BindingToSyntax = (function () {
28 function BindingToSyntax(binding) {
29 this._binding = binding;
30 }
31 BindingToSyntax.prototype.to = function (constructor) {
32 this._binding.type = literal_types_1.BindingTypeEnum.Instance;
33 this._binding.implementationType = constructor;
34 return new binding_in_when_on_syntax_1.BindingInWhenOnSyntax(this._binding);
35 };
36 BindingToSyntax.prototype.toSelf = function () {
37 if (typeof this._binding.serviceIdentifier !== "function") {
38 throw new Error("" + ERROR_MSGS.INVALID_TO_SELF_VALUE);
39 }
40 var self = this._binding.serviceIdentifier;
41 return this.to(self);
42 };
43 BindingToSyntax.prototype.toConstantValue = function (value) {
44 this._binding.type = literal_types_1.BindingTypeEnum.ConstantValue;
45 this._binding.cache = value;
46 this._binding.dynamicValue = null;
47 this._binding.implementationType = null;
48 this._binding.scope = literal_types_1.BindingScopeEnum.Singleton;
49 return new binding_when_on_syntax_1.BindingWhenOnSyntax(this._binding);
50 };
51 BindingToSyntax.prototype.toDynamicValue = function (func) {
52 this._binding.type = literal_types_1.BindingTypeEnum.DynamicValue;
53 this._binding.cache = null;
54 this._binding.dynamicValue = func;
55 this._binding.implementationType = null;
56 return new binding_in_when_on_syntax_1.BindingInWhenOnSyntax(this._binding);
57 };
58 BindingToSyntax.prototype.toConstructor = function (constructor) {
59 this._binding.type = literal_types_1.BindingTypeEnum.Constructor;
60 this._binding.implementationType = constructor;
61 this._binding.scope = literal_types_1.BindingScopeEnum.Singleton;
62 return new binding_when_on_syntax_1.BindingWhenOnSyntax(this._binding);
63 };
64 BindingToSyntax.prototype.toFactory = function (factory) {
65 this._binding.type = literal_types_1.BindingTypeEnum.Factory;
66 this._binding.factory = factory;
67 this._binding.scope = literal_types_1.BindingScopeEnum.Singleton;
68 return new binding_when_on_syntax_1.BindingWhenOnSyntax(this._binding);
69 };
70 BindingToSyntax.prototype.toFunction = function (func) {
71 if (typeof func !== "function") {
72 throw new Error(ERROR_MSGS.INVALID_FUNCTION_BINDING);
73 }
74 var bindingWhenOnSyntax = this.toConstantValue(func);
75 this._binding.type = literal_types_1.BindingTypeEnum.Function;
76 this._binding.scope = literal_types_1.BindingScopeEnum.Singleton;
77 return bindingWhenOnSyntax;
78 };
79 BindingToSyntax.prototype.toAutoFactory = function (serviceIdentifier) {
80 this._binding.type = literal_types_1.BindingTypeEnum.Factory;
81 this._binding.factory = function (context) {
82 var autofactory = function () { return context.container.get(serviceIdentifier); };
83 return autofactory;
84 };
85 this._binding.scope = literal_types_1.BindingScopeEnum.Singleton;
86 return new binding_when_on_syntax_1.BindingWhenOnSyntax(this._binding);
87 };
88 BindingToSyntax.prototype.toAutoNamedFactory = function (serviceIdentifier) {
89 this._binding.type = literal_types_1.BindingTypeEnum.Factory;
90 this._binding.factory = function (context) {
91 return function (named) { return context.container.getNamed(serviceIdentifier, named); };
92 };
93 return new binding_when_on_syntax_1.BindingWhenOnSyntax(this._binding);
94 };
95 BindingToSyntax.prototype.toProvider = function (provider) {
96 this._binding.type = literal_types_1.BindingTypeEnum.Provider;
97 this._binding.provider = provider;
98 this._binding.scope = literal_types_1.BindingScopeEnum.Singleton;
99 return new binding_when_on_syntax_1.BindingWhenOnSyntax(this._binding);
100 };
101 BindingToSyntax.prototype.toService = function (service) {
102 this.toDynamicValue(function (context) { return context.container.get(service); });
103 };
104 return BindingToSyntax;
105}());
106exports.BindingToSyntax = BindingToSyntax;