Repository URL to install this package:
|
Version:
1.2.13 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getInstance = getInstance;
exports.getScoped = exports.useInstanceOrBuildScoped = useInstanceOrBuildScoped;
exports.default = exports.registryWeakMap = exports.registry = exports.referenceList = void 0;
// import { toComponentName } from 'modules/view-container/deps/toComponentName'
// =====
/**
* @description registry of references to have 1 per container
* POC here before moving to a standardized approach
* for multi-container architecture
*/
const referenceList = new WeakMap();
/**
* @description could use object pooling here too
* @todo curry1
* @todo use ./decorateWithIdentity for INSTANCE scoping + CLASS scoping
*
* @param {Class} Klass class to instantiate
* @param {Class} Target use to set as reference
*
* @return {Klass.constructor | Klass} instantiated class
*/
exports.referenceList = referenceList;
function getInstance(Klass, Target) {
if (referenceList.has(Target) === false) {
const instance = new Klass();
instance.isMultiContainer = true;
referenceList.set(Target, instance);
}
return referenceList.get(Target);
}
/**
* @see getInstance (this is the opposite of that)
*
* !!!!
* @todo USE IDENTIFIER + GETINSTANCE FN TO CROSS-CHECK AND BE ABLE TO KEEP FLAT CONTAINERS
*
* @alias getScoped
* @param {*} scopedInstance
* @param {*} klassInstance
* @param {*} Klass could get Klass from Object.getPrototypeOf(klassInstance)?
* like get pooled?
*
* @return {Object} instanceof Klass
*/
function useInstanceOrBuildScoped(scopedInstance, klassInstance, Klass) {
if (referenceList.has(scopedInstance) === false) {
const instance = new Klass(); // instance.scoped = true
referenceList.set(scopedInstance, instance);
}
return referenceList.get(scopedInstance);
}
const registry = {
get: getInstance,
registry: referenceList
};
exports.registryWeakMap = exports.registry = registry;
var _default = registry;
exports.default = _default;