1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.forwardAsync = forwardAsync;
|
---|
7 | exports.isAsync = void 0;
|
---|
8 | exports.isThenable = isThenable;
|
---|
9 | exports.maybeAsync = maybeAsync;
|
---|
10 | exports.waitFor = exports.onFirstPause = void 0;
|
---|
11 | function _gensync() {
|
---|
12 | const data = require("gensync");
|
---|
13 | _gensync = function () {
|
---|
14 | return data;
|
---|
15 | };
|
---|
16 | return data;
|
---|
17 | }
|
---|
18 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
---|
19 | function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
---|
20 | const runGenerator = _gensync()(function* (item) {
|
---|
21 | return yield* item;
|
---|
22 | });
|
---|
23 | const isAsync = exports.isAsync = _gensync()({
|
---|
24 | sync: () => false,
|
---|
25 | errback: cb => cb(null, true)
|
---|
26 | });
|
---|
27 | function maybeAsync(fn, message) {
|
---|
28 | return _gensync()({
|
---|
29 | sync(...args) {
|
---|
30 | const result = fn.apply(this, args);
|
---|
31 | if (isThenable(result)) throw new Error(message);
|
---|
32 | return result;
|
---|
33 | },
|
---|
34 | async(...args) {
|
---|
35 | return Promise.resolve(fn.apply(this, args));
|
---|
36 | }
|
---|
37 | });
|
---|
38 | }
|
---|
39 | const withKind = _gensync()({
|
---|
40 | sync: cb => cb("sync"),
|
---|
41 | async: function () {
|
---|
42 | var _ref = _asyncToGenerator(function* (cb) {
|
---|
43 | return cb("async");
|
---|
44 | });
|
---|
45 | return function async(_x) {
|
---|
46 | return _ref.apply(this, arguments);
|
---|
47 | };
|
---|
48 | }()
|
---|
49 | });
|
---|
50 | function forwardAsync(action, cb) {
|
---|
51 | const g = _gensync()(action);
|
---|
52 | return withKind(kind => {
|
---|
53 | const adapted = g[kind];
|
---|
54 | return cb(adapted);
|
---|
55 | });
|
---|
56 | }
|
---|
57 | const onFirstPause = exports.onFirstPause = _gensync()({
|
---|
58 | name: "onFirstPause",
|
---|
59 | arity: 2,
|
---|
60 | sync: function (item) {
|
---|
61 | return runGenerator.sync(item);
|
---|
62 | },
|
---|
63 | errback: function (item, firstPause, cb) {
|
---|
64 | let completed = false;
|
---|
65 | runGenerator.errback(item, (err, value) => {
|
---|
66 | completed = true;
|
---|
67 | cb(err, value);
|
---|
68 | });
|
---|
69 | if (!completed) {
|
---|
70 | firstPause();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | });
|
---|
74 | const waitFor = exports.waitFor = _gensync()({
|
---|
75 | sync: x => x,
|
---|
76 | async: function () {
|
---|
77 | var _ref2 = _asyncToGenerator(function* (x) {
|
---|
78 | return x;
|
---|
79 | });
|
---|
80 | return function async(_x2) {
|
---|
81 | return _ref2.apply(this, arguments);
|
---|
82 | };
|
---|
83 | }()
|
---|
84 | });
|
---|
85 | function isThenable(val) {
|
---|
86 | return !!val && (typeof val === "object" || typeof val === "function") && !!val.then && typeof val.then === "function";
|
---|
87 | }
|
---|
88 | 0 && 0;
|
---|
89 |
|
---|
90 | //# sourceMappingURL=async.js.map
|
---|