UNPKG

2.68 kBJavaScriptView Raw
1'use strict';
2
3var INHERIT = require('inherit'),
4 ASSERT = require('assert'),
5 LOGGER = require('./logger'),
6
7 registry = {},
8 cache = {};
9
10module.exports = {
11
12 /**
13 * Calls INHERIT for specified parameters set and put the result into cache.
14 *
15 * @param {String} nodeName Name of the node
16 * @param {Object|String} objectOrBaseName Object definition or name of the class to inherit from
17 * @param {Object|Object} objectOrStatic Object with static members definition|object definition
18 * @param {Object} staticObject Object with static members definition
19 * @return {Object} cache
20 * @private
21 */
22 _decl: function(nodeName, objectOrBaseName, objectOrStatic, staticObject) {
23
24 var explicitBase = typeof objectOrBaseName === 'string',
25 baseName = explicitBase? objectOrBaseName : nodeName,
26 staticObj = typeof objectOrBaseName !== 'string'? objectOrStatic: staticObject,
27 obj = explicitBase? objectOrStatic : objectOrBaseName,
28 base = baseName;
29
30 if (typeof baseName === 'string') {
31 base = baseName === nodeName? cache[baseName] : this.getNodeClass(baseName);
32 }
33
34 if (typeof objectOrStatic === 'function') {
35 cache[nodeName] = objectOrStatic;
36 } else {
37 cache[nodeName] = base? INHERIT(base, obj, staticObj) : INHERIT(obj, staticObj);
38 }
39
40 return cache;
41
42 },
43
44 _inheritChain: function(nodeName) {
45
46 var stack = registry[nodeName];
47
48 ASSERT.ok(Array.isArray(stack), 'definition for class ' + nodeName + ' is not found in the registry');
49
50 for(var i = 0; i < stack.length; i++) {
51 this._decl.apply(this, stack[i]);
52 }
53
54 return cache[nodeName];
55
56 },
57
58 /**
59 * Stores specified arguments into registry for further processing with INHERIT.
60 *
61 * @param {String} nodeName Name of the node
62 * @param {Object|String} objectOrBaseName Object definition or name of the class to inherit from
63 * @param {Object|Object} objectOrStatic Object with static members definition|object definition
64 * @param {Object} staticObject Object with static members definition
65 */
66 decl: function(nodeName, objectOrBaseName, objectOrStatic, staticObject) {
67
68 cache = {};
69
70 var stack = registry[nodeName] || [];
71 stack.push(Array.prototype.slice.call(arguments, 0));
72
73 registry[nodeName] = stack;
74
75 },
76
77 getRegistry: function(node) {
78 return node? registry[node] : registry;
79 },
80
81
82 getNodeClass: function(nodeName){
83 return cache[nodeName] || this._inheritChain(nodeName);
84 }
85
86};