Innovenergy_trunk/frontend/node_modules/fp-ts/lib/Task.js

558 lines
16 KiB
JavaScript
Raw Permalink Normal View History

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
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;
/**
* ```ts
* interface Task<A> {
* (): Promise<A>
* }
* ```
*
* `Task<A>` represents an asynchronous computation that yields a value of type `A` and **never fails**.
* If you want to represent an asynchronous computation that may fail, please see `TaskEither`.
*
* @since 2.0.0
*/
var Applicative_1 = require("./Applicative");
var Apply_1 = require("./Apply");
var Chain_1 = require("./Chain");
var FromIO_1 = require("./FromIO");
var function_1 = require("./function");
var Functor_1 = require("./Functor");
var _ = __importStar(require("./internal"));
// -------------------------------------------------------------------------------------
// conversions
// -------------------------------------------------------------------------------------
/**
* @category conversions
* @since 2.0.0
*/
var fromIO = function (ma) { return function () { return Promise.resolve().then(ma); }; };
exports.fromIO = fromIO;
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
/**
* Creates a task that will complete after a time delay
*
* @example
* import { sequenceT } from 'fp-ts/Apply'
* import * as T from 'fp-ts/Task'
* import { takeRight } from 'fp-ts/Array'
*
* async function test() {
* const log: Array<string> = []
* const append = (message: string): T.Task<void> =>
* T.fromIO(() => {
* log.push(message)
* })
* const fa = append('a')
* const fb = T.delay(20)(append('b'))
* const fc = T.delay(10)(append('c'))
* const fd = append('d')
* await sequenceT(T.ApplyPar)(fa, fb, fc, fd)()
* assert.deepStrictEqual(takeRight(2)(log), ['c', 'b'])
* }
*
* test()
*
* @since 2.0.0
*/
function delay(millis) {
return function (ma) { return function () {
return new Promise(function (resolve) {
setTimeout(function () {
Promise.resolve().then(ma).then(resolve);
}, millis);
});
}; };
}
exports.delay = delay;
var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); };
var _apPar = function (fab, fa) { return (0, function_1.pipe)(fab, (0, exports.ap)(fa)); };
var _apSeq = function (fab, fa) {
return (0, function_1.pipe)(fab, (0, exports.chain)(function (f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); }));
};
var _chain = function (ma, f) { return (0, function_1.pipe)(ma, (0, exports.chain)(f)); };
/**
* `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
* use the type constructor `F` to represent some computational context.
*
* @category mapping
* @since 2.0.0
*/
var map = function (f) { return function (fa) { return function () {
return Promise.resolve().then(fa).then(f);
}; }; };
exports.map = map;
/**
* @since 2.0.0
*/
var ap = function (fa) { return function (fab) { return function () {
return Promise.all([Promise.resolve().then(fab), Promise.resolve().then(fa)]).then(function (_a) {
var f = _a[0], a = _a[1];
return f(a);
});
}; }; };
exports.ap = ap;
/**
* @category constructors
* @since 2.0.0
*/
var of = function (a) { return function () { return Promise.resolve(a); }; };
exports.of = of;
/**
* Composes computations in sequence, using the return value of one computation to determine the next computation.
*
* @category sequencing
* @since 2.0.0
*/
var chain = function (f) { return function (ma) { return function () {
return Promise.resolve()
.then(ma)
.then(function (a) { return f(a)(); });
}; }; };
exports.chain = chain;
/**
* @category sequencing
* @since 2.0.0
*/
exports.flatten = (0, exports.chain)(function_1.identity);
/**
* @category type lambdas
* @since 2.0.0
*/
exports.URI = 'Task';
/**
* Monoid returning the first completed task.
*
* Note: uses `Promise.race` internally.
*
* @example
* import * as T from 'fp-ts/Task'
*
* async function test() {
* const S = T.getRaceMonoid<string>()
* const fa = T.delay(20)(T.of('a'))
* const fb = T.delay(10)(T.of('b'))
* assert.deepStrictEqual(await S.concat(fa, fb)(), 'b')
* }
*
* test()
*
* @category instances
* @since 2.0.0
*/
function getRaceMonoid() {
return {
concat: function (x, y) { return function () { return Promise.race([Promise.resolve().then(x), Promise.resolve().then(y)]); }; },
empty: exports.never
};
}
exports.getRaceMonoid = getRaceMonoid;
/**
* @category instances
* @since 2.7.0
*/
exports.Functor = {
URI: exports.URI,
map: _map
};
/**
* @category mapping
* @since 2.10.0
*/
exports.flap = (0, Functor_1.flap)(exports.Functor);
/**
* @category instances
* @since 2.10.0
*/
exports.Pointed = {
URI: exports.URI,
of: exports.of
};
/**
* Runs computations in parallel.
*
* @category instances
* @since 2.10.0
*/
exports.ApplyPar = {
URI: exports.URI,
map: _map,
ap: _apPar
};
/**
* Combine two effectful actions, keeping only the result of the first.
*
* @since 2.0.0
*/
exports.apFirst = (0, Apply_1.apFirst)(exports.ApplyPar);
/**
* Combine two effectful actions, keeping only the result of the second.
*
* @since 2.0.0
*/
exports.apSecond = (0, Apply_1.apSecond)(exports.ApplyPar);
/**
* Runs computations in parallel.
*
* @category instances
* @since 2.7.0
*/
exports.ApplicativePar = {
URI: exports.URI,
map: _map,
ap: _apPar,
of: exports.of
};
/**
* Runs computations sequentially.
*
* @category instances
* @since 2.10.0
*/
exports.ApplySeq = {
URI: exports.URI,
map: _map,
ap: _apSeq
};
/**
* Runs computations sequentially.
*
* @category instances
* @since 2.7.0
*/
exports.ApplicativeSeq = {
URI: exports.URI,
map: _map,
ap: _apSeq,
of: exports.of
};
/**
* @category instances
* @since 2.10.0
*/
exports.Chain = {
URI: exports.URI,
map: _map,
ap: _apPar,
chain: _chain
};
/**
* @category instances
* @since 2.10.0
*/
exports.Monad = {
URI: exports.URI,
map: _map,
of: exports.of,
ap: _apPar,
chain: _chain
};
/**
* @category instances
* @since 2.10.0
*/
exports.MonadIO = {
URI: exports.URI,
map: _map,
of: exports.of,
ap: _apPar,
chain: _chain,
fromIO: exports.fromIO
};
/**
* @category zone of death
* @since 2.7.0
* @deprecated
*/
exports.fromTask = function_1.identity;
/**
* @category instances
* @since 2.10.0
*/
exports.MonadTask = {
URI: exports.URI,
map: _map,
of: exports.of,
ap: _apPar,
chain: _chain,
fromIO: exports.fromIO,
fromTask: exports.fromTask
};
/**
* Composes computations in sequence, using the return value of one computation to determine the next computation and
* keeping only the result of the first.
*
* @category sequencing
* @since 2.0.0
*/
exports.chainFirst = (0, Chain_1.chainFirst)(exports.Chain);
/**
* @category instances
* @since 2.10.0
*/
exports.FromIO = {
URI: exports.URI,
fromIO: exports.fromIO
};
/**
* @category lifting
* @since 2.4.0
*/
exports.fromIOK =
/*#__PURE__*/ (0, FromIO_1.fromIOK)(exports.FromIO);
/**
* @category sequencing
* @since 2.4.0
*/
exports.chainIOK = (0, FromIO_1.chainIOK)(exports.FromIO, exports.Chain);
/**
* @category sequencing
* @since 2.10.0
*/
exports.chainFirstIOK = (0, FromIO_1.chainFirstIOK)(exports.FromIO, exports.Chain);
/**
* @category instances
* @since 2.10.0
*/
exports.FromTask = {
URI: exports.URI,
fromIO: exports.fromIO,
fromTask: exports.fromTask
};
// -------------------------------------------------------------------------------------
// utils
// -------------------------------------------------------------------------------------
/**
* A `Task` that never completes.
*
* @since 2.0.0
*/
var never = function () { return new Promise(function (_) { return undefined; }); };
exports.never = never;
// -------------------------------------------------------------------------------------
// do notation
// -------------------------------------------------------------------------------------
/**
* @category do notation
* @since 2.9.0
*/
exports.Do = (0, exports.of)(_.emptyRecord);
/**
* @category do notation
* @since 2.8.0
*/
exports.bindTo = (0, Functor_1.bindTo)(exports.Functor);
var let_ = /*#__PURE__*/ (0, Functor_1.let)(exports.Functor);
exports.let = let_;
/**
* @category do notation
* @since 2.8.0
*/
exports.bind = (0, Chain_1.bind)(exports.Chain);
/**
* @category do notation
* @since 2.8.0
*/
exports.apS = (0, Apply_1.apS)(exports.ApplyPar);
/**
* @since 2.11.0
*/
exports.ApT = (0, exports.of)(_.emptyReadonlyArray);
// -------------------------------------------------------------------------------------
// array utils
// -------------------------------------------------------------------------------------
/**
* Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativePar)`.
*
* @category traversing
* @since 2.11.0
*/
var traverseReadonlyNonEmptyArrayWithIndex = function (f) {
return function (as) {
return function () {
return Promise.all(as.map(function (a, i) { return Promise.resolve().then(function () { return f(i, a)(); }); }));
};
};
};
exports.traverseReadonlyNonEmptyArrayWithIndex = traverseReadonlyNonEmptyArrayWithIndex;
/**
* Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativePar)`.
*
* @category traversing
* @since 2.11.0
*/
var traverseReadonlyArrayWithIndex = function (f) {
var g = (0, exports.traverseReadonlyNonEmptyArrayWithIndex)(f);
return function (as) { return (_.isNonEmpty(as) ? g(as) : exports.ApT); };
};
exports.traverseReadonlyArrayWithIndex = traverseReadonlyArrayWithIndex;
/**
* Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(ApplicativeSeq)`.
*
* @category traversing
* @since 2.11.0
*/
var traverseReadonlyNonEmptyArrayWithIndexSeq = function (f) {
return function (as) {
return function () {
return _.tail(as).reduce(function (acc, a, i) {
return acc.then(function (bs) {
return Promise.resolve()
.then(f(i + 1, a))
.then(function (b) {
bs.push(b);
return bs;
});
});
}, Promise.resolve()
.then(f(0, _.head(as)))
.then(_.singleton));
};
};
};
exports.traverseReadonlyNonEmptyArrayWithIndexSeq = traverseReadonlyNonEmptyArrayWithIndexSeq;
/**
* Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
*
* @category traversing
* @since 2.11.0
*/
var traverseReadonlyArrayWithIndexSeq = function (f) {
var g = (0, exports.traverseReadonlyNonEmptyArrayWithIndexSeq)(f);
return function (as) { return (_.isNonEmpty(as) ? g(as) : exports.ApT); };
};
exports.traverseReadonlyArrayWithIndexSeq = traverseReadonlyArrayWithIndexSeq;
/**
* Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
*
* @category traversing
* @since 2.9.0
*/
exports.traverseArrayWithIndex = exports.traverseReadonlyArrayWithIndex;
/**
* Equivalent to `ReadonlyArray#traverse(Applicative)`.
*
* @category traversing
* @since 2.9.0
*/
var traverseArray = function (f) {
return (0, exports.traverseReadonlyArrayWithIndex)(function (_, a) { return f(a); });
};
exports.traverseArray = traverseArray;
/**
* Equivalent to `ReadonlyArray#sequence(Applicative)`.
*
* @category traversing
* @since 2.9.0
*/
exports.sequenceArray =
/*#__PURE__*/ (0, exports.traverseArray)(function_1.identity);
/**
* Equivalent to `ReadonlyArray#traverseWithIndex(ApplicativeSeq)`.
*
* @category traversing
* @since 2.9.0
*/
exports.traverseSeqArrayWithIndex = exports.traverseReadonlyArrayWithIndexSeq;
/**
* Equivalent to `ReadonlyArray#traverse(ApplicativeSeq)`.
*
* @category traversing
* @since 2.9.0
*/
var traverseSeqArray = function (f) {
return (0, exports.traverseReadonlyArrayWithIndexSeq)(function (_, a) { return f(a); });
};
exports.traverseSeqArray = traverseSeqArray;
/**
* Equivalent to `ReadonlyArray#sequence(ApplicativeSeq)`.
*
* @category traversing
* @since 2.9.0
*/
exports.sequenceSeqArray =
/*#__PURE__*/ (0, exports.traverseSeqArray)(function_1.identity);
// -------------------------------------------------------------------------------------
// deprecated
// -------------------------------------------------------------------------------------
/**
* This instance is deprecated, use small, specific instances instead.
* For example if a function needs a `Functor` instance, pass `T.Functor` instead of `T.task`
* (where `T` is from `import T from 'fp-ts/Task'`)
*
* @category zone of death
* @since 2.0.0
* @deprecated
*/
exports.task = {
URI: exports.URI,
map: _map,
of: exports.of,
ap: _apPar,
chain: _chain,
fromIO: exports.fromIO,
fromTask: exports.fromTask
};
/**
* This instance is deprecated, use small, specific instances instead.
* For example if a function needs a `Functor` instance, pass `T.Functor` instead of `T.taskSeq`
* (where `T` is from `import T from 'fp-ts/Task'`)
*
* @category zone of death
* @since 2.0.0
* @deprecated
*/
exports.taskSeq = {
URI: exports.URI,
map: _map,
of: exports.of,
ap: _apSeq,
chain: _chain,
fromIO: exports.fromIO,
fromTask: exports.fromTask
};
/**
* Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
*
* @category zone of death
* @since 2.0.0
* @deprecated
*/
exports.getSemigroup = (0, Apply_1.getApplySemigroup)(exports.ApplySeq);
/**
* Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
*
* Lift a monoid into 'Task', the inner values are concatenated using the provided `Monoid`.
*
* @category zone of death
* @since 2.0.0
* @deprecated
*/
exports.getMonoid = (0, Applicative_1.getApplicativeMonoid)(exports.ApplicativeSeq);