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 |
|
12 | function _gensync() {
|
13 | const data = require("gensync");
|
14 |
|
15 | _gensync = function () {
|
16 | return data;
|
17 | };
|
18 |
|
19 | return data;
|
20 | }
|
21 |
|
22 | const id = x => x;
|
23 |
|
24 | const runGenerator = _gensync()(function* (item) {
|
25 | return yield* item;
|
26 | });
|
27 |
|
28 | const isAsync = _gensync()({
|
29 | sync: () => false,
|
30 | errback: cb => cb(null, true)
|
31 | });
|
32 |
|
33 | exports.isAsync = isAsync;
|
34 |
|
35 | function maybeAsync(fn, message) {
|
36 | return _gensync()({
|
37 | sync(...args) {
|
38 | const result = fn.apply(this, args);
|
39 | if (isThenable(result)) throw new Error(message);
|
40 | return result;
|
41 | },
|
42 |
|
43 | async(...args) {
|
44 | return Promise.resolve(fn.apply(this, args));
|
45 | }
|
46 |
|
47 | });
|
48 | }
|
49 |
|
50 | const withKind = _gensync()({
|
51 | sync: cb => cb("sync"),
|
52 | async: cb => cb("async")
|
53 | });
|
54 |
|
55 | function forwardAsync(action, cb) {
|
56 | const g = _gensync()(action);
|
57 |
|
58 | return withKind(kind => {
|
59 | const adapted = g[kind];
|
60 | return cb(adapted);
|
61 | });
|
62 | }
|
63 |
|
64 | const onFirstPause = _gensync()({
|
65 | name: "onFirstPause",
|
66 | arity: 2,
|
67 | sync: function (item) {
|
68 | return runGenerator.sync(item);
|
69 | },
|
70 | errback: function (item, firstPause, cb) {
|
71 | let completed = false;
|
72 | runGenerator.errback(item, (err, value) => {
|
73 | completed = true;
|
74 | cb(err, value);
|
75 | });
|
76 |
|
77 | if (!completed) {
|
78 | firstPause();
|
79 | }
|
80 | }
|
81 | });
|
82 |
|
83 | exports.onFirstPause = onFirstPause;
|
84 |
|
85 | const waitFor = _gensync()({
|
86 | sync: id,
|
87 | async: id
|
88 | });
|
89 |
|
90 | exports.waitFor = waitFor;
|
91 |
|
92 | function isThenable(val) {
|
93 | return !!val && (typeof val === "object" || typeof val === "function") && !!val.then && typeof val.then === "function";
|
94 | }
|
95 |
|
96 | 0 && 0; |
\ | No newline at end of file |