Skip to content

Commit

Permalink
chore: various internal type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku committed Feb 3, 2024
1 parent d9d95d8 commit b112c61
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions lib/injector.js
Expand Up @@ -12,16 +12,19 @@ import {
* @typedef { import('./index.js').ModuleDeclaration } ModuleDeclaration
* @typedef { import('./index.js').ModuleDefinition } ModuleDefinition
* @typedef { import('./index.js').InjectorContext } InjectorContext
*
* @typedef { import('./index.js').TypedDeclaration<any, any> } TypedDeclaration
*/

/**
* Create a new injector with the given modules.
*
* @param {ModuleDefinition[]} modules
* @param {InjectorContext} [parent]
* @param {InjectorContext} [_parent]
*/
export default function Injector(modules, parent) {
parent = parent || {
export default function Injector(modules, _parent) {

const parent = _parent || /** @type InjectorContext */ ({
get: function(name, strict) {
currentlyResolving.push(name);

Expand All @@ -31,7 +34,7 @@ export default function Injector(modules, parent) {
throw error(`No provider for "${ name }"!`);
}
}
};
});

const currentlyResolving = [];
const providers = this._providers = Object.create(parent._providers || null);
Expand All @@ -54,12 +57,13 @@ export default function Injector(modules, parent) {
* @return {any}
*/
function get(name, strict) {
if (!providers[name] && name.indexOf('.') !== -1) {
if (!providers[name] && name.includes('.')) {

const parts = name.split('.');
let pivot = get(parts.shift());
let pivot = get(/** @type { string } */ (parts.shift()));

while (parts.length) {
pivot = pivot[parts.shift()];
pivot = pivot[/** @type { string } */ (parts.shift())];
}

return pivot;
Expand Down Expand Up @@ -99,6 +103,9 @@ export default function Injector(modules, parent) {
}
}

/**
* @type {string[]}
*/
const inject = fn.$inject || parseAnnotations(fn);
const dependencies = inject.map(dep => {
if (hasOwnProp(locals, dep)) {
Expand All @@ -110,7 +117,7 @@ export default function Injector(modules, parent) {

return {
fn: fn,
dependencies: dependencies
dependencies
};
}

Expand All @@ -130,7 +137,7 @@ export default function Injector(modules, parent) {
} = fnDef(type);

// instantiate var args constructor
const Constructor = Function.prototype.bind.apply(fn, [ null ].concat(dependencies));
const Constructor = Function.prototype.bind.call(fn, null, ...dependencies);

return new Constructor();
}
Expand Down Expand Up @@ -310,13 +317,17 @@ export default function Injector(modules, parent) {
return;
}

if (moduleDefinition[key][2] === 'private') {
providers[key] = moduleDefinition[key];
const typeDeclaration = /** @type { TypedDeclaration } */ (
moduleDefinition[key]
);

if (typeDeclaration[2] === 'private') {
providers[key] = typeDeclaration;
return;
}

const type = moduleDefinition[key][0];
const value = moduleDefinition[key][1];
const type = typeDeclaration[0];
const value = typeDeclaration[1];

providers[key] = [ factoryMap[type], arrayUnwrap(type, value), type ];
});
Expand Down

0 comments on commit b112c61

Please sign in to comment.