Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Rechkunov committed Mar 4, 2016
2 parents fd74d48 + 959eb8e commit a702f05
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@

TESTS = test/lib/*
SOURCES = ./lib
TESTS = ./test

all: lint test

lint:
./node_modules/.bin/eslint ./lib
./node_modules/.bin/eslint $(SOURCES) $(TESTS)

lint-fix:
./node_modules/.bin/eslint ./lib --fix
./node_modules/.bin/eslint $(SOURCES) $(TESTS) --fix

test:
ifeq ($(TRAVIS),true)
@echo "Running tests for Travis..."
$(MAKE) travis-cov
else
@echo "Running tests..."
./node_modules/.bin/mocha $(TESTS)
./node_modules/.bin/mocha $(TESTS) --recursive
endif

test-cov:
@echo "Getting coverage report..."
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha $(TESTS)
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- $(TESTS) --recursive

travis-cov:
@echo "Getting coverage for Travis..."
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- $(TESTS) -R spec && ./node_modules/.bin/codecov
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- $(TESTS) --recursive -R spec && ./node_modules/.bin/codecov

clean:
rm -rf coverage
Expand Down
27 changes: 11 additions & 16 deletions lib/ServiceLocator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict';

const util = require('util');

const DEPENDENCY_REGEXP = /^\$\w+/;

/**
* Implements a Service Locator pattern.
*/
Expand All @@ -16,10 +12,10 @@ class ServiceLocator {

/**
* Current type registrations.
* @type {Map}
* @type {Object}
* @private
*/
this._registrations = new Map();
this._registrations = Object.create(null);
}

/**
Expand All @@ -37,7 +33,7 @@ class ServiceLocator {

this._initializeRegistration(type);

this._registrations.get(type).unshift({
this._registrations[type].unshift({
Implementation: implementation,
isSingleton: Boolean(isSingleton),
singleInstance: null
Expand All @@ -53,7 +49,7 @@ class ServiceLocator {
this._throwIfNotString(type);
this._initializeRegistration(type, this);

this._registrations.get(type).unshift({
this._registrations[type].unshift({
Implementation: instance.constructor,
isSingleton: true,
singleInstance: instance
Expand All @@ -68,7 +64,7 @@ class ServiceLocator {
resolve(type) {
this._throwIfNotString(type);
this._throwIfNoType(type);
const firstRegistration = this._registrations.get(type)[0];
const firstRegistration = this._registrations[type][0];
return this._createInstance(firstRegistration);
}

Expand All @@ -80,8 +76,7 @@ class ServiceLocator {
resolveAll(type) {
this._throwIfNotString(type);
this._throwIfNoType(type);
return this._registrations
.get(type)
return this._registrations[type]
.map(registration => this._createInstance(registration));
}

Expand All @@ -91,7 +86,7 @@ class ServiceLocator {
*/
unregister(type) {
this._throwIfNotString(type);
this._registrations.set(type, []);
this._registrations[type] = [];
}

/**
Expand Down Expand Up @@ -121,10 +116,10 @@ class ServiceLocator {
* @private
*/
_initializeRegistration(type) {
if (this._registrations.has(type)) {
if (type in this._registrations) {
return;
}
this._registrations.set(type, []);
this._registrations[type] = [];
}

/**
Expand All @@ -133,8 +128,8 @@ class ServiceLocator {
* @private
*/
_throwIfNoType(type) {
if (this._registrations.has(type) &&
this._registrations.get(type).length > 0) {
if (type in this._registrations &&
this._registrations[type].length > 0) {
return;
}
throw new Error(`Type "${type}" not registered`);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "catberry-locator",
"version": "2.0.0",
"version": "2.1.0",
"author": "Denis Rechkunov <[email protected]>",
"description": "Service Locator for Catberry Framework",
"homepage": "https://github.com/catberry/catberry-locator",
Expand Down

0 comments on commit a702f05

Please sign in to comment.