diff options
| author | mo khan <mo@mokhan.ca> | 2017-01-12 12:16:29 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2017-01-12 12:16:29 -0700 |
| commit | dad49f47cfef756e7f40c417a607ab4265ddb0d7 (patch) | |
| tree | 5485c6e3735dc7ca64610794cf4a0f793852e2f5 | |
| parent | 240fa550476ebc6bfdd141d77500c200cb237ac2 (diff) | |
use a set and map in the registry.
| -rw-r--r-- | app/infrastructure/registry.js | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/app/infrastructure/registry.js b/app/infrastructure/registry.js index 13b92de..4542796 100644 --- a/app/infrastructure/registry.js +++ b/app/infrastructure/registry.js @@ -13,8 +13,8 @@ export class Resolver { } parseConstructor(func) { - let code = func.toString(); - let regex = /function ([a-zA-Z]*)\((.*)\) *\{/; + const code = func.toString(); + const regex = /function ([a-zA-Z]*)\((.*)\) *\{/; return code.match(regex); } @@ -24,9 +24,9 @@ export class Resolver { } resolveDependenciesUsing(container) { - let ctor = this.parseConstructor(this.factory); - let parameters = ctor.slice(2)[0].split(',').filter(Boolean); - let dependencies = parameters.map((parameter) => { + const ctor = this.parseConstructor(this.factory); + const parameters = ctor.slice(2)[0].split(',').filter(Boolean); + const dependencies = parameters.map((parameter) => { return container.resolve(parameter.trim()); }); return new this.factory(...dependencies); @@ -43,7 +43,7 @@ export class Registration { } asSingleton() { - let originalFactory = this.factory; + const originalFactory = this.factory; let item = null; this.factory = (container) => { if (item == null) { @@ -56,20 +56,20 @@ export class Registration { export default class Registry { constructor() { - this.registrations = {}; + this.registrations = new Map(); } register(key, factory) { - if (this.registrations[key] == undefined) { - this.registrations[key] = []; + if (!this.isRegistered(key)) { + this.registrations.set(key, new Set()); } - let registration = new Registration(factory); - this.registrations[key].push(registration); + const registration = new Registration(factory); + this.registrations.get(key).add(registration); return registration; } isRegistered(key) { - return this.registrations.hasOwnProperty(key); + return this.registrations.has(key); } resolve(key) { @@ -78,7 +78,7 @@ export default class Registry { } try { - let registration = this.registrations[key][0]; + const registration = this._registrationsFor(key)[0]; return registration.create(this); } catch(error) { console.error(`ERROR: Could not resolve "${key}" ${error}`); @@ -87,6 +87,10 @@ export default class Registry { } resolveAll(key) { - return this.registrations[key].map(registration => registration.create(this)); + return this._registrationsFor(key).map(registration => registration.create(this)); + } + + _registrationsFor(key) { + return Array.from(this.registrations.get(key)); } } |
