-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_.aardio
304 lines (269 loc) · 7.21 KB
/
_.aardio
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
//promise 状态管理
//https://promisesaplus.com/#notes promise 实现标准
class promise{
ctor(executor/*输入构造函数所需要的参数*/){
isFuntion(executor,"构造函数")
this.executor = executor //function(resolve,reject){}
this.state = _pending //pending fulfilled rejected三个状态
this.value = null //返回的成功值
this.reason = null //返回的错误值
this.onFulfilledCallbacks = {} //异步回调then fufilled函数调用堆
this.onRejectedCallbacks = {} //异步回调then rejected函数调用堆
};
clutch = function(fun){
return this.then(null,fun);
};
finally = function(fun){
return this.then(fun,fun)
};
resolve = function(value){
if(this.state == _pending){
logger.trace("change resolve",#this.onFulfilledCallbacks)
this.state =_fulfilled
this.value = value
for(index,fulfilledCallback in this.onFulfilledCallbacks){
fulfilledCallback()
}
}
};
reject = function(reason){
if(this.state ==_pending){
logger.trace("change reject",#this.onRejectedCallbacks)
this.state = _rejected
this.reason = reason
logger.trace("reject",reason)
for(index,rejectCallback in this.onRejectedCallbacks){
rejectCallback()
}
}
};
//递归调用返回的promise
resolvePromise = function(promise2,x,resolve,reject){
//logger.trace("resolvePromise",promise2,x,resolve,reject)
var clled = false //重复call
if(promise2===x and x !=null){
logger.debug("循环引用")
//实际上promise2不能是当前的promise地址,只会是上一个
//return reject("循环引用");
}
//判断x 是一个 promise
if( isPromise(x)){
try{
var then = x.then
function resolve2(y){
if(clled){
logger.trace("resolve2 clled is true");
return;
} ;
//递归调用
return this.resolvePromise(promise2,y,resolve,reject)
}
function reject2(reason){
if(clled){
logger.trace("resolve2 clled is true");
return;
} ;
called = true;
reject(reason)
}
if(type.function == type(then)){
//如果then是函数则调用它
call(then(resolve2,reject2),x)
}else{
if (called) return ;
called = true;
resolve(x);
}
}
catch(e){
if (called) return ;
called = true;
reject(e)
}
}else{
//x 是一个普通值
return resolve(x)
}
};
then = function(onFulfilled,onRejected){
//判断参数
if(onFulfilled != null ){
isFuntion(onFulfilled,"then() @1")
}else{
onFulfilled = lambda(value) value
}
if(onRejected != null ){
isFuntion(onRejected,"then() @2")
}else {
onRejected = lambda(reason) error(reason)
}
logger.trace("promise then",this.state)
//返回一个promise
var promise2
promise2 = ..promise(function(resolve,reject){
logger.trace("return promese",this.state)
function onFulfilledItem(){
try{
var x = onFulfilled(this.value)
this.resolvePromise(promise2, x, resolve, reject);
}catch(e){
reject(e)
}
}
function onRejectedItem(){
try{
var x = onRejected(this.reason)
this.resolvePromise(promise2, x, resolve, reject);
}catch(e){
reject(this.reason)
}
}
//判断当前promise状态
if(this.state == _pending){
..table.push(this.onFulfilledCallbacks,onFulfilledItem)
..table.push(this.onRejectedCallbacks,onRejectedItem )
}
if(this.state == _fulfilled){
onFulfilledItem()
}
if(this.state ==_rejected){
onRejectedItem()
}
})
return promise2;
};
run=function(){
try{
this.executor(this.resolve,this.reject)
}
catch(e){
this.reject(e)
}
}
this.run();
//定义类型为 promise
@{
_type="promise"
}
}
namespace promise
_pending = "pending"
_fulfilled = "fulfilled"
_rejected = "rejected"
logger=null
try{
import log4j;
logger = log4j.getLogger('promise') //logger('promise')
logger.defaultLevel="OFF"
}catch(e){
logger={
@{
_get = lambda() function(){}
}
}
}
//静态方法
function resolve(arg){
if(isPromise(arg)){
return arg
}else{
return ..promise(function(resolve,reject){resolve(arg)})
}
}
function reject(arg){
if(isPromise(arg)){
return arg;
}else{
return ..promise(function(resolve,reject){reject(arg)});
}
}
function all(arrsyPromise){
if(type.table != type(arrsyPromise)) error("参数必须为数组",2)
if(#arrsyPromise == 0 ) return ..promise.resolve(arrsyPromise);
return ..promise(function(resolve,reject){
var result = {}
function onFulfilled(value){
..table.push(result,value)
if(#result == #arrsyPromise){
resolve(result)
}
}
function onRejected(reason){
reject(reason)
}
for(k,v in arrsyPromise){
..promise.resolve(v).then(onFulfilled,onRejected)
}
});
}
function any(){
if(type.table != type(arrsyPromise)) error("参数必须为数组",2)
if(#arrsyPromise == 0 ) return ..promise.resolve(arrsyPromise);
return ..promise(function(resolve,reject){
function onFulfilled(value){
resolve(result)
}
function onRejected(reason){
reject(reason)
}
for(k,v in arrayPromise){
..promise.resolve(v).then(onFulfilled,onRejected)
}
});
}
function allSettled(arrayPromise){
return ..promise(function(resolve,reject){
var result = {}
function onFulfilled(value){
table.push(result,{
status = _fulfilled;
value= value;
})
if(#result == #arrayPromise){
resolve(result)
}
}
function onRejected(reason){
table.push(result,{
status = _rejected;
reason = reason;
})
if(#result == #arrayPromise){
resolve(result)
}
}
for(k,v in arrayPromise){
..promise.resolve(v).then(onFulfilled,onRejected)
}
});
}
function isFuntion(fun,msg){
msg = msg ? msg:""
var isFuntion = type.function == type(fun);
if(!isFuntion) error(..string.concat(msg,"参数错误,参数必需是函数或者promise"))
}
function isPromise(value){
var _,t = type(value)
return t=='promise';
}
/**intellisense()
promise() = !promise.
promise(. = @promise(function(resolve,reject){})
promise.isPromise(arg) = if arg is promise \n@return true or false
promise.resolve()=!promise.
promise.resolve(arg) = 如果 arg 是值那么返回一个 fulfilled 状态的promise 如果arg是一个promise返回一个args执行后的promise
promise.reject()=!promise.
promise.reject(arg) = 静态方法,传入一个值返回 rejected 状态的promise
promise.any()=!promise.
promise.any(arrayPromies) = 参数为promise数组哪个promise最先返回就resolve它的结果
promise.allSettled()=!promise.
promise.allSettled(arrayPromise) = 参数为promise数组返回一个在所有给定的promise都已经fulfilled或rejected后的promise,并带有一个对象数组,每个对象表示对应的promise结果。
pomise.all()=!promise.
pomise.all(arrayPromies) = 参数为一个promise数组,只有所有promise为resolve才返回resolve数组 否则有一个reject则马上返回reject
!promise.clutch(callbackFunction) = 出错要执行的函数
!promise.clutch(callbackFunction) = @.clutch(function(item){})
!promise.finally(callbackFunction) = 总是执行的函数
!promise.finally(callbackFunction) = @.finally(function(item){})
!promise.then = @.then(onFulfilled,onRejected)
!promise.then() = !promise.
end intellisense**/