UNPKG

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