版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Task.js 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.getMonoid = exports.getSemigroup = exports.taskSeq = exports.task = exports.sequenceSeqArray = exports.traverseSeqArray = exports.traverseSeqArrayWithIndex = exports.sequenceArray = exports.traverseArray = exports.traverseArrayWithIndex = exports.traverseReadonlyArrayWithIndexSeq = exports.traverseReadonlyNonEmptyArrayWithIndexSeq = exports.traverseReadonlyArrayWithIndex = exports.traverseReadonlyNonEmptyArrayWithIndex = exports.ApT = exports.apS = exports.bind = exports.let = exports.bindTo = exports.Do = exports.never = exports.FromTask = exports.chainFirstIOK = exports.chainIOK = exports.fromIOK = exports.FromIO = exports.chainFirst = exports.MonadTask = exports.fromTask = exports.MonadIO = exports.Monad = exports.Chain = exports.ApplicativeSeq = exports.ApplySeq = exports.ApplicativePar = exports.apSecond = exports.apFirst = exports.ApplyPar = exports.Pointed = exports.flap = exports.Functor = exports.getRaceMonoid = exports.URI = exports.flatten = exports.chain = exports.of = exports.ap = exports.map = exports.delay = exports.fromIO = void 0;
  27. /**
  28. * ```ts
  29. * interface Task<A> {
  30. * (): Promise<A>
  31. * }
  32. * ```
  33. *
  34. * `Task<A>` represents an asynchronous computation that yields a value of type `A` and **never fails**.
  35. * If you want to represent an asynchronous computation that may fail, please see `TaskEither`.
  36. *
  37. * @since 2.0.0
  38. */
  39. var Applicative_1 = require("./Applicative");
  40. var Apply_1 = require("./Apply");
  41. var Chain_1 = require("./Chain");
  42. var FromIO_1 = require("./FromIO");
  43. var function_1 = require("./function");
  44. var Functor_1 = require("./Functor");
  45. var _ = __importStar(require("./internal"));
  46. // -------------------------------------------------------------------------------------
  47. // conversions
  48. // -------------------------------------------------------------------------------------
  49. /**
  50. * @category conversions
  51. * @since 2.0.0
  52. */
  53. var fromIO = function (ma) { return function () { return Promise.resolve().then(ma); }; };
  54. exports.fromIO = fromIO;
  55. // -------------------------------------------------------------------------------------
  56. // combinators
  57. // -------------------------------------------------------------------------------------
  58. /**
  59. * Creates a task that will complete after a time delay
  60. *
  61. * @example
  62. * import { sequenceT } from 'fp-ts/Apply'
  63. * import * as T from 'fp-ts/Task'
  64. * import { takeRight } from 'fp-ts/Array'
  65. *
  66. * async function test() {
  67. * const log: Array<string> = []
  68. * const append = (message: string): T.Task<void> =>
  69. * T.fromIO(() => {
  70. * log.push(message)
  71. * })
  72. * const fa = append('a')
  73. * const fb = T.delay(20)(append('b'))
  74. * const fc = T.delay(10)(append('c'))
  75. * const fd = append('d')
  76. * await sequenceT(T.ApplyPar)(fa, fb, fc, fd)()
  77. * assert.deepStrictEqual(takeRight(2)(log), ['c', 'b'])
  78. * }
  79. *
  80. * test()
  81. *
  82. * @since 2.0.0
  83. */
  84. function delay(millis) {
  85. return function (ma) { return function () {
  86. return new Promise(function (resolve) {
  87. setTimeout(function () {
  88. Promise.resolve().then(ma).then(resolve);
  89. }, millis);
  90. });
  91. }; };
  92. }
  93. exports.delay = delay;
  94. var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); };
  95. var _apPar = function (fab, fa) { return (0, function_1.pipe)(fab, (0, exports.ap)(fa)); };
  96. var _apSeq = function (fab, fa) {
  97. return (0, function_1.pipe)(fab, (0, exports.chain)(function (f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); }));
  98. };
  99. var _chain = function (ma, f) { return (0, function_1.pipe)(ma, (0, exports.chain)(f)); };
  100. /**
  101. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  102. * use the type constructor `F` to represent some computational context.
  103. *
  104. * @category mapping
  105. * @since 2.0.0
  106. */
  107. var map = function (f) { return function (fa) { return function () {
  108. return Promise.resolve().then(fa).then(f);
  109. }; }; };
  110. exports.map = map;
  111. /**
  112. * @since 2.0.0
  113. */
  114. var ap = function (fa) { return function (fab) { return function () {
  115. return Promise.all([Promise.resolve().then(fab), Promise.resolve().then(fa)]).then(function (_a) {
  116. var f = _a[0], a = _a[1];
  117. return f(a);
  118. });
  119. }; }; };
  120. exports.ap = ap;
  121. /**
  122. * @category constructors
  123. * @since 2.0.0
  124. */
  125. var of = function (a) { return function () { return Promise.resolve(a); }; };
  126. exports.of = of;
  127. /**
  128. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  129. *
  130. * @category sequencing
  131. * @since 2.0.0
  132. */
  133. var chain = function (f) { return function (ma) { return function () {
  134. return Promise.resolve()
  135. .then(ma)
  136. .then(function (a) { return f(a)(); });
  137. }; }; };
  138. exports.chain = chain;
  139. /**
  140. * @category sequencing
  141. * @since 2.0.0
  142. */
  143. exports.flatten = (0, exports.chain)(function_1.identity);
  144. /**
  145. * @category type lambdas
  146. * @since 2.0.0
  147. */
  148. exports.URI = 'Task';
  149. /**
  150. * Monoid returning the first completed task.
  151. *
  152. * Note: uses `Promise.race` internally.
  153. *
  154. * @example
  155. * import * as T from 'fp-ts/Task'
  156. *
  157. * async function test() {
  158. * const S = T.getRaceMonoid<string>()
  159. * const fa = T.delay(20)(T.of('a'))
  160. * const fb = T.delay(10)(T.of('b'))
  161. * assert.deepStrictEqual(await S.concat(fa, fb)(), 'b')
  162. * }
  163. *
  164. * test()
  165. *
  166. * @category instances
  167. * @since 2.0.0
  168. */
  169. function getRaceMonoid() {
  170. return {
  171. concat: function (x, y) { return function () { return Promise.race([Promise.resolve().then(x), Promise.resolve().then(y)]); }; },
  172. empty: exports.never
  173. };
  174. }
  175. exports.getRaceMonoid = getRaceMonoid;
  176. /**
  177. * @category instances
  178. * @since 2.7.0
  179. */
  180. exports.Functor = {
  181. URI: exports.URI,
  182. map: _map
  183. };
  184. /**
  185. * @category mapping
  186. * @since 2.10.0
  187. */
  188. exports.flap = (0, Functor_1.flap)(exports.Functor);
  189. /**
  190. * @category instances
  191. * @since 2.10.0
  192. */
  193. exports.Pointed = {
  194. URI: exports.URI,
  195. of: exports.of
  196. };
  197. /**
  198. * Runs computations in parallel.
  199. *
  200. * @category instances
  201. * @since 2.10.0
  202. */
  203. exports.ApplyPar = {
  204. URI: exports.URI,
  205. map: _map,
  206. ap: _apPar
  207. };
  208. /**
  209. * Combine two effectful actions, keeping only the result of the first.
  210. *
  211. * @since 2.0.0
  212. */
  213. exports.apFirst = (0, Apply_1.apFirst)(exports.ApplyPar);
  214. /**
  215. * Combine two effectful actions, keeping only the result of the second.
  216. *
  217. * @since 2.0.0
  218. */
  219. exports.apSecond = (0, Apply_1.apSecond)(exports.ApplyPar);
  220. /**
  221. * Runs computations in parallel.
  222. *
  223. * @category instances
  224. * @since 2.7.0
  225. */
  226. exports.ApplicativePar = {
  227. URI: exports.URI,
  228. map: _map,
  229. ap: _apPar,
  230. of: exports.of
  231. };
  232. /**
  233. * Runs computations sequentially.
  234. *
  235. * @category instances
  236. * @since 2.10.0
  237. */
  238. exports.ApplySeq = {
  239. URI: exports.URI,
  240. map: _map,
  241. ap: _apSeq
  242. };
  243. /**
  244. * Runs computations sequentially.
  245. *
  246. * @category instances
  247. * @since 2.7.0
  248. */
  249. exports.ApplicativeSeq = {
  250. URI: exports.URI,
  251. map: _map,
  252. ap: _apSeq,
  253. of: exports.of
  254. };
  255. /**
  256. * @category instances
  257. * @since 2.10.0
  258. */
  259. exports.Chain = {
  260. URI: exports.URI,
  261. map: _map,
  262. ap: _apPar,
  263. chain: _chain
  264. };
  265. /**
  266. * @category instances
  267. * @since 2.10.0
  268. */
  269. exports.Monad = {
  270. URI: exports.URI,
  271. map: _map,
  272. of: exports.of,
  273. ap: _apPar,
  274. chain: _chain
  275. };
  276. /**
  277. * @category instances
  278. * @since 2.10.0
  279. */
  280. exports.MonadIO = {
  281. URI: exports.URI,
  282. map: _map,
  283. of: exports.of,
  284. ap: _apPar,
  285. chain: _chain,
  286. fromIO: exports.fromIO
  287. };
  288. /**
  289. * @category zone of death
  290. * @since 2.7.0
  291. * @deprecated
  292. */
  293. exports.fromTask = function_1.identity;
  294. /**
  295. * @category instances
  296. * @since 2.10.0
  297. */
  298. exports.MonadTask = {
  299. URI: exports.URI,
  300. map: _map,
  301. of: exports.of,
  302. ap: _apPar,
  303. chain: _chain,
  304. fromIO: exports.fromIO,
  305. fromTask: exports.fromTask
  306. };
  307. /**
  308. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  309. * keeping only the result of the first.
  310. *
  311. * @category sequencing
  312. * @since 2.0.0
  313. */
  314. exports.chainFirst = (0, Chain_1.chainFirst)(exports.Chain);
  315. /**
  316. * @category instances
  317. * @since 2.10.0
  318. */
  319. exports.FromIO = {
  320. URI: exports.URI,
  321. fromIO: exports.fromIO
  322. };
  323. /**
  324. * @category lifting
  325. * @since 2.4.0
  326. */
  327. exports.fromIOK =
  328. /*#__PURE__*/ (0, FromIO_1.fromIOK)(exports.FromIO);
  329. /**
  330. * @category sequencing
  331. * @since 2.4.0
  332. */
  333. exports.chainIOK = (0, FromIO_1.chainIOK)(exports.FromIO, exports.Chain);
  334. /**
  335. * @category sequencing
  336. * @since 2.10.0
  337. */
  338. exports.chainFirstIOK = (0, FromIO_1.chainFirstIOK)(exports.FromIO, exports.Chain);
  339. /**
  340. * @category instances
  341. * @since 2.10.0
  342. */
  343. exports.FromTask = {
  344. URI: exports.URI,
  345. fromIO: exports.fromIO,
  346. fromTask: exports.fromTask
  347. };
  348. // -------------------------------------------------------------------------------------
  349. // utils
  350. // -------------------------------------------------------------------------------------
  351. /**
  352. * A `Task` that never completes.
  353. *
  354. * @since 2.0.0
  355. */
  356. var never = function () { return new Promise(function (_) { return undefined; }); };
  357. exports.never = never;
  358. // -------------------------------------------------------------------------------------
  359. // do notation
  360. // -------------------------------------------------------------------------------------
  361. /**
  362. * @category do notation
  363. * @since 2.9.0
  364. */
  365. exports.Do = (0, exports.of)(_.emptyRecord);
  366. /**
  367. * @category do notation
  368. * @since 2.8.0
  369. */
  370. exports.bindTo = (0, Functor_1.bindTo)(exports.Functor);
  371. var let_ = /*#__PURE__*/ (0, Functor_1.let)(exports.Functor);
  372. exports.let = let_;
  373. /**
  374. * @category do notation
  375. * @since 2.8.0
  376. */
  377. exports.bind = (0, Chain_1.bind)(exports.Chain);
  378. /**
  379. * @category do notation
  380. * @since 2.8.0
  381. */
  382. exports.apS = (0, Apply_1.apS)(exports.ApplyPar);
  383. /**
  384. * @since 2.11.0
  385. */
  386. exports.ApT = (0, exports.of)(_.emptyReadonlyArray);
  387. // -------------------------------------------------------------------------------------
  388. // array utils
  389. // -------------------------------------------------------------------------------------
  390. /**
  391. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`.
  392. *
  393. * @category traversing
  394. * @since 2.11.0
  395. */
  396. var traverseReadonlyNonEmptyArrayWithIndex = function (f) {
  397. return function (as) {
  398. return function () {
  399. return Promise.all(as.map(function (a, i) { return Promise.resolve().then(function () { return f(i, a)(); }); }));
  400. };
  401. };
  402. };
  403. exports.traverseReadonlyNonEmptyArrayWithIndex = traverseReadonlyNonEmptyArrayWithIndex;
  404. /**
  405. * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`.
  406. *
  407. * @category traversing
  408. * @since 2.11.0
  409. */
  410. var traverseReadonlyArrayWithIndex = function (f) {
  411. var g = (0, exports.traverseReadonlyNonEmptyArrayWithIndex)(f);
  412. return function (as) { return (_.isNonEmpty(as) ? g(as) : exports.ApT); };
  413. };
  414. exports.traverseReadonlyArrayWithIndex = traverseReadonlyArrayWithIndex;
  415. /**
  416. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`.
  417. *
  418. * @category traversing
  419. * @since 2.11.0
  420. */
  421. var traverseReadonlyNonEmptyArrayWithIndexSeq = function (f) {
  422. return function (as) {
  423. return function () {
  424. return _.tail(as).reduce(function (acc, a, i) {
  425. return acc.then(function (bs) {
  426. return Promise.resolve()
  427. .then(f(i + 1, a))
  428. .then(function (b) {
  429. bs.push(b);
  430. return bs;
  431. });
  432. });
  433. }, Promise.resolve()
  434. .then(f(0, _.head(as)))
  435. .then(_.singleton));
  436. };
  437. };
  438. };
  439. exports.traverseReadonlyNonEmptyArrayWithIndexSeq = traverseReadonlyNonEmptyArrayWithIndexSeq;
  440. /**
  441. * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
  442. *
  443. * @category traversing
  444. * @since 2.11.0
  445. */
  446. var traverseReadonlyArrayWithIndexSeq = function (f) {
  447. var g = (0, exports.traverseReadonlyNonEmptyArrayWithIndexSeq)(f);
  448. return function (as) { return (_.isNonEmpty(as) ? g(as) : exports.ApT); };
  449. };
  450. exports.traverseReadonlyArrayWithIndexSeq = traverseReadonlyArrayWithIndexSeq;
  451. /**
  452. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  453. *
  454. * @category traversing
  455. * @since 2.9.0
  456. */
  457. exports.traverseArrayWithIndex = exports.traverseReadonlyArrayWithIndex;
  458. /**
  459. * Equivalent to `ReadonlyArray#traverse(Applicative)`.
  460. *
  461. * @category traversing
  462. * @since 2.9.0
  463. */
  464. var traverseArray = function (f) {
  465. return (0, exports.traverseReadonlyArrayWithIndex)(function (_, a) { return f(a); });
  466. };
  467. exports.traverseArray = traverseArray;
  468. /**
  469. * Equivalent to `ReadonlyArray#sequence(Applicative)`.
  470. *
  471. * @category traversing
  472. * @since 2.9.0
  473. */
  474. exports.sequenceArray =
  475. /*#__PURE__*/ (0, exports.traverseArray)(function_1.identity);
  476. /**
  477. * Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
  478. *
  479. * @category traversing
  480. * @since 2.9.0
  481. */
  482. exports.traverseSeqArrayWithIndex = exports.traverseReadonlyArrayWithIndexSeq;
  483. /**
  484. * Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`.
  485. *
  486. * @category traversing
  487. * @since 2.9.0
  488. */
  489. var traverseSeqArray = function (f) {
  490. return (0, exports.traverseReadonlyArrayWithIndexSeq)(function (_, a) { return f(a); });
  491. };
  492. exports.traverseSeqArray = traverseSeqArray;
  493. /**
  494. * Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`.
  495. *
  496. * @category traversing
  497. * @since 2.9.0
  498. */
  499. exports.sequenceSeqArray =
  500. /*#__PURE__*/ (0, exports.traverseSeqArray)(function_1.identity);
  501. // -------------------------------------------------------------------------------------
  502. // deprecated
  503. // -------------------------------------------------------------------------------------
  504. /**
  505. * This instance is deprecated, use small, specific instances instead.
  506. * For example if a function needs a `Functor` instance, pass `T.Functor` instead of `T.task`
  507. * (where `T` is from `import T from 'fp-ts/Task'`)
  508. *
  509. * @category zone of death
  510. * @since 2.0.0
  511. * @deprecated
  512. */
  513. exports.task = {
  514. URI: exports.URI,
  515. map: _map,
  516. of: exports.of,
  517. ap: _apPar,
  518. chain: _chain,
  519. fromIO: exports.fromIO,
  520. fromTask: exports.fromTask
  521. };
  522. /**
  523. * This instance is deprecated, use small, specific instances instead.
  524. * For example if a function needs a `Functor` instance, pass `T.Functor` instead of `T.taskSeq`
  525. * (where `T` is from `import T from 'fp-ts/Task'`)
  526. *
  527. * @category zone of death
  528. * @since 2.0.0
  529. * @deprecated
  530. */
  531. exports.taskSeq = {
  532. URI: exports.URI,
  533. map: _map,
  534. of: exports.of,
  535. ap: _apSeq,
  536. chain: _chain,
  537. fromIO: exports.fromIO,
  538. fromTask: exports.fromTask
  539. };
  540. /**
  541. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  542. *
  543. * @category zone of death
  544. * @since 2.0.0
  545. * @deprecated
  546. */
  547. exports.getSemigroup = (0, Apply_1.getApplySemigroup)(exports.ApplySeq);
  548. /**
  549. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  550. *
  551. * Lift a monoid into 'Task', the inner values are concatenated using the provided `Monoid`.
  552. *
  553. * @category zone of death
  554. * @since 2.0.0
  555. * @deprecated
  556. */
  557. exports.getMonoid = (0, Applicative_1.getApplicativeMonoid)(exports.ApplicativeSeq);