[6a3a178] | 1 | var AwaitValue = require("./AwaitValue.js");
|
---|
| 2 |
|
---|
| 3 | function AsyncGenerator(gen) {
|
---|
| 4 | var front, back;
|
---|
| 5 |
|
---|
| 6 | function send(key, arg) {
|
---|
| 7 | return new Promise(function (resolve, reject) {
|
---|
| 8 | var request = {
|
---|
| 9 | key: key,
|
---|
| 10 | arg: arg,
|
---|
| 11 | resolve: resolve,
|
---|
| 12 | reject: reject,
|
---|
| 13 | next: null
|
---|
| 14 | };
|
---|
| 15 |
|
---|
| 16 | if (back) {
|
---|
| 17 | back = back.next = request;
|
---|
| 18 | } else {
|
---|
| 19 | front = back = request;
|
---|
| 20 | resume(key, arg);
|
---|
| 21 | }
|
---|
| 22 | });
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | function resume(key, arg) {
|
---|
| 26 | try {
|
---|
| 27 | var result = gen[key](arg);
|
---|
| 28 | var value = result.value;
|
---|
| 29 | var wrappedAwait = value instanceof AwaitValue;
|
---|
| 30 | Promise.resolve(wrappedAwait ? value.wrapped : value).then(function (arg) {
|
---|
| 31 | if (wrappedAwait) {
|
---|
| 32 | resume(key === "return" ? "return" : "next", arg);
|
---|
| 33 | return;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | settle(result.done ? "return" : "normal", arg);
|
---|
| 37 | }, function (err) {
|
---|
| 38 | resume("throw", err);
|
---|
| 39 | });
|
---|
| 40 | } catch (err) {
|
---|
| 41 | settle("throw", err);
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | function settle(type, value) {
|
---|
| 46 | switch (type) {
|
---|
| 47 | case "return":
|
---|
| 48 | front.resolve({
|
---|
| 49 | value: value,
|
---|
| 50 | done: true
|
---|
| 51 | });
|
---|
| 52 | break;
|
---|
| 53 |
|
---|
| 54 | case "throw":
|
---|
| 55 | front.reject(value);
|
---|
| 56 | break;
|
---|
| 57 |
|
---|
| 58 | default:
|
---|
| 59 | front.resolve({
|
---|
| 60 | value: value,
|
---|
| 61 | done: false
|
---|
| 62 | });
|
---|
| 63 | break;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | front = front.next;
|
---|
| 67 |
|
---|
| 68 | if (front) {
|
---|
| 69 | resume(front.key, front.arg);
|
---|
| 70 | } else {
|
---|
| 71 | back = null;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | this._invoke = send;
|
---|
| 76 |
|
---|
| 77 | if (typeof gen["return"] !== "function") {
|
---|
| 78 | this["return"] = undefined;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | AsyncGenerator.prototype[typeof Symbol === "function" && Symbol.asyncIterator || "@@asyncIterator"] = function () {
|
---|
| 83 | return this;
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | AsyncGenerator.prototype.next = function (arg) {
|
---|
| 87 | return this._invoke("next", arg);
|
---|
| 88 | };
|
---|
| 89 |
|
---|
| 90 | AsyncGenerator.prototype["throw"] = function (arg) {
|
---|
| 91 | return this._invoke("throw", arg);
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 | AsyncGenerator.prototype["return"] = function (arg) {
|
---|
| 95 | return this._invoke("return", arg);
|
---|
| 96 | };
|
---|
| 97 |
|
---|
| 98 | module.exports = AsyncGenerator;
|
---|
| 99 | module.exports["default"] = module.exports, module.exports.__esModule = true; |
---|