forked from JCloudYu/PumpJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.js
415 lines (330 loc) · 10.9 KB
/
pipe.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**
* Created by JCloudYu on 5/10/16.
*/
(function() {
var
__prevPipe = window.pipe,
__pipeInst = window.pipe = function( dependencies, passive ) {
if ( !Array.isArray( dependencies ) ) return false;
var __chainHead = ___CREATE_PIPE( dependencies, !passive );
__chainHead.pipe = ___CREATE_PIPE_CHAIN( __chainHead );
return __chainHead;
};
// INFO: Extensions
(function(){
__pipeInst.passive = function( dependencies ){
return __pipeInst( dependencies, true );
};
__pipeInst.loadResource = function( resList, immediate ){
return ___RESOURCE_FETCHER( resList, arguments.length > 1 ? !!immediate : true );
};
})();
(function(){
var __compBasePath = './components';
__pipeInst.components = function( components ){
if ( !Array.isArray( components ) ) return false;
var __promiseGenerator, __promise = Promise.resolve(), __promises = [];
components.forEach(function( item ){
var purge = false, async = true, args = [];
if ( typeof item === "string" )
args.push( item );
else
{
async = item.hasOwnProperty('async') ? !!item.async : async;
purge = item.hasOwnProperty('remove-anchor') ? !!item[ 'remove-anchor' ] : purge;
args.push( item.name, item.basePath || __compBasePath, item.anchor );
}
__promiseGenerator = (function( args, anchor, purge ){ return function(){
return ___LOAD_COMPONENT.apply( null, args ).then(function(){
if ( !purge || !anchor || anchor == "" ) return;
$( anchor ).remove();
});
};})( args, item.anchor, purge );
if ( !async )
__promise = __promise.then(__promiseGenerator);
else
__promises.push(__promiseGenerator);
});
return __promise.then(function(){
var promiseQueue = [];
__promises.forEach(function(doPromise){ promiseQueue.push( doPromise() ); });
return Promise.all( promiseQueue );
});
};
__pipeInst.components.base_path = function( path ){
__compBasePath = path || './components';
};
})();
// INFO: Kernel APIs
function ___LOAD_COMPONENT( componentName, basePath, anchor ) {
basePath = basePath || './components';
anchor = anchor || null;
return new Promise(function( fulfill, reject ){
var
modulePath = basePath + '/' + componentName + '/';
$.getJSON( modulePath + 'component.json?' + (((new Date()).getTime() / 1000) | 0), function( descriptor ) {
var trigger, promiseGenerator,
scripts = [], styles = [],
comps = descriptor[ 'components' ] || [],
basePromise = (new Promise(function(fulfill){ trigger = fulfill; })),
earlyPool = [], laterPool = [];
comps.forEach(function( comp ) {
var fPath,
async = (comp.hasOwnProperty('async') ? !!comp.async : true),
caching = comp.hasOwnProperty( 'cache' ) ? !!comp.cache : true,
targetAnchor = !!comp.anchor ? comp.anchor : anchor;
// Load view
comp.view = comp.html || comp.view;
if ( comp.view ) {
promiseGenerator = (function( fPath, anchor, cache ){
return function() {
return new Promise(function(complete, failure) {
var
target = $( anchor || 'body' ),
targetOp = anchor ? target.before : target.prepend;
$.get( fPath + ( cache ? '' : '?' + (((new Date()).getTime() / 1000) | 0) ), function( htmlText ){
$( htmlText ).each(function(idx, tag){ targetOp.call( target, tag ); });
complete();
}, 'text').fail(failure);
});
}
})( modulePath + comp.view, targetAnchor, caching );
if ( !async )
basePromise = basePromise.then(promiseGenerator);
else
earlyPool.push(promiseGenerator);
}
// Load css
comp.style = comp.css || comp.style;
if ( comp.style ) {
fPath = modulePath + comp.style + ( caching ? '' : '?' + (((new Date()).getTime() / 1000) | 0) );
if ( $.inArray( fPath, styles ) < 0 )
{
promiseGenerator = (function( fPath, anchor ){
return function(){ return ___LOAD_RESOURCE( fPath, 'css', anchor ); };
})( fPath, targetAnchor );
styles.push( fPath );
if ( !async )
basePromise = basePromise.then(promiseGenerator);
else
earlyPool.push(promiseGenerator);
}
}
// Load js
comp.script = comp.js || comp.script;
if ( comp.script ) {
if ( !comp.modulize )
{
fPath = modulePath + comp.script + ( caching ? '' : '?' + (((new Date()).getTime() / 1000) | 0) );
if ( $.inArray( fPath, scripts ) < 0 )
{
promiseGenerator = (function( fPath, anchor ){
return function(){ return ___LOAD_RESOURCE( fPath, 'js', anchor, true, !!comp.important ); };
})( fPath, targetAnchor );
scripts.push( fPath );
}
}
else
{
promiseGenerator = (function( fPath, cache ){
return function(){ return ___LOAD_MODULE( fPath + ( cache ? '' : '?' + (((new Date()).getTime() / 1000) | 0) ), null, !!comp.important ); }
})( modulePath + comp.script, caching );
}
if ( !async )
basePromise = basePromise.then(promiseGenerator);
else
laterPool.push(promiseGenerator);
}
});
if ( earlyPool.length == 0 ) earlyPool.push(function(){ return Promise.resolve(); });
if ( laterPool.length == 0 ) laterPool.push(function(){ return Promise.resolve(); });
basePromise.then(function(){
var promises = [];
earlyPool.forEach(function(initiator){ promises.push( initiator() ); });
return Promise.all(promises);
})
.then(function(){
var promises = [];
laterPool.forEach(function(initiator){ promises.push( initiator() ); });
return Promise.all(promises);
}).then( fulfill ).catch( reject );
// Start promise chain
trigger();
}).fail( reject );
});
}
function ___LOAD_RESOURCE( src, type, anchor, late , important) {
var args = Array.prototype.slice.call( arguments );
return new Promise(function( fulfill, reject ) {
var tag, target,
ajax = false,
required = (args.length > 4 ? !!important : true);
switch ( type )
{
case "css":
if ( !!__pipeInst.__CSS_USE_SPECIAL_TREATMENT )
ajax = true;
else
{
tag = document.createElement( 'link' );
tag.rel = "stylesheet";
tag.type = "text/css";
tag.href = src;
}
break;
case "js":
tag = document.createElement( 'script' );
tag.type = "application/javascript";
tag.src = src;
break;
case "html":
ajax = true;
break;
default:
return null;
}
if ( anchor ) anchor = $(anchor);
if ( anchor && anchor.length > 0 )
{
anchor = anchor[0];
target = anchor.parentElement;
}
else
{
anchor = null;
target = $( type == "css" ? 'head' : 'body' )[0];
}
if ( !ajax )
{
tag.onload = fulfill;
tag.onerror = processErr;
if ( late )
setTimeout(function(){ target.insertBefore( tag, anchor ); }, 0);
else
target.insertBefore( tag, anchor );
}
else
{
$.get( src, function( context ) {
if ( type == "css" )
context = "<style>" + context + "</style>";
$( context ).each(function(idx, tag) {
if ( late )
setTimeout(function(){ target.insertBefore( tag, anchor ); }, 0);
else
target.insertBefore( tag, anchor );
});
fulfill();
}, 'text')
.fail(processErr);
}
function processErr(){ (required ? reject : fulfill).call( null, Array.prototype.slice.call( arguments ) ); }
});
}
function ___LOAD_MODULE( src, overwrites, important ) {
var args = Array.prototype.slice.call( arguments );
return new Promise(function( fulfill, reject ) {
var
variables = [],
values = [],
required = (args.length > 2 ? !!important : true);
if ( args.length > 1 && !!overwrites )
{
for( var prop in overwrites )
{
if ( prop !== "module" && overwrites.hasOwnProperty( prop ) )
{
variables.push( prop );
values.push( overwrites[ prop ] );
}
}
}
$.get( src, function( jsContext ){
var moduleCtrl = {};
variables.push( 'module', jsContext );
values.push( moduleCtrl );
(Function.apply( null, variables )).apply( {}, values );
Promise.resolve(moduleCtrl.signal).then(fulfill).catch(reject);
}, 'text')
.fail(function(){
(required ? reject : fulfill).call( null, Array.prototype.slice.call( arguments ) );
});
});
}
function ___RESOURCE_FETCHER( resList, loadImmediately ) {
var
fileList = Array.isArray( resList ) ? resList : [ resList ],
loader = function() {
var __promises = [];
fileList.forEach(function( item ) {
var itemAddr, itemType, promise, important, caching = true, isModulized, moduleOverwite = {};
if ( item === Object(item) )
{
if ( !item.path ) return;
itemAddr = item.path;
itemType = item.type || 'js';
isModulized = !!item.modulize;
moduleOverwite = item.overwrites || {};
caching = item.hasOwnProperty( 'cache' ) ? !!item[ 'cache' ] : true;
important = item.hasOwnProperty( 'important' ) ? !!item[ 'important' ] : true;
}
else
{
itemAddr = item;
itemType = 'js';
isModulized = false;
important = true;
}
itemAddr = itemAddr + ( caching ? '' : '?' + (((new Date()).getTime() / 1000) | 0) );
promise = ( itemType == 'js' && isModulized ) ? ___LOAD_MODULE( itemAddr, moduleOverwite, important ) : ___LOAD_RESOURCE( itemAddr, itemType, null, null, important );
__promises.push( promise );
});
return Promise.all( __promises );
};
return ( !!loadImmediately ) ? loader() : loader;
}
function ___CREATE_PIPE_CHAIN( prevChain ) {
return function( dependencies ) {
if ( !Array.isArray( dependencies ) ) return false;
var newChain = prevChain.then(___CREATE_PIPE( dependencies, false ));
newChain.pipe = ___CREATE_PIPE_CHAIN( newChain );
return newChain;
};
}
function ___CREATE_PIPE( dependencies, immediate ) {
var __trigger,
__resources = [],
__chainHead = new Promise(function( fulfill ){ __trigger = fulfill; });
dependencies.forEach(function( item ) {
if ( typeof item === 'string' ) {
__resources.push( item );
return;
}
// INFO: Pure object
if ( (item === Object(item)) && !Array.isArray(item) && ( typeof item !== 'function' ) && item.path )
{
__resources.push( item );
return;
}
if ( __resources.length > 0 )
{
__chainHead = __chainHead.then(___RESOURCE_FETCHER( __resources ));
__resources = [];
}
if ( typeof item === 'function' ) {
__chainHead = __chainHead.then(item);
}
});
if ( __resources.length > 0 )
__chainHead = __chainHead.then(___RESOURCE_FETCHER(__resources));
if ( !immediate )
{
return function(){
setTimeout(__trigger, 0);
return __chainHead;
};
}
setTimeout(__trigger, 0);
return __chainHead;
}
})();