fp-ts-contrib
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -16,2 +16,12 @@ # Changelog | ||
# 0.0.6 | ||
- **New Feature** | ||
- add `Task/withTimeout` (@gabro) | ||
- add `TaskEither/withTimeout` (@gabro) | ||
- add `TaskOption.withTimeout` (@gabro) | ||
- add `TaskValidation.withTimeout` (@gabro) | ||
- **Bug Fix** | ||
- revert 55fe295e5d984a6b54c72005277733fd893f2234 (@gcanti) | ||
# 0.0.5 | ||
@@ -18,0 +28,0 @@ |
105
lib/Do.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Apply_1 = require("fp-ts/lib/Apply"); | ||
var nil = { type: 'Nil', length: 0 }; | ||
var cons = function (head, tail) { return ({ | ||
type: 'Cons', | ||
head: head, | ||
tail: tail, | ||
length: tail.length + 1 | ||
}); }; | ||
var toArray = function (list) { | ||
var len = list.length; | ||
var r = new Array(len); | ||
var l = list; | ||
var i = 1; | ||
while (l.type !== 'Nil') { | ||
r[len - i] = l.head; | ||
i++; | ||
l = l.tail; | ||
} | ||
return r; | ||
}; | ||
var DoClass = /** @class */ (function () { | ||
function DoClass(M, actions) { | ||
function DoClass(M, result) { | ||
this.M = M; | ||
this.actions = actions; | ||
this._sequenceS = undefined; | ||
this.result = result; | ||
} | ||
DoClass.prototype.do = function (action) { | ||
return new DoClass(this.M, cons({ type: 'do', action: action }, this.actions)); | ||
var _this = this; | ||
return new DoClass(this.M, this.M.chain(this.result, function (s) { return _this.M.map(action, function () { return s; }); })); | ||
}; | ||
DoClass.prototype.doL = function (f) { | ||
return new DoClass(this.M, cons({ type: 'doL', f: f }, this.actions)); | ||
var _this = this; | ||
return new DoClass(this.M, this.M.chain(this.result, function (s) { return _this.M.map(f(s), function () { return s; }); })); | ||
}; | ||
DoClass.prototype.bind = function (name, action) { | ||
return new DoClass(this.M, cons({ type: 'bind', name: name, action: action }, this.actions)); | ||
var _this = this; | ||
return new DoClass(this.M, this.M.chain(this.result, function (s) { | ||
return _this.M.map(action, function (b) { | ||
var s2 = Object.assign({}, s); | ||
s2[name] = b; | ||
return s2; | ||
}); | ||
})); | ||
}; | ||
DoClass.prototype.bindL = function (name, f) { | ||
return new DoClass(this.M, cons({ type: 'bindL', name: name, f: f }, this.actions)); | ||
var _this = this; | ||
return new DoClass(this.M, this.M.chain(this.result, function (s) { | ||
return _this.M.map(f(s), function (b) { | ||
var s2 = Object.assign({}, s); | ||
s2[name] = b; | ||
return s2; | ||
}); | ||
})); | ||
}; | ||
DoClass.prototype.sequenceS = function (r) { | ||
return new DoClass(this.M, cons({ type: 'sequenceS', r: r }, this.actions)); | ||
var _this = this; | ||
return new DoClass(this.M, this.M.chain(this.result, function (s) { return _this.M.map(Apply_1.sequenceS(_this.M)(r), function (r) { return Object.assign({}, s, r); }); })); | ||
}; | ||
DoClass.prototype.sequenceSL = function (f) { | ||
return new DoClass(this.M, cons({ type: 'sequenceSL', f: f }, this.actions)); | ||
var _this = this; | ||
return new DoClass(this.M, this.M.chain(this.result, function (s) { return _this.M.map(Apply_1.sequenceS(_this.M)(f(s)), function (r) { return Object.assign({}, s, r); }); })); | ||
}; | ||
DoClass.prototype.return = function (f) { | ||
return this.M.map(this.done(), f); | ||
return this.M.map(this.result, f); | ||
}; | ||
DoClass.prototype.done = function () { | ||
var actions = this.actions; | ||
var len = actions.length; | ||
var as = toArray(actions); | ||
var result = this.M.of({}); | ||
var M = this.M; | ||
var _loop_1 = function (i) { | ||
var a = as[i]; | ||
switch (a.type) { | ||
case 'do': | ||
result = M.chain(result, function (s) { return M.map(a.action, function () { return s; }); }); | ||
break; | ||
case 'doL': | ||
result = M.chain(result, function (s) { return M.map(a.f(s), function () { return s; }); }); | ||
break; | ||
case 'bind': | ||
result = M.chain(result, function (s) { | ||
return M.map(a.action, function (b) { | ||
s[a.name] = b; | ||
return s; | ||
}); | ||
}); | ||
break; | ||
case 'bindL': | ||
result = M.chain(result, function (s) { | ||
return M.map(a.f(s), function (b) { | ||
s[a.name] = b; | ||
return s; | ||
}); | ||
}); | ||
break; | ||
case 'sequenceS': | ||
var _sequenceS_1 = this_1._sequenceS === undefined ? (this_1._sequenceS = Apply_1.sequenceS(M)) : this_1._sequenceS; | ||
result = M.chain(result, function (s) { return M.map(_sequenceS_1(a.r), function (r) { return Object.assign(s, r); }); }); | ||
break; | ||
case 'sequenceSL': | ||
var _sequenceSL_1 = this_1._sequenceS === undefined ? (this_1._sequenceS = Apply_1.sequenceS(M)) : this_1._sequenceS; | ||
result = M.chain(result, function (s) { return M.map(_sequenceSL_1(a.f(s)), function (r) { return Object.assign(s, r); }); }); | ||
break; | ||
} | ||
}; | ||
var this_1 = this; | ||
for (var i = 0; i < len; i++) { | ||
_loop_1(i); | ||
} | ||
return result; | ||
return this.result; | ||
}; | ||
return DoClass; | ||
}()); | ||
var init = {}; | ||
function Do(M) { | ||
return new DoClass(M, nil); | ||
return new DoClass(M, M.of(init)); | ||
} | ||
exports.Do = Do; |
@@ -30,2 +30,19 @@ import { Monad1 } from 'fp-ts/lib/Monad'; | ||
export declare const tryCatch: <A>(f: Lazy<Promise<A>>) => TaskOption<A>; | ||
/** | ||
* Returns the `TaskOption` result if it completes within a timeout, or a fallback value instead. | ||
* | ||
* @example | ||
* import { TaskOption, withTimeout } from 'fp-ts-contrib/lib/TaskOption' | ||
* import { delay } from 'fp-ts/lib/Task' | ||
* import { some, none } from 'fp-ts/lib/Option' | ||
* | ||
* const completeAfter2s = new TaskOption(delay(2000, some('result'))) | ||
* | ||
* withTimeout(completeAfter2s, some('timeout'), 3000).run() // Promise(some('result')) | ||
* withTimeout(completeAfter2s, none, 1000).run() // Promise(none) | ||
* withTimeout(completeAfter2s, some('timeout'), 1000).run() // Promise(some('timeout')) | ||
* | ||
* @since 0.0.6 | ||
*/ | ||
export declare const withTimeout: <A>(fa: TaskOption<A>, onTimeout: Option<A>, millis: number) => TaskOption<A>; | ||
export declare const taskOption: Monad1<URI>; |
@@ -7,2 +7,3 @@ "use strict"; | ||
var function_1 = require("fp-ts/lib/function"); | ||
var withTimeout_1 = require("./Task/withTimeout"); | ||
exports.URI = 'TaskOption'; | ||
@@ -50,2 +51,21 @@ var T = optionT.getOptionT2v(Task_1.task); | ||
}; | ||
/** | ||
* Returns the `TaskOption` result if it completes within a timeout, or a fallback value instead. | ||
* | ||
* @example | ||
* import { TaskOption, withTimeout } from 'fp-ts-contrib/lib/TaskOption' | ||
* import { delay } from 'fp-ts/lib/Task' | ||
* import { some, none } from 'fp-ts/lib/Option' | ||
* | ||
* const completeAfter2s = new TaskOption(delay(2000, some('result'))) | ||
* | ||
* withTimeout(completeAfter2s, some('timeout'), 3000).run() // Promise(some('result')) | ||
* withTimeout(completeAfter2s, none, 1000).run() // Promise(none) | ||
* withTimeout(completeAfter2s, some('timeout'), 1000).run() // Promise(some('timeout')) | ||
* | ||
* @since 0.0.6 | ||
*/ | ||
exports.withTimeout = function (fa, onTimeout, millis) { | ||
return new TaskOption(withTimeout_1.withTimeout(fa.value, onTimeout, millis)); | ||
}; | ||
exports.taskOption = { | ||
@@ -52,0 +72,0 @@ URI: exports.URI, |
@@ -22,3 +22,20 @@ import { Applicative2C } from 'fp-ts/lib/Applicative'; | ||
} | ||
/** | ||
* Returns the `TaskValidation` result if it completes within a timeout, or a fallback value instead. | ||
* | ||
* @example | ||
* import { TaskValidation, withTimeout } from 'fp-ts-contrib/lib/TaskValidation' | ||
* import { delay } from 'fp-ts/lib/Task' | ||
* import { success, failure } from 'fp-ts/lib/Validation' | ||
* | ||
* const completeAfter2s = new TaskValidation(delay(2000, success('result'))) | ||
* | ||
* withTimeout(completeAfter2s, failure('timeout'), 3000).value.run() // Promise(success('result')) | ||
* withTimeout(completeAfter2s, failure('timeout'), 1000).value.run() // Promise(failure('timeout')) | ||
* withTimeout(completeAfter2s, success('timeout'), 1000).value.run() // Promise(success('timeout')) | ||
* | ||
* @since 0.0.6 | ||
*/ | ||
export declare const withTimeout: <L, A>(fa: TaskValidation<L, A>, onTimeout: validation.Validation<L, A>, millis: number) => TaskValidation<L, A>; | ||
export declare const getApplicative: <L>(S: Semigroup<L>) => Applicative2C<"TaskValidation", L>; | ||
export declare const taskValidation: Functor2<URI>; |
@@ -8,2 +8,3 @@ "use strict"; | ||
var function_1 = require("fp-ts/lib/function"); | ||
var withTimeout_1 = require("./Task/withTimeout"); | ||
var taskValidationFunctor = Functor_1.getFunctorComposition(task.task, validation.validation); | ||
@@ -27,2 +28,21 @@ exports.URI = 'TaskValidation'; | ||
}; | ||
/** | ||
* Returns the `TaskValidation` result if it completes within a timeout, or a fallback value instead. | ||
* | ||
* @example | ||
* import { TaskValidation, withTimeout } from 'fp-ts-contrib/lib/TaskValidation' | ||
* import { delay } from 'fp-ts/lib/Task' | ||
* import { success, failure } from 'fp-ts/lib/Validation' | ||
* | ||
* const completeAfter2s = new TaskValidation(delay(2000, success('result'))) | ||
* | ||
* withTimeout(completeAfter2s, failure('timeout'), 3000).value.run() // Promise(success('result')) | ||
* withTimeout(completeAfter2s, failure('timeout'), 1000).value.run() // Promise(failure('timeout')) | ||
* withTimeout(completeAfter2s, success('timeout'), 1000).value.run() // Promise(success('timeout')) | ||
* | ||
* @since 0.0.6 | ||
*/ | ||
exports.withTimeout = function (fa, onTimeout, millis) { | ||
return new TaskValidation(withTimeout_1.withTimeout(fa.value, onTimeout, millis)); | ||
}; | ||
exports.getApplicative = function (S) { | ||
@@ -29,0 +49,0 @@ var taskValidationApplicative = Applicative_1.getApplicativeComposition(task.task, validation.getApplicative(S)); |
{ | ||
"name": "fp-ts-contrib", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "A community driven utility package for fp-ts", | ||
@@ -5,0 +5,0 @@ "files": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
81967
48
1891