-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzco.browser.js
675 lines (510 loc) · 12.4 KB
/
zco.browser.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
(function (win) {
/**
* Created by jason on 2017/8/17.
*/
var slice = Array.prototype.slice;
var toString = Object.prototype.toString;
/**
* Judge if the target value is a zco future
*
* @param {Mixed} future
* @return {Boolean}
* @api private
* */
var isZcoFuture = function (future) {
return future && ("function" === typeof future.__suspend__) && ("function" === typeof future.__ctx__);
}
/**
* Judge if the object is an instance of Promise
* @param {Object} pro
* @return {Boolean}
* @api private
* */
var isPromise = function (pro) {
return pro && ("function" === typeof pro.then) && ("function" === typeof pro.catch );
}
/**
* The two runtime model flag of `zco_core`
* */
var WRAP_DEFER_MODEL = 1;
var BRIEF_MODEl = 2;
/**
* the core of zco
*
* @param {GeneratorFunction} gen
* @param {Number} model
* @return {Function} future
*
* @api private
*
* */
var zco_core = function (gen, model) {
/**
* references
* */
var iterator = null,
handler = null,
deferFunc = null,
current_child_future = null;
/**
* flags
* */
var resumed = true, // if the coroutine has already been resumed
suspended = false, // if the coroutine has already been suspended
exited = false, // if the coroutine has already exited
is_run_future = false;
/**
* Be used to save the context
* */
var _this = {
"ctx": {}
};
/**
* Run the handler fo zco (The handler provided by user)
* */
var zco_core_run_handler = function (error, value) {
if (handler != null) {
return handler.apply(_this, [error, value]);
}
if (error) {
throw error; //your code throws an error ,but no handler provided ,zco throws it out ,do not catch silently
}
}
/**
*build defer func,and deliver error
* */
var zco_core_make_defer_func = function (error) {
return zco_core(deferFunc, WRAP_DEFER_MODEL, error); //
}
/**
*End the current coroutine
* */
var zco_core_exit = function (e, v) {
if (exited) {
return;
}
exited = true;
/**
* Run defer
* */
if (deferFunc != null) {
var _func = zco_core_make_defer_func(e);
_func.__ctx__(_this.ctx);
return _func(function (ee) {
if (ee != null && handler === null) {
throw ee; //error occurred in defer,throw out if no handler provided
} else {
zco_core_run_handler(e || ee, v)
}
});
}
/**
* Run handler
* */
return zco_core_run_handler(e, v);
}
/**
* Resume the coroutine
* */
var zco_core_run = function (arg) {
var v = null,
error = null;
resumed = true;
try {
v = iterator.next(arg);
} catch (e) {
error = e;
}
resumed = false;
/**
* if `error` is not null ,end the coroutine
* */
if (error != null) {
return zco_core_exit(error);
}
/**
* if it is finished ,end the coroutine
* */
if (v.done) {
return zco_core_exit(null, v.value);
}
/**
* if the value is a future of zco ,invoke the future and resume the coroutine automatically
* */
if (isZcoFuture(v.value)) {
current_child_future = v.value;
is_run_future = true;
current_child_future.__ctx__(_this.ctx);
return current_child_future(zco_core_resume);
}
/**
* As you can see, if the result returned by the expression after `yield` is not a zco future,it
* will be ignored .And why not assign to the variable declared before `yield`? Because it will cause
* conflicts .Zco is designed to work with callback-style-function directly ,so that you do not need to
* do any wrap with this kind of function. When `yield` a callback-style-function ,zco can't predict if
* you will run `co_next` in the function(the callback).For the sake of safety ,zco ignore the result.
*
* And yielding a sync function (or just a value) is also meaningless, it just like code below:
*
*```js
* var a = yield 1;
*
*```
* Although it's Correct in grammar .
*
* So ,in zco ,you can yield a zco future directly ,and you can also yield a callback-style-function,
* and it is your duty to run `co_next` in the callback-style-function to resume the coroutine.
*
* */
}
/**
* define defer
* @param {GeneratorFunction} func
* @return {Null}
* @api public
* */
var defer = function (func) {
if (deferFunc != null) {
throw new Error("you can only defer once");
}
if ("[object GeneratorFunction]" !== toString.call(func)) {
throw new TypeError("The arg of refer must be a generator function!");
}
deferFunc = func;
}
/**
* define zco future
* @param {Function} handler
* @api public
* */
var zco_core_future = function (han) {
if ("function" == typeof han) {
handler = han;
}
zco_core_run();
}
/**
* define 'suspend' method
* */
zco_core_future.__suspend__ = function () {
if (exited || suspended) {
return;
}
suspended = true;
exited = true;
if (deferFunc != null) {
var _func = zco_core_make_defer_func(new Error("coroutine is suspended,maybe because of timeout."));
/**
* run defer ,ignore the error occurred in defer
* */
_func(function () {});
}
/**
* suspend child future
* */
if (current_child_future != null) {
current_child_future.__suspend__();
}
iterator.return ();
}
/**
* A method used to deliver context
* */
zco_core_future.__ctx__ = function (ctx) {
_this.ctx = ctx;
}
/**
* The common callback 'co_next'
*
* Resume the coroutine
*
* @param {Mixed} ....
* @api public
* */
var zco_core_resume = function () {
if (suspended || exited) {
return;
}
var arg = slice.call(arguments);
/**
* Judge the runtime model
* */
if (model === BRIEF_MODEl && true === is_run_future) {
if (arg[0] !== null) {
return zco_core_exit(arg[0]);
}
arg = arg[1];
}
is_run_future = false;
/**
* Already resumed ,but resume again,it means that the action is not async.
* Convert it to an async action by setTimeout.
* support fake async operation,avoid error: "Generator is already running"
* */
if (resumed) {
setTimeout(function () {
return zco_core_run(arg);
}, 0);
} else {
return zco_core_run(arg);
}
}
zco_core_resume.ctx = function () {
return _this.ctx;
}
if ("[object GeneratorFunction]" === toString.call(gen)) { //todo: support other Generator implements
if (model === WRAP_DEFER_MODEL) {
iterator = gen.apply(_this, [zco_core_resume, arguments[2]]);
} else {
iterator = gen.apply(_this, [zco_core_resume, defer]);
}
} else {
throw new TypeError("The arg of co must be a generator function!");
}
return zco_core_future;
}
/**
* The method `zco.all`
* @param {Function} future...
* @param {Number} timeout (optional)
* @return {Function} future
* @api public
* */
var all = function () {
var timeout_handle = null,
callback = null,
exited = false,
actions = [],
timeout = null,
args = slice.call(arguments);
var _this = {
"ctx": {}
};
var timeout_error = new Error("timeout");
for (var i = 0; i < args.length; i++) {
if ("function" == typeof args[i]) {
actions.push(args[i]);
} else if ("[object Number]" === toString.call(args[i])) {
/**
* if the arg is number , treat as timeout
* */
timeout = args[i];
break;
} else {
break;
}
}
var _suspend_zco_future = function () {
for (var i = 0; i < actions.length; i++) {
if (isZcoFuture(actions[i])) {
actions[i].__suspend__();
}
}
}
var _exit = function (error, result, is_timeout) {
if (exited) {
return;
}
exited = true;
if (!is_timeout && timeout_handle !== null) {
clearTimeout(timeout_handle);
}
if (is_timeout || null !== error) { //self timeout or error occurred ,suspend zco futures
_suspend_zco_future();
}
if (callback !== null) {
return callback.apply(_this, [error, result]);
}
if (error) { //your code throws an error ,but no handler provided ,zco throws it out ,do not catch silently
throw error; //something error ,or timeout
}
}
var _run = function () {
var has_done = 0,
result = [];
var check = function () {
if (exited) {
return;
}
has_done++;
if (has_done == actions.length) {
_exit(null, result, false);
}
}
if (actions.length == 0) {
return _exit(null, [], false);
}
for (var i = 0; i < actions.length; i++) {
(function (index) {
var _action = actions[index];
if (isZcoFuture(_action)) {
_action.__ctx__(_this.ctx);
_action(function (err, data) {
if (err) {
return _exit(err, null, false);
}
result[index] = data;
check();
});
} else {
try {
var slave_cb = function () {
var return_value = slice.call(arguments);
result[index] = return_value;
check();
};
slave_cb.ctx = function () {
return _this.ctx;
}
_action(slave_cb);
} catch (e) {
_exit(e, null, false);
}
}
})(i)
}
if (timeout !== null) {
timeout_handle = setTimeout(function () {
_exit(timeout_error, null, true);
}, timeout)
}
}
var future = function (handler) {
if ("function" == typeof handler) {
callback = handler;
}
_run();
};
future.__suspend__ = function () {
if (exited) {
return;
}
exited = true;
if (timeout_handle != null) {
clearTimeout(timeout_handle);
}
_suspend_zco_future();
}
future.__ctx__ = function (ctx) {
_this.ctx = ctx;
}
return future;
}
/**
* The method 'zco.wrapPromise'
*
* I can't yield Promise directly ,that's unsafe.Becauce some callback-style API also return a Promise at the
* same time,such as `pg.client.query`.
*
*
* @param {Promise} pro
* @return {Function} future
* @api public
* */
var wrapPromise = function (pro) {
if (!isPromise(pro)) {
throw new TypeError("The arg of wrapPromise must be an instance of Promise!");
}
var future = function (callback) {
var _end = function (err, data) {
callback && callback(err, data);
}
pro.then(function (data) {
_end(null, data);
}).catch (function (err) {
_end(err);
});
}
future.__suspend__ = function () {};
future.__ctx__ = function () {}
return future;
}
/**
* The method 'zco.timeLimit'
* @param {Number} ms (time limit)
* @param {Function} future
* @return {Function} future
* @api public
* */
var timeLimit = function (ms, future) {
var timeout_handle = null,
is_timeout = false,
exited = false,
callback = null;
var _this = {
"ctx": {}
};
if ("[object Number]" !== toString.call(ms) || ms < 0) {
throw new TypeError("Illegal timeout setting!")
}
if (!isZcoFuture(future)) {
throw new TypeError("You can only set timeout of zco future(value returned by zco)");
}
var timeout_error = new Error("timeout");
var handler = function (err, result) {
if (exited) {
return;
}
exited = true;
if (!is_timeout && timeout_handle != null) {
clearTimeout(timeout_handle);
}
if (is_timeout) { //if timeout,suspend the operation
future.__suspend__();
}
if (callback) {
return callback.apply(_this, [err, result]);
}
if (err) { //your code throws an error ,but no handler provided ,zco throws it out ,do not catch silently
throw err;
}
}
var _future = function (cb) {
if ("function" == typeof cb) {
callback = cb;
}
timeout_handle = setTimeout(function () {
is_timeout = true;
handler(timeout_error);
}, ms);
future.__ctx__(_this.ctx);
future(handler);
}
_future.__suspend__ = function () {
if (exited) {
return;
}
exited = true;
if (!is_timeout && timeout_handle !== null) {
clearTimeout(timeout_handle);
}
future.__suspend__();
}
_future.__ctx__ = function (ctx) {
_this.ctx = ctx;
}
return _future;
}
/**
* The main method "zco()";
* @param {GeneratorFunction} gen
* @return {Function} future
* @api public
* */
var co = function (gen) {
return zco_core(gen);
}
/**
* The brief model fo 'zco'
* @param {GeneratorFunction} gen
* @return {Function} future
* @api public
* */
co.brief = function (gen) {
return zco_core(gen, BRIEF_MODEl);
}
co.all = all;
co.wrapPromise = wrapPromise;
co.timeLimit = timeLimit;
win.zco = co;
})(window)