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