-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.js
120 lines (106 loc) · 2.6 KB
/
executor.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
/**
* 用于执行一个模块/类/普通对象方法
* 统一异常管理,转交给errorHandler
* 统一的执行时间记录, 由log模块输出
*
* @author qijun.weiqj
*/
define('Executor', ['jQuery', 'Class', 'Log'],
function($, Class, Log) {
var log = new Log('may:Executor'),
proxy = function() {};
return new Class({
init: function(options) {
options = options || {};
this._error = options.error || may.error;
this._userload = options.load;
},
/**
* execute(function, context, args)
* execute(function, null, args)
* execute(function, args);
*
* execute(context, name, args)
* execute(context, args); // name = 'init'
*
* execute(module, context|name, args)
* -> execute(load(module), context, args);
*/
execute: function(method, context, args) {
// execute(module|function|context, args)
if (args === undefined && $.isArray(context)) {
args = context;
context = null;
}
var type = typeof method;
// execute(function, context, args)
if (type === 'function') {
this._call(context, method, args);
// execute(module, context|name, args)
} else if (type === 'string') {
this._load(method, function(o) {
self.execute(o, context, args);
});
} else if (method) {
// execute(context, name, args);
context = method[typeof context === 'string' ? context : 'init'];
if (typeof context === 'function') {
this._call(method, context, args);
} else {
this._error('parameters error');
}
} else {
this._error('parameters error');
}
},
_call: function(o, method, args) {
var time = null,
guid = this._guid++;
try {
if (log.isEnabled('info')) {
time = (new Date()).getTime();
log.info('[' + guid + '] start');
}
if (o === true) {
proxy.prototype = method.prototype;
o = new proxy();
}
args = args || [];
method.apply(o, args);
if (log.isEnabled('info')) {
time = (new Date()).getTime() - time;
log.info('[' + guid + '] cost ' + time + ' ms');
}
} catch (e) {
this._error(e);
}
},
_load: function(module, success) {
var self = this,
// 首先从以名字空间中取,如a.b.c -> window['a']['b']['c']
o = this._get(module);
if (o) {
success(o);
} else if (this._userload) {
// 如果有load方法,则使用load来载入模块
this._userload(module, success, function() {
self._error('load module [' + module + '] fail');
});
} else {
log.error('can not resolve module [' + module + ']');
}
},
_get: function(module) {
var parts = module.split('.'),
o = window;
for (var i = 0, c = parts.length; i < c; i++) {
o = o[parts[i]];
if (!o) {
return;
}
}
return o;
},
_guid: 1
});
});