This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
duun.js
62 lines (49 loc) · 1.43 KB
/
duun.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Duun provides a way to combine multiple dependencies into a single exposed
* object, to ease management of dependencies (and their injection) across
* large applications.
*/
'use strict';
var proxy = require( './core/proxy' );
/**
* Duun constructor
* @arg {String} name
* @constructor
*/
function Duun( name ) {
// guard against new-less invocation
if ( ! ( this instanceof Duun ) ) {
return new Duun( name );
}
// children should inherit everything :P
this.prototype = this;
// save ref to super-object's plugins
var superPlugins = this.plugins || [];
// define special properties
Object.defineProperties( this, {
// permanent name of Duun instance
name: { value: name, enumerable: true },
// array of registered Duun plugins
plugins: { value: [], writable: true }
} );
// proxy a set of functions onto a Duun instance
this.proxy = proxy;
// alias method to proxy
this.register = proxy;
// inherit plugins of super-object, but with fresh instances
superPlugins.forEach( function ( plugin ) {
// create fresh instance and push onto this.plugins
this.proxy( plugin );
}.bind( this ) );
// simplify fn.call() usage (eg. in create() below)
return this;
}
/**
* Duun factory function
* @arg {String} name
* @return {Duun}
*/
Duun.prototype.create = Duun.create = function ( name ) {
return Duun.call( Object.create( this.prototype ), name );
};
module.exports = Duun;