This repository has been archived by the owner on May 10, 2020. It is now read-only.
/
index.js
266 lines (235 loc) · 5.79 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
var pzero = require('pzero');
/**
* Logic constructor and getter
*
* @param {String} name
* @param {Object} params
* @param {Object} options
*
* @returns pzero
*/
function logic(name, params, options) {
params = params || {};
options = options || {};
return logic._create(name)._run(params, options);
}
logic.prototype = {
/**
* Dependencies to be loaded,
* before logic can provide it's data
*
* @example
* ['l-1', 'l-2', 'l-3']
* – will load `l-1`, then `l-2`, then `l-3`
* ['l-1', ['l-2', 'l-3'], 'l-4']
* – will load 'l-1', then 'l-2' and 'l-3' at once, then 'l-4'
*
* @type Array
*/
deps: [],
/**
* Callback to be called,
* when all dependencies are loaded
* to finalize data provided by logic.
*
* Should be overwritten.
*
* @param {Array} results
* @param {Object} params
* @param {Object} options
*
* @returns Array
*/
ready: function(results, params, options) {
return results;
},
/**
* Creates logic instance and calls ready, when all deps are resolved
*
* @private
* @param {Object} params
* @param {Object} options
*
* @returns pzero
*/
_run: function(params, options) {
var that = this;
return this._ensure(params, options)
.then(function(results) {
return that.ready(results, params, options);
});
},
/**
* Creates promises with all logics this one dependent on
*
* @private
*
* @param {Object} params
* @param {Object} options
*
* @returns pzero
*/
_ensure: function(params, options) {
var that = this;
var execs, orig;
var promise = pzero().resolve([]);
this.deps.forEach(function(dep) {
// ensure array of names
if (!Array.isArray(dep)) { dep = [dep]; }
// crete executera and add to line
promise = promise.then( that._exec(dep, params, options) );
});
return promise;
},
/**
* Creates event object for logic's event handlers
*
* @param {String} name
* @param {Object} params
* @param {Object} options
*
* @returns Object
*/
_event: function(name, results, params, options) {
return {
name: name,
params: params,
options: options,
results: results,
preventDefault: function() {
this._isPrevented = true;
}
};
},
/**
* Creates executer for each logic
* and cares about event handling before execution
*
* @param {String} name
* @param {Object} params
* @param {Object} options
*
* @returns Function
*/
_exec: function(names, params, options) {
var that = this;
var provs = names.map(function(name) {
return logic._provider(name, params, options);
});
return function logic_exec(results) {
var execs = provs.map(function(prov, i) {
var event, ans, name = names[i];
if (typeof that[name] === 'function') {
event = that._event(name, results, params, options);
reply = that[name](event);
if (event._isPrevented) { return pzero().resolve(reply); }
}
return prov(name, params, options);
});
return pzero.when(execs).then(function(local) {
results.push.apply(results, local);
return results;
});
};
}
};
/**
* List of logic definitions, that can be created
*
* @private
* @type Object
*/
logic._list = {};
/**
* List of logic data providers
* with default one by logic itself
*
* @private
* @type Object
*/
logic._providers = [
function(name) { return logic._list[name] && logic; }
];
/**
* Provider getter
*
* @static
* @private
*
* @param {String} name
* @param {Object} params
* @param {Object} options
*
* @return Function
*/
logic._provider = function(name, params, options) {
var provider;
var providers = logic._providers;
for (var i = 0; i < providers.length; i++ ) {
provider = providers[i](name, params, options);
if (typeof provider === 'function') {
return provider;
}
}
return logic.error('No provider found for ' + name);
};
/**
* Creates logic copy for given name
*
* @static
* @private
*
* @param {String} name
*
* @returns logic
*/
logic._create = function(name) {
if (!logic._list[name]) {
logic.error('Logic ' + name + ' not defined');
} else {
return Object.create(logic._list[name]);
}
};
/**
* Provider setter
* External libs can set their own providers for logic.
* They should pass function, which can ensure,
* that lib can provide informations for given logic name
*
* This function will recive
* - name
* – params
* – options
* to provide information
*
* @static
* @public
*
* @param {Function} checker
*/
logic.provider = function(func) {
logic._providers.push(func);
};
/**
* Defines logic, should provide name and dependencies.
* This logic will be stored and can be created and filled later
* with logic constructor
*
* @static
* @public
*
* @param {String} name
* @param {Object} extend
*/
logic.define = function define(name, extend) {
extend = extend || {};
var properties = {};
Object.getOwnPropertyNames(extend).forEach(function(name) {
properties[name] = Object.getOwnPropertyDescriptor(extend, name);
});
logic._list[name] = Object.create(logic.prototype, properties);
};
logic.error = function(msg) {
throw new Error(msg);
};
module.exports = logic;