Comparing version 0.1.2 to 0.2.0
{ | ||
"name": "defur", | ||
"description": "Defer construction of a service until it is needed.", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/adlawson/defur", | ||
@@ -6,0 +6,0 @@ |
@@ -5,4 +5,5 @@ # Defur # | ||
**Version:** *0.1.2*<br/> | ||
**Master build:** [![Master branch build status][travis-master]][travis] | ||
**Version:** *0.2.0*<br/> | ||
**Master build:** [![Master branch build status][travis-master]][travis]<br/> | ||
**Develop build:** [![Develop branch build status][travis-develop]][travis] | ||
@@ -18,6 +19,15 @@ | ||
var http = require('http'); // Just using HTTP as an example | ||
var services = {}; | ||
var services = { | ||
// Defer construction | ||
defur('server', services, function() { | ||
// Define the service inline... | ||
get server() { | ||
return defur('server', this, function() { | ||
return http.createServer().listen(3000); | ||
}); | ||
} | ||
}; | ||
// ...or define the service later | ||
defur.define('server', this, function() { | ||
return http.createServer().listen(3000); | ||
@@ -59,7 +69,8 @@ }); | ||
<!-- Links --> | ||
[travis]: https://travis-ci.org/adlawson/defur | ||
[travis-master]: https://travis-ci.org/adlawson/defur.png?branch=master | ||
[npm]: https://npmjs.org/package/defur | ||
[nodeico]: https://nodei.co/npm/defur.png | ||
[vagrant]: http://vagrantup.com | ||
[license]: /LICENSE | ||
[travis]: https://travis-ci.org/adlawson/defur | ||
[travis-develop]: https://travis-ci.org/adlawson/defur.png?branch=develop | ||
[travis-master]: https://travis-ci.org/adlawson/defur.png?branch=master | ||
[npm]: https://npmjs.org/package/defur | ||
[nodeico]: https://nodei.co/npm/defur.png | ||
[vagrant]: http://vagrantup.com | ||
[license]: /LICENSE |
@@ -16,22 +16,32 @@ /* | ||
var definitions = {}; | ||
var containers = []; | ||
var containers = []; | ||
var services = {}; | ||
/** | ||
* @param {String} name | ||
* @param {Object} container | ||
* @param {Object} context | ||
* @param {Function} definition | ||
* @return {Mixed} | ||
* @api public | ||
*/ | ||
var defur = function(name, container, definition) { | ||
var services = resolveContainer(container); | ||
var defur = function(name, context, definition) { | ||
var services = resolveContext(context); | ||
if (!services.hasOwnProperty(name)) { | ||
services[name] = definition(); | ||
} | ||
Object.defineProperty(container, name, { | ||
return services[name]; | ||
}; | ||
/** | ||
* @param {String} name | ||
* @param {Object} context | ||
* @param {Function} definition | ||
* @api public | ||
*/ | ||
defur.define = function(name, context, definition) { | ||
Object.defineProperty(context, name, { | ||
enumerable: true, | ||
get: function() { | ||
if (!services.hasOwnProperty(name)) { | ||
services[name] = definition(); | ||
} | ||
return services[name]; | ||
return defur(name, context, definition); | ||
} | ||
@@ -41,11 +51,11 @@ }); | ||
function resolveContainer(container) { | ||
var index = containers.indexOf(container); | ||
function resolveContext(context) { | ||
var index = containers.indexOf(context); | ||
if (-1 === index) { | ||
containers.push(container); | ||
index = containers.indexOf(container); | ||
definitions[index] = {}; | ||
containers.push(context); | ||
index = containers.indexOf(context); | ||
services[index] = {}; | ||
} | ||
return definitions[index]; | ||
return services[index]; | ||
}; | ||
@@ -52,0 +62,0 @@ |
5893
62
74