@cashfarm/plow
Version:
Library for validating input data and parameters
57 lines • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const inversify_1 = require("inversify");
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
// Import classes here so they can register themselves
// tslint:disable:no-import-side-effect
const debug = require('debug')('plow:ioc');
const _container = new inversify_1.Container();
//
// Make provide and FluentProvide decorators
//
const realProvide = inversify_binding_decorators_1.makeProvideDecorator(_container);
const _provide = (target) => {
debug('Providing:', typeof target === 'function' ? target.name : target, `(${_container.guid})\n\t\tin`, module.parent.filename);
return realProvide(target);
};
const _fluentProvide = inversify_binding_decorators_1.makeFluentProvideDecorator(_container);
//
// create a unique, global symbol name
// -----------------------------------
const PROVIDE = Symbol.for('cashfarm.plow.provide');
const FLUENT_PROVIDE = Symbol.for('cashfarm.plow.fluentProvide');
const CONTAINER = Symbol.for('cashfarm.plow.container');
// check if the global object has this symbol
// add it if it does not have the symbol, yet
// ------------------------------------------
const globalSymbols = Object.getOwnPropertySymbols(global);
if (!(globalSymbols.indexOf(CONTAINER) > -1)) {
global[PROVIDE] = _provide;
global[FLUENT_PROVIDE] = _fluentProvide;
global[CONTAINER] = _container;
}
// define the singleton API
// ------------------------
const singleton = {};
Object.defineProperty(singleton, 'provide', {
get: () => {
return global[PROVIDE];
}
});
Object.defineProperty(singleton, 'fluentProvide', {
get: () => {
return global[FLUENT_PROVIDE];
}
});
Object.defineProperty(singleton, 'container', {
get: () => {
return global[CONTAINER];
}
});
// ensure the API is never changed
// -------------------------------
Object.freeze(singleton);
// export the singleton API only
// -----------------------------
exports.container = singleton.container, exports.provide = singleton.provide, exports.fluentProvide = singleton.fluentProvide;
//# sourceMappingURL=container.js.map