UNPKG

1.79 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.forwardAsync = forwardAsync;
7exports.isAsync = void 0;
8exports.isThenable = isThenable;
9exports.maybeAsync = maybeAsync;
10exports.waitFor = exports.onFirstPause = void 0;
11
12function _gensync() {
13 const data = require("gensync");
14
15 _gensync = function () {
16 return data;
17 };
18
19 return data;
20}
21
22const id = x => x;
23
24const runGenerator = _gensync()(function* (item) {
25 return yield* item;
26});
27
28const isAsync = _gensync()({
29 sync: () => false,
30 errback: cb => cb(null, true)
31});
32
33exports.isAsync = isAsync;
34
35function 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
50const withKind = _gensync()({
51 sync: cb => cb("sync"),
52 async: cb => cb("async")
53});
54
55function 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
64const 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
83exports.onFirstPause = onFirstPause;
84
85const waitFor = _gensync()({
86 sync: id,
87 async: id
88});
89
90exports.waitFor = waitFor;
91
92function isThenable(val) {
93 return !!val && (typeof val === "object" || typeof val === "function") && !!val.then && typeof val.then === "function";
94}
95
960 && 0;
\No newline at end of file