Skip to content

Commit

Permalink
Service locator
Browse files Browse the repository at this point in the history
  • Loading branch information
baryshev committed Nov 6, 2015
1 parent 7c2cc90 commit 9484e53
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 93 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.DS_Store
.idea
*.iml
node_modules
2 changes: 2 additions & 0 deletions .npmignore
@@ -1,2 +1,4 @@
*.md
.git*
.gitignore
node_modules
4 changes: 2 additions & 2 deletions README.md
@@ -1,7 +1,7 @@
# About

Application launcher with simple container for shared resources.
Service locator

# Installation

sudo npm install -g hub
npm install hub
86 changes: 0 additions & 86 deletions bin/hub

This file was deleted.

163 changes: 163 additions & 0 deletions lib/hub.js
@@ -0,0 +1,163 @@
(function () {
'use strict';

var async = require('async');
var Promise = require("bluebird");

var Hub = function () {
this.constructors = {};
this.destructors = {};
this.cache = {};
this.waiting = {};

};

Hub.prototype.getService = function (name, forceNew, callback) {
if (!callback) {
var resolve, reject;
var promise = new Promise(function () {
resolve = arguments[0];
reject = arguments[1];
});
}
if (this.cache[name] && !forceNew) {
if (callback) {
callback(null, this.cache[name]);
} else {
resolve(this.cache[name]);
}
} else {
if (!forceNew && typeof this.waiting[name] !== 'undefined') {
this.waiting[name].push(callback || {resolve: resolve, reject: reject});
} else {
if (!forceNew && typeof this.waiting[name] === 'undefined') {
this.waiting[name] = [];
}
var that = this;
if (callback) {
if (!this.constructors[name]) {
callback(new Error('Service "' + name + '" is not registered'));
return;
}
this.constructors[name](this, function (error, service) {
if (!error && service) {
if (!forceNew) {
that.cache[name] = service;
}
}
callback(error, service);
if (!forceNew && typeof that.waiting[name] !== 'undefined') {
while (that.waiting[name].length) {
var waiting = that.waiting[name].pop();
waiting(error, service);
}
delete(that.waiting[name]);
}
});
} else {
if (!this.constructors[name]) {
reject(new Error('Service "' + name + '" is not registered'));
return promise;
}
this.constructors[name](this).then(function (service) {
if (service && !forceNew) {
that.cache[name] = service;
}
resolve(service);
if (!forceNew && typeof that.waiting[name] !== 'undefined') {
while (that.waiting[name].length) {
var waiting = that.waiting[name].pop();
waiting.resolve(service);
}
delete(that.waiting[name]);
}
}).catch(function (error) {
reject(error);
if (!forceNew && typeof that.waiting[name] !== 'undefined') {
while (that.waiting[name].length) {
var waiting = that.waiting[name].pop();
waiting.reject(error);
}
delete(that.waiting[name]);
}
});
}
}
}
if (!callback) {
return promise;
}
};

Hub.prototype.exists = function (name) {
return !!this.cache[name];
};

Hub.prototype.register = function (name, constructor, destructor) {
this.constructors[name] = constructor;
if (destructor) {
this.destructors = destructor;
}
};

Hub.prototype.destroy = function (name, service, callback) {
if (callback) {
if (this.destructors[name]) {
this.destructors[name](this, service, callback);
} else {
callback(new Error('There is no destructor for service "' + name + '"'));
}
} else {
if (this.destructors[name]) {
return this.destructors[name](this, service);
} else {
return new Promise(function (resolve, reject) {
reject(new Error('There is no destructor for service "' + name + '"'));
});
}
}
};

Hub.prototype.get = function (name, forceNew, callback) {
var that = this;
if (Object.prototype.toString.call(name) === '[object Array]') {
if (callback) {
var tasks = [];
for (var i = 0; i < name.length; i++) {
if (Object.prototype.toString.call(name[i]) === '[object Array]') {
tasks.push((function (name, forceNew) {
return function (callback) {
that.getService(name, forceNew, callback);
};
}(name[i][0], name[i][1])));
} else {
tasks.push((function (name, forceNew) {
return function (callback) {
that.getService(name, forceNew, callback);
};
}(name[i], false)));
}
}
async.auto(tasks, callback);
} else {
var promises = [];
for (var i = 0; i < name.length; i++) {
if (Object.prototype.toString.call(name[i]) === '[object Array]') {
promises.push(this.getService(name[i][0], name[i][1]));
} else {
promises.push(this.getService(name[i], false));
}
}
return Promise.all(promises);
}
} else {
if (callback) {
that.getService(name, forceNew, callback);
} else {
return that.getService(name, forceNew);
}
}
};

module.exports = Hub;
}());
10 changes: 5 additions & 5 deletions package.json
@@ -1,16 +1,16 @@
{
"name": "hub",
"description": "Application loader with simple container for shared resources",
"version": "2.0.1",
"description": "Service locator",
"version": "1.0.0",
"author": "Vadim M. Baryshev <vadimbaryshev@gmail.com>",
"dependencies": {
"commander": "0.6.1"
"async": "1.5.0",
"bluebird": "3.0.5"
},
"keywords": ["hub", "container", "resources"],
"keywords": ["hub", "container", "dependency", "service locator"],
"repository": {
"type": "git",
"url": "git://github.com/baryshev/hub.git"
}, "main": "index",
"bin": { "hub": "./bin/hub" },
"engines": { "node": ">= 0.6.0" }
}
2 changes: 2 additions & 0 deletions test/hub.js
@@ -0,0 +1,2 @@
var Hub = require('../lib/hub');
var hub = new Hub();

0 comments on commit 9484e53

Please sign in to comment.