@knowark/injectarkjs
Advanced tools
Comparing version 0.10.0 to 0.10.1
@@ -7,2 +7,3 @@ // Base Factory Class | ||
this.allowed = [] | ||
this.lazy = [] | ||
} | ||
@@ -9,0 +10,0 @@ |
@@ -8,3 +8,3 @@ export declare class Factory { | ||
extract(method: string): Function | ||
lazy: string[] | ||
} | ||
@@ -11,0 +11,0 @@ |
@@ -11,2 +11,3 @@ import { Factory } from './factory.js' // eslint-disable-line | ||
this.registry = {} | ||
this._load() | ||
} | ||
@@ -62,2 +63,20 @@ | ||
_load () { | ||
if (!this.factory) return | ||
const lazy = this.factory.lazy | ||
if (Object.keys(this.strategy).length) { | ||
Object.keys(this.strategy).forEach( | ||
resource => !lazy.includes(resource) && this.resolve(resource)) | ||
return | ||
} | ||
const dependencies = Object.getOwnPropertyNames( | ||
Object.getPrototypeOf(this.factory)).filter( | ||
method => (!['constructor', 'extract', ...lazy].includes(method) && ( | ||
typeof this.factory[method]) === 'function')) | ||
for (const dependency of dependencies) { | ||
const normalized = dependency[0].toUpperCase() + dependency.slice(1) | ||
this.resolve(normalized) | ||
} | ||
} | ||
/** @param {string} resource */ | ||
@@ -83,3 +102,3 @@ _registryFetch (resource) { | ||
const rule = this.strategy[resource] || { | ||
method: resource.substr(0, 1).toLowerCase() + resource.substr(1) | ||
method: resource[0].toLowerCase() + resource.slice(1) | ||
} | ||
@@ -86,0 +105,0 @@ |
@@ -54,2 +54,4 @@ import { Injectark } from './injectark.js' | ||
class DefaultFactory extends Factory { | ||
lazy = ['dataService'] | ||
constructor (config) { | ||
@@ -147,3 +149,3 @@ super(config) | ||
expect(Object.keys(injector.registry).length).toEqual(2) | ||
expect(Object.keys(injector.registry).length).toEqual(4) | ||
}) | ||
@@ -157,3 +159,3 @@ | ||
expect(Object.keys(injector.registry).length).toEqual(1) | ||
expect(Object.keys(injector.registry).length).toEqual(4) | ||
}) | ||
@@ -167,3 +169,3 @@ | ||
expect(instance.b).toEqual(expect.any(B)) | ||
expect(Object.keys(injector.registry).length).toEqual(3) | ||
expect(Object.keys(injector.registry).length).toEqual(4) | ||
expect(injector.resolve('A')).toBe(instance.a) | ||
@@ -179,3 +181,3 @@ expect(injector.resolve('B')).toBe(instance.b) | ||
expect(instance.b).toEqual(expect.any(B)) | ||
expect(Object.keys(injector.registry).length).toEqual(3) | ||
expect(Object.keys(injector.registry).length).toEqual(4) | ||
expect(injector.resolve('A')).toBe(instance.a) | ||
@@ -186,3 +188,3 @@ expect(injector.resolve('B')).toBe(instance.b) | ||
it("doesn't persist ephemeral dependencies on the registry", () => { | ||
injector.strategy = { | ||
const strategy = { | ||
A: { | ||
@@ -193,2 +195,7 @@ method: 'standardA', | ||
} | ||
injector = new Injectark({ | ||
strategy, | ||
factory, | ||
parent | ||
}) | ||
const instance = injector.resolve('A') | ||
@@ -199,2 +206,6 @@ | ||
}) | ||
it('preemptively instantiates all its dependencies', () => { | ||
expect(Object.keys(injector.registry).length).toEqual(4) | ||
}) | ||
}) | ||
@@ -250,5 +261,2 @@ }) | ||
beforeEach(() => { | ||
factory = new StandardFactory() | ||
strategy = Object.assign({}, standardStrategy) | ||
parent = new Injectark({ | ||
@@ -261,2 +269,4 @@ strategy: coreStrategy, | ||
factory = new StandardFactory() | ||
strategy = Object.assign({}, standardStrategy) | ||
injector = parent.forge({ | ||
@@ -274,3 +284,3 @@ strategy, | ||
it('resolves a resource own by its parent registry', () => { | ||
it('resolves a resource owned by its parent registry', () => { | ||
const instance = injector.resolve('X') | ||
@@ -280,11 +290,11 @@ expect(instance).toEqual(expect.any(X)) | ||
expect(Object.keys(parent.registry).length).toEqual(1) | ||
expect(Object.keys(injector.registry).length).toEqual(0) | ||
expect(Object.keys(parent.registry).length).toEqual(2) | ||
expect(Object.keys(injector.registry).length).toEqual(4) | ||
}) | ||
it('resolves a resource its parent know how to build', () => { | ||
it('resolves a resource its parent knows how to build', () => { | ||
const instance = injector.resolve('Y') | ||
expect(instance).toEqual(injector.parent.registry.Y) | ||
expect(Object.keys(injector.parent.registry).length).toEqual(2) | ||
expect(Object.keys(injector.registry).length).toEqual(0) | ||
expect(Object.keys(injector.registry).length).toEqual(4) | ||
}) | ||
@@ -317,3 +327,3 @@ | ||
describe('Injectark optional strategy', () => { | ||
const parent = new Injectark() | ||
const parent = new Injectark({ factory: new Factory() }) | ||
const config = { key: 'value' } | ||
@@ -362,2 +372,6 @@ const factory = new DefaultFactory(config) | ||
it('returns null on unresolved resources both locally and its parent', () => { | ||
expect(injector.resolve('MISSING')).toBe(undefined) | ||
}) | ||
describe('Injectark default factory resolvers', () => { | ||
@@ -364,0 +378,0 @@ it('resolves a resource by its camelCase name by default', () => { |
{ | ||
"name": "@knowark/injectarkjs", | ||
"version": "0.10.0", | ||
"version": "0.10.1", | ||
"description": "Dependency Injector for Javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20860
584