t-tasks 1.2.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/t-tasks.js CHANGED
@@ -1,2 +1,814 @@
1
- function t(){t=function(){return r};var e,r={},n=Object.prototype,o=n.hasOwnProperty,i=Object.defineProperty||function(t,e,r){t[e]=r.value},a="function"==typeof Symbol?Symbol:{},s=a.iterator||"@@iterator",u=a.asyncIterator||"@@asyncIterator",c=a.toStringTag||"@@toStringTag";function h(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{h({},"")}catch(e){h=function(t,e,r){return t[e]=r}}function p(t,e,r,n){var o=Object.create((e&&e.prototype instanceof v?e:v).prototype),a=new C(n||[]);return i(o,"_invoke",{value:L(t,r,a)}),o}function f(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}r.wrap=p;var l="suspendedStart",x="suspendedYield",g="executing",m="completed",y={};function v(){}function d(){}function b(){}var j={};h(j,s,function(){return this});var M=Object.getPrototypeOf,w=M&&M(M(O([])));w&&w!==n&&o.call(w,s)&&(j=w);var E=b.prototype=v.prototype=Object.create(j);function T(t){["next","throw","return"].forEach(function(e){h(t,e,function(t){return this._invoke(e,t)})})}function k(t,e){function r(n,i,a,s){var u=f(t[n],t,i);if("throw"!==u.type){var c=u.arg,h=c.value;return h&&"object"==typeof h&&o.call(h,"__await")?e.resolve(h.__await).then(function(t){r("next",t,a,s)},function(t){r("throw",t,a,s)}):e.resolve(h).then(function(t){c.value=t,a(c)},function(t){return r("throw",t,a,s)})}s(u.arg)}var n;i(this,"_invoke",{value:function(t,o){function i(){return new e(function(e,n){r(t,o,e,n)})}return n=n?n.then(i,i):i()}})}function L(t,r,n){var o=l;return function(i,a){if(o===g)throw new Error("Generator is already running");if(o===m){if("throw"===i)throw a;return{value:e,done:!0}}for(n.method=i,n.arg=a;;){var s=n.delegate;if(s){var u=_(s,n);if(u){if(u===y)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===l)throw o=m,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=g;var c=f(t,r,n);if("normal"===c.type){if(o=n.done?m:x,c.arg===y)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=m,n.method="throw",n.arg=c.arg)}}}function _(t,r){var n=r.method,o=t.iterator[n];if(o===e)return r.delegate=null,"throw"===n&&t.iterator.return&&(r.method="return",r.arg=e,_(t,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var i=f(o,t.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,y;var a=i.arg;return a?a.done?(r[t.resultName]=a.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,y):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function P(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function N(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function C(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(P,this),this.reset(!0)}function O(t){if(t||""===t){var r=t[s];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function r(){for(;++n<t.length;)if(o.call(t,n))return r.value=t[n],r.done=!1,r;return r.value=e,r.done=!0,r};return i.next=i}}throw new TypeError(typeof t+" is not iterable")}return d.prototype=b,i(E,"constructor",{value:b,configurable:!0}),i(b,"constructor",{value:d,configurable:!0}),d.displayName=h(b,c,"GeneratorFunction"),r.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===d||"GeneratorFunction"===(e.displayName||e.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,b):(t.__proto__=b,h(t,c,"GeneratorFunction")),t.prototype=Object.create(E),t},r.awrap=function(t){return{__await:t}},T(k.prototype),h(k.prototype,u,function(){return this}),r.AsyncIterator=k,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new k(p(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then(function(t){return t.done?t.value:a.next()})},T(E),h(E,c,"Generator"),h(E,s,function(){return this}),h(E,"toString",function(){return"[object Generator]"}),r.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=O,C.prototype={constructor:C,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(N),!t)for(var r in this)"t"===r.charAt(0)&&o.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function n(n,o){return s.type="throw",s.arg=t,r.next=n,o&&(r.method="next",r.arg=e),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],s=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=o.call(a,"catchLoc"),c=o.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&o.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),N(r),y}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;N(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:O(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),y}},r}var e;exports.Just=void 0,(exports.Just||(exports.Just={})).just=function(t){return t.just},exports.Maybe=void 0,(e=exports.Maybe||(exports.Maybe={})).just=function(t){return new r(t)},e.nothing=function(){return n},e.fromOptional=function(t){return void 0!==t?exports.Maybe.just(t):exports.Maybe.nothing()},e.fromNullable=function(t){return exports.Maybe.fromOptional(t).chain(t=>null!==t?exports.Maybe.just(t):exports.Maybe.nothing())},e.isJust=function(t){return t.isJust()},e.isNothing=function(t){return t.isNothing()},e.everyJust=function(t){return t.every(exports.Maybe.isJust)},e.someJust=function(t){return t.some(exports.Maybe.isJust)},e.everyNothing=function(t){return t.every(exports.Maybe.isNothing)},e.someNothing=function(t){return t.some(exports.Maybe.isNothing)},e.generate=function(t){const e=t(),r=t=>t.done?exports.Maybe.just(t.value):t.value.matchChain({just:t=>r(e.next(t)),nothing:exports.Maybe.nothing});return exports.Maybe.just(void 0).chain(()=>r(e.next()))};class r{constructor(t){this.just=t}tap(t){return t(this.just),this}orTap(){return this}map(t){return exports.Maybe.just(t(this.just))}orMap(){return this}chain(t){return t(this.just)}orChain(){return this}isJust(){return!0}isNothing(){return!1}matchTap(t){return this.tap(t.just)}matchMap(t){return this.map(t.just)}matchChain(t){return this.chain(t.just)}generator(){/*#__PURE__*/return t().mark(function e(r){return t().wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r;case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},e)})(this)}}const n=new class{tap(){return this}orTap(t){return t(),this}map(){return this}orMap(t){return exports.Maybe.just(t())}chain(){return this}orChain(t){return t()}isJust(){return!1}isNothing(){return!0}matchTap(t){return this.orTap(t.nothing)}matchMap(t){return this.orMap(t.nothing)}matchChain(t){return this.orChain(t.nothing)}generator(){/*#__PURE__*/return t().mark(function e(r){return t().wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r;case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},e)})(this)}};var o;exports.Right=void 0,(exports.Right||(exports.Right={})).right=function(t){return t.right},exports.Left=void 0,(exports.Left||(exports.Left={})).left=function(t){return t.left},exports.Either=void 0,(o=exports.Either||(exports.Either={})).right=function(t){return new i(t)},o.left=function(t){return new a(t)},o.fromOptional=function(t,e){return void 0!==t?exports.Either.right(t):exports.Either.left(e)},o.fromNullable=function(t,e){return exports.Either.fromOptional(t,e).chain(r=>null!==r?exports.Either.right(t):exports.Either.left(e))},o.isRight=function(t){return t.isRight()},o.isLeft=function(t){return t.isLeft()},o.everyRight=function(t){return t.every(exports.Either.isRight)},o.someRight=function(t){return t.some(exports.Either.isRight)},o.everyLeft=function(t){return t.every(exports.Either.isLeft)},o.someLeft=function(t){return t.some(exports.Either.isLeft)},o.generate=function(t){const e=t(),r=t=>t.done?exports.Either.right(t.value):t.value.matchChain({right:t=>r(e.next(t)),left:exports.Either.left});return exports.Either.right(void 0).chain(()=>r(e.next()))};class i{constructor(t){this.right=t}tap(t){return t(this.right),this}orTap(){return this}map(t){return exports.Either.right(t(this.right))}orMap(){return this}chain(t){return t(this.right)}orChain(){return this}isRight(){return!0}isLeft(){return!1}matchTap(t){return this.tap(t.right)}matchMap(t){return this.map(t.right)}matchChain(t){return this.chain(t.right)}generator(){/*#__PURE__*/return t().mark(function e(r){return t().wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r;case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},e)})(this)}}class a{constructor(t){this.left=t}tap(){return this}orTap(t){return t(this.left),this}map(){return this}orMap(t){return exports.Either.right(t(this.left))}chain(){return this}orChain(t){return t(this.left)}isRight(){return!1}isLeft(){return!0}matchTap(t){return this.orTap(t.left)}matchMap(t){return this.orMap(t.left)}matchChain(t){return this.orChain(t.left)}generator(){/*#__PURE__*/return t().mark(function e(r){return t().wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r;case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},e)})(this)}}exports.Task=void 0,function(e){function r(t){const e=t(),r=t=>t.done?exports.Task.resolved(t.value):t.value.matchChain({resolved:t=>r(e.next(t)),rejected:t=>r(e.throw(t)),canceled:exports.Task.canceled});return exports.Task.resolved(void 0).chain(()=>r(e.next()))}function n(t,e){let r;const[n,o]=s();return exports.Task.create(new Promise(e=>{o(e),r=t(t=>n(exports.Maybe.just(exports.Either.right(t))),t=>n(exports.Maybe.just(exports.Either.left(t))),()=>n(exports.Maybe.nothing()))}),t=>{n(t.map(exports.Either.left)),e(r)})}e.create=function(t,e){return new x(t,e)},e.resolved=function(t){return exports.Task.create(Promise.resolve(exports.Maybe.just(exports.Either.right(t))),()=>{})},e.rejected=function(t){return exports.Task.create(Promise.resolve(exports.Maybe.just(exports.Either.left(t))),()=>{})},e.canceled=function(){return exports.Task.create(Promise.resolve(exports.Maybe.nothing()),()=>{})},e.fromPromise=function(t){const[e,r]=s();return exports.Task.create(new Promise(n=>{r(n),t.then(exports.Either.right,exports.Either.left).then(exports.Maybe.just).then(e)}),t=>e(t.map(exports.Either.left)))},e.fromFunction=function(t,e){const[r,n]=s(),o=t=>r(t.map(exports.Either.left));return e.cancel=o,exports.Task.create(new Promise(e=>{n(e);try{r(exports.Maybe.just(exports.Either.right(t())))}catch(t){r(exports.Maybe.just(exports.Either.left(t)))}}),o)},e.promiseGenerator=function(t){return exports.Task.fromPromise(t).generator()},e.timeoutGenerator=function(t){return exports.Task.timeout(t).generator()},e.lift=function(t){return function(){return exports.Task.fromPromise(t(...[].slice.call(arguments)))}},e.generate=r,e.generateFunction=function(e){return function(){var n=[].slice.call(arguments);return r(/*#__PURE__*/t().mark(function r(){return t().wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.delegateYield(e(...n),"t0",1);case 1:return t.abrupt("return",t.t0);case 2:case"end":return t.stop()}},r)}))}},e.timeout=function(t){return n(e=>setTimeout(()=>e(),t),clearTimeout)},e.fromCallback=n,e.sequence=function(t){return Array.from(t).reduce((t,e)=>t.chain(t=>e().map(e=>t.concat(e))),exports.Task.resolved([]))},e.all=function(t){const[e,r]=s(),n=Array.from(t),o=t=>n.forEach(e=>e._cancel(t)),i=n.map(exports.Maybe.nothing),a=t=>{e(t.map(exports.Either.left)),o(t)};return exports.Task.create(new Promise(t=>{if(r(t),n.length<1)return e(exports.Maybe.just(exports.Either.right([])));n.map((t,r)=>t.matchTap({resolved:t=>((t,r)=>{i[r]=exports.Maybe.just(t),exports.Maybe.everyJust(i)&&e(exports.Maybe.just(exports.Either.right(i.map(exports.Just.just))))})(t,r),rejected:t=>a(exports.Maybe.just(t)),canceled:()=>a(exports.Maybe.nothing())}))}),o)},e.any=function(t){const[e,r]=s(),n=Array.from(t),o=t=>n.forEach(e=>e._cancel(t)),i=n.map(exports.Maybe.nothing),a=t=>{e(t.map(exports.Either.right)),o(exports.Maybe.nothing())};return exports.Task.create(new Promise(t=>{if(r(t),n.length<1)return e(exports.Maybe.nothing());n.map((t,r)=>t.matchTap({resolved:t=>a(exports.Maybe.just(t)),rejected:t=>((t,r)=>{i[r]=exports.Maybe.just(t),exports.Maybe.everyJust(i)&&e(exports.Maybe.just(exports.Either.left(i.map(exports.Just.just))))})(t,r),canceled:()=>a(exports.Maybe.nothing())}))}),o)},e.limit=function(t,e){return exports.Task.any([t,e.chain(exports.Task.canceled)]).chainRejected(t=>exports.Task.rejected(t[0]))},e.repeat=function(t){const e=()=>t().chainRejected(e);return e()}}(exports.Task||(exports.Task={}));const s=()=>{const t=()=>{};let e=t;return[r=>{const n=e;e=t,n(r)},t=>{e=t}]};function u(t,e){const r={cancel:t._cancel};return exports.Task.create(t._invoke.then(function(t){try{return Promise.resolve(function(t,e){try{var r=t()}catch(t){return e(t)}return r&&r.then?r.then(void 0,e):r}(function(){const{_invoke:n,_cancel:o}=exports.Task.fromFunction(()=>e(t),r);return r.cancel=o,Promise.resolve(n).then(function(t){if(t.isNothing())return exports.Maybe.nothing();if(t.just.isLeft())return exports.Maybe.just(exports.Either.left(t.just.left));const{_invoke:e,_cancel:n}=t.just.right;return r.cancel=n,e})},function(t){return exports.Maybe.just(exports.Either.left(t))}))}catch(t){return Promise.reject(t)}}),t=>r.cancel(t))}function c(t,e){return u(t,t=>t.map(e).orMap(exports.Task.canceled).just)}function h(t,e){return u(t,t=>e(t).matchMap({just:t=>t.matchMap({right:exports.Task.resolved,left:exports.Task.rejected}).right,nothing:exports.Task.canceled}).just)}function p(t,e){return h(t,t=>t.map(e))}function f(t,e){return h(t,t=>(e(t),t))}function l(t,e){return f(t,t=>t.tap(e))}class x{constructor(t,e){this._invoke=t,this._cancel=e}tapCanceled(t){return f(this,e=>e.orTap(t))}tapRejected(t){return l(this,e=>e.orTap(t))}tap(t){return l(this,e=>e.tap(t))}mapCanceled(t){return h(this,e=>e.orMap(()=>exports.Either.right(t())))}mapRejected(t){return p(this,e=>e.orMap(t))}map(t){return p(this,e=>e.map(t))}chainCanceled(t){const e=t=>t.map(exports.Task.resolved).orMap(exports.Task.rejected).right;return u(this,r=>r.map(e).orMap(t).just)}chainRejected(t){return c(this,e=>e.map(exports.Task.resolved).orMap(t).right)}chain(t){return c(this,e=>e.map(t).orMap(exports.Task.rejected).right)}resolve(){return this._invoke}cancel(){return this._cancel(exports.Maybe.nothing())}reject(t){return this._cancel(exports.Maybe.just(t))}generator(){/*#__PURE__*/return t().mark(function e(r){return t().wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r;case 2:return t.abrupt("return",t.sent);case 3:case"end":return t.stop()}},e)})(this)}matchTap(t){return f(this,e=>e.matchTap({just:e=>e.matchTap({right:t.resolved,left:t.rejected}),nothing:t.canceled}))}matchMap(t){return h(this,e=>e.matchMap({just:e=>e.matchMap({right:t.resolved,left:t.rejected}),nothing:()=>exports.Either.right(t.canceled())}))}matchChain(t){return u(this,e=>e.matchMap({just:e=>e.matchMap({right:t.resolved,left:t.rejected}).right,nothing:t.canceled}).just)}}exports.Nothing=void 0;
2
- //# sourceMappingURL=t-tasks.js.map
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/maybe.ts
3
+ let Just;
4
+ (function(_Just) {
5
+ function just(just) {
6
+ return just.just;
7
+ }
8
+ _Just.just = just;
9
+ })(Just || (Just = {}));
10
+ let Maybe;
11
+ (function(_Maybe) {
12
+ function just(value) {
13
+ return new JustClass(value);
14
+ }
15
+ _Maybe.just = just;
16
+ function nothing() {
17
+ return staticNothing;
18
+ }
19
+ _Maybe.nothing = nothing;
20
+ function fromOptional(value) {
21
+ return typeof value !== "undefined" ? Maybe.just(value) : Maybe.nothing();
22
+ }
23
+ _Maybe.fromOptional = fromOptional;
24
+ function fromNullable(value) {
25
+ return Maybe.fromOptional(value).chain((v) => v !== null ? Maybe.just(v) : Maybe.nothing());
26
+ }
27
+ _Maybe.fromNullable = fromNullable;
28
+ function fromMaybe(maybe, fallback) {
29
+ return maybe.match({
30
+ just: (value) => value,
31
+ nothing: () => fallback
32
+ });
33
+ }
34
+ _Maybe.fromMaybe = fromMaybe;
35
+ function isJust(maybe) {
36
+ return maybe.isJust();
37
+ }
38
+ _Maybe.isJust = isJust;
39
+ function isNothing(maybe) {
40
+ return maybe.isNothing();
41
+ }
42
+ _Maybe.isNothing = isNothing;
43
+ function everyJust(maybes) {
44
+ return maybes.every(Maybe.isJust);
45
+ }
46
+ _Maybe.everyJust = everyJust;
47
+ function someJust(maybes) {
48
+ return maybes.some(Maybe.isJust);
49
+ }
50
+ _Maybe.someJust = someJust;
51
+ function everyNothing(maybes) {
52
+ return maybes.every(Maybe.isNothing);
53
+ }
54
+ _Maybe.everyNothing = everyNothing;
55
+ function someNothing(maybes) {
56
+ return maybes.some(Maybe.isNothing);
57
+ }
58
+ _Maybe.someNothing = someNothing;
59
+ function generate(maybeGeneratorFunction) {
60
+ const generator = maybeGeneratorFunction();
61
+ let next = generator.next();
62
+ while (!next.done) {
63
+ if (next.value.isNothing()) return Maybe.nothing();
64
+ next = generator.next(next.value.just);
65
+ }
66
+ return Maybe.just(next.value);
67
+ }
68
+ _Maybe.generate = generate;
69
+ })(Maybe || (Maybe = {}));
70
+ var JustClass = class {
71
+ constructor(just) {
72
+ this.just = just;
73
+ }
74
+ tap(op) {
75
+ op(this.just);
76
+ return this;
77
+ }
78
+ orTap() {
79
+ return this;
80
+ }
81
+ map(op) {
82
+ return Maybe.just(op(this.just));
83
+ }
84
+ orMap() {
85
+ return this;
86
+ }
87
+ chain(op) {
88
+ return op(this.just);
89
+ }
90
+ orChain() {
91
+ return this;
92
+ }
93
+ isJust() {
94
+ return true;
95
+ }
96
+ isNothing() {
97
+ return false;
98
+ }
99
+ match(op) {
100
+ return op.just(this.just);
101
+ }
102
+ matchTap(op) {
103
+ return this.tap(op.just);
104
+ }
105
+ matchMap(op) {
106
+ return this.map(op.just);
107
+ }
108
+ matchChain(op) {
109
+ return this.chain(op.just);
110
+ }
111
+ generator() {
112
+ return (function* (value) {
113
+ return yield value;
114
+ })(this);
115
+ }
116
+ };
117
+ var NothingClass = class {
118
+ tap() {
119
+ return this;
120
+ }
121
+ orTap(op) {
122
+ op();
123
+ return this;
124
+ }
125
+ map() {
126
+ return this;
127
+ }
128
+ orMap(op) {
129
+ return Maybe.just(op());
130
+ }
131
+ chain() {
132
+ return this;
133
+ }
134
+ orChain(op) {
135
+ return op();
136
+ }
137
+ isJust() {
138
+ return false;
139
+ }
140
+ isNothing() {
141
+ return true;
142
+ }
143
+ match(op) {
144
+ return op.nothing();
145
+ }
146
+ matchTap(op) {
147
+ return this.orTap(op.nothing);
148
+ }
149
+ matchMap(op) {
150
+ return this.orMap(op.nothing);
151
+ }
152
+ matchChain(op) {
153
+ return this.orChain(op.nothing);
154
+ }
155
+ generator() {
156
+ return (function* (value) {
157
+ return yield value;
158
+ })(this);
159
+ }
160
+ };
161
+ const staticNothing = new NothingClass();
162
+ //#endregion
163
+ //#region src/either.ts
164
+ let Right;
165
+ (function(_Right) {
166
+ function right(right) {
167
+ return right.right;
168
+ }
169
+ _Right.right = right;
170
+ })(Right || (Right = {}));
171
+ let Left;
172
+ (function(_Left) {
173
+ function left(left) {
174
+ return left.left;
175
+ }
176
+ _Left.left = left;
177
+ })(Left || (Left = {}));
178
+ let Either;
179
+ (function(_Either) {
180
+ function right(value) {
181
+ return new RightClass(value);
182
+ }
183
+ _Either.right = right;
184
+ function left(error) {
185
+ return new LeftClass(error);
186
+ }
187
+ _Either.left = left;
188
+ function fromOptional(value, error) {
189
+ return typeof value !== "undefined" ? Either.right(value) : Either.left(error);
190
+ }
191
+ _Either.fromOptional = fromOptional;
192
+ function fromNullable(value, error) {
193
+ return Either.fromOptional(value, error).chain((v) => v !== null ? Either.right(value) : Either.left(error));
194
+ }
195
+ _Either.fromNullable = fromNullable;
196
+ function fromEither(either, fallback) {
197
+ return either.match({
198
+ right: (value) => value,
199
+ left: () => fallback
200
+ });
201
+ }
202
+ _Either.fromEither = fromEither;
203
+ function isRight(either) {
204
+ return either.isRight();
205
+ }
206
+ _Either.isRight = isRight;
207
+ function isLeft(either) {
208
+ return either.isLeft();
209
+ }
210
+ _Either.isLeft = isLeft;
211
+ function everyRight(eithers) {
212
+ return eithers.every(Either.isRight);
213
+ }
214
+ _Either.everyRight = everyRight;
215
+ function someRight(eithers) {
216
+ return eithers.some(Either.isRight);
217
+ }
218
+ _Either.someRight = someRight;
219
+ function everyLeft(eithers) {
220
+ return eithers.every(Either.isLeft);
221
+ }
222
+ _Either.everyLeft = everyLeft;
223
+ function someLeft(eithers) {
224
+ return eithers.some(Either.isLeft);
225
+ }
226
+ _Either.someLeft = someLeft;
227
+ function generate(eitherGeneratorFunction) {
228
+ const generator = eitherGeneratorFunction();
229
+ let next = generator.next();
230
+ while (!next.done) {
231
+ if (next.value.isLeft()) return Either.left(next.value.left);
232
+ next = generator.next(next.value.right);
233
+ }
234
+ return Either.right(next.value);
235
+ }
236
+ _Either.generate = generate;
237
+ })(Either || (Either = {}));
238
+ var RightClass = class {
239
+ constructor(right) {
240
+ this.right = right;
241
+ }
242
+ tap(op) {
243
+ op(this.right);
244
+ return this;
245
+ }
246
+ orTap() {
247
+ return this;
248
+ }
249
+ map(op) {
250
+ return Either.right(op(this.right));
251
+ }
252
+ orMap() {
253
+ return this;
254
+ }
255
+ chain(op) {
256
+ return op(this.right);
257
+ }
258
+ orChain() {
259
+ return this;
260
+ }
261
+ isRight() {
262
+ return true;
263
+ }
264
+ isLeft() {
265
+ return false;
266
+ }
267
+ match(op) {
268
+ return op.right(this.right);
269
+ }
270
+ matchTap(op) {
271
+ return this.tap(op.right);
272
+ }
273
+ matchMap(op) {
274
+ return this.map(op.right);
275
+ }
276
+ matchChain(op) {
277
+ return this.chain(op.right);
278
+ }
279
+ generator() {
280
+ return (function* (value) {
281
+ return yield value;
282
+ })(this);
283
+ }
284
+ };
285
+ var LeftClass = class {
286
+ constructor(left) {
287
+ this.left = left;
288
+ }
289
+ tap() {
290
+ return this;
291
+ }
292
+ orTap(op) {
293
+ op(this.left);
294
+ return this;
295
+ }
296
+ map() {
297
+ return this;
298
+ }
299
+ orMap(op) {
300
+ return Either.right(op(this.left));
301
+ }
302
+ chain() {
303
+ return this;
304
+ }
305
+ orChain(op) {
306
+ return op(this.left);
307
+ }
308
+ isRight() {
309
+ return false;
310
+ }
311
+ isLeft() {
312
+ return true;
313
+ }
314
+ match(op) {
315
+ return op.left(this.left);
316
+ }
317
+ matchTap(op) {
318
+ return this.orTap(op.left);
319
+ }
320
+ matchMap(op) {
321
+ return this.orMap(op.left);
322
+ }
323
+ matchChain(op) {
324
+ return this.orChain(op.left);
325
+ }
326
+ generator() {
327
+ return (function* (value) {
328
+ return yield value;
329
+ })(this);
330
+ }
331
+ };
332
+ //#endregion
333
+ //#region src/task.ts
334
+ let Task;
335
+ (function(_Task) {
336
+ function create(invoke, cancel) {
337
+ return new TaskClass({
338
+ resolved: false,
339
+ invoke,
340
+ cancel
341
+ });
342
+ }
343
+ _Task.create = create;
344
+ function resolved(value) {
345
+ return new TaskClass({
346
+ resolved: true,
347
+ result: Maybe.just(Either.right(value))
348
+ });
349
+ }
350
+ _Task.resolved = resolved;
351
+ function rejected(error) {
352
+ return new TaskClass({
353
+ resolved: true,
354
+ result: Maybe.just(Either.left(error))
355
+ });
356
+ }
357
+ _Task.rejected = rejected;
358
+ function canceled() {
359
+ return new TaskClass({
360
+ resolved: true,
361
+ result: Maybe.nothing()
362
+ });
363
+ }
364
+ _Task.canceled = canceled;
365
+ function fromPromise(promise) {
366
+ const cell = settleCell();
367
+ const cancel = (error) => cell.resolve(error.map(Either.left));
368
+ promise.then(Either.right, Either.left).then(Maybe.just).then(cell.resolve);
369
+ return Task.create(cell.promise, cancel);
370
+ }
371
+ _Task.fromPromise = fromPromise;
372
+ function fromFunction(producer, cancelRef) {
373
+ const cell = settleCell();
374
+ const cancel = (error) => cell.resolve(error.map(Either.left));
375
+ cancelRef.cancel = cancel;
376
+ try {
377
+ cell.resolve(Maybe.just(Either.right(producer())));
378
+ } catch (e) {
379
+ cell.resolve(Maybe.just(Either.left(e)));
380
+ }
381
+ return Task.create(cell.promise, cancel);
382
+ }
383
+ _Task.fromFunction = fromFunction;
384
+ function fromCallback(create, onCancel) {
385
+ const cell = settleCell();
386
+ let handler = null;
387
+ const cancel = (error) => {
388
+ const wasPending = cell.resolve(error.map(Either.left));
389
+ if (wasPending && handler) onCancel(handler.value);
390
+ return wasPending;
391
+ };
392
+ try {
393
+ const value = create((r) => cell.resolve(Maybe.just(Either.right(r))), (e) => cell.resolve(Maybe.just(Either.left(e))), () => cancel(Maybe.nothing()));
394
+ handler = { value };
395
+ const result = cell.settled();
396
+ if (result && !result.value.match({
397
+ just: (v) => v.isRight(),
398
+ nothing: () => false
399
+ })) onCancel(value);
400
+ } catch (e) {
401
+ cell.resolve(Maybe.just(Either.left(e)));
402
+ }
403
+ return Task.create(cell.promise, cancel);
404
+ }
405
+ _Task.fromCallback = fromCallback;
406
+ function fromAbortSignal(producer) {
407
+ const cell = settleCell();
408
+ const controller = new AbortController();
409
+ const cancel = (error) => {
410
+ const wasPending = cell.resolve(error.map(Either.left));
411
+ if (wasPending) controller.abort(error.match({
412
+ just: (err) => err,
413
+ nothing: () => void 0
414
+ }));
415
+ return wasPending;
416
+ };
417
+ try {
418
+ producer(controller.signal).then((value) => {
419
+ cell.resolve(Maybe.just(Either.right(value)));
420
+ }, (error) => {
421
+ if (controller.signal.aborted && (error?.name === "AbortError" || typeof DOMException !== "undefined" && error instanceof DOMException && error.name === "AbortError")) cell.resolve(Maybe.nothing());
422
+ else cell.resolve(Maybe.just(Either.left(error)));
423
+ });
424
+ } catch (e) {
425
+ cell.resolve(Maybe.just(Either.left(e)));
426
+ }
427
+ return Task.create(cell.promise, cancel);
428
+ }
429
+ _Task.fromAbortSignal = fromAbortSignal;
430
+ function promiseGenerator(promise) {
431
+ return Task.fromPromise(promise).generator();
432
+ }
433
+ _Task.promiseGenerator = promiseGenerator;
434
+ function timeoutGenerator(delay) {
435
+ return Task.timeout(delay).generator();
436
+ }
437
+ _Task.timeoutGenerator = timeoutGenerator;
438
+ function lift(promiseFunction) {
439
+ return (...args) => Task.fromPromise(promiseFunction(...args));
440
+ }
441
+ _Task.lift = lift;
442
+ function generate(taskGeneratorFunction) {
443
+ const generator = taskGeneratorFunction();
444
+ const cell = settleCell();
445
+ let current = null;
446
+ const sequentor = (invoke, override) => {
447
+ try {
448
+ const next = invoke();
449
+ if (next.done) {
450
+ cell.resolve(override ?? current?.error?.map(Either.left) ?? Maybe.just(Either.right(next.value)));
451
+ current = null;
452
+ return;
453
+ }
454
+ current = {
455
+ task: next.value,
456
+ error: null
457
+ };
458
+ next.value._invoke((maybe) => maybe.matchTap({
459
+ just: (either) => either.matchTap({
460
+ right: (value) => sequentor(() => generator.next(value), override),
461
+ left: (error) => sequentor(() => generator.throw(error), override)
462
+ }),
463
+ nothing: () => sequentor(() => generator.return(void 0), Maybe.nothing())
464
+ }));
465
+ } catch (error) {
466
+ cell.resolve(override ?? Maybe.just(Either.left(error)));
467
+ current = null;
468
+ }
469
+ };
470
+ sequentor(() => generator.next());
471
+ return Task.create(cell.promise, (error) => {
472
+ if (cell.settled() || !current) return false;
473
+ current.error = error;
474
+ return current.task._cancel(error);
475
+ });
476
+ }
477
+ _Task.generate = generate;
478
+ function generateFunction(taskGeneratorFunction) {
479
+ return (...args) => generate(() => taskGeneratorFunction(...args));
480
+ }
481
+ _Task.generateFunction = generateFunction;
482
+ function timeout(delay) {
483
+ return fromCallback((resolve) => setTimeout(resolve, delay), clearTimeout);
484
+ }
485
+ _Task.timeout = timeout;
486
+ function sequence(taskFunctions) {
487
+ return Array.from(taskFunctions).reduce((prev, taskFunction) => {
488
+ return prev.chain((list) => {
489
+ return taskFunction().map((value) => list.concat(value));
490
+ });
491
+ }, Task.resolved([]));
492
+ }
493
+ _Task.sequence = sequence;
494
+ function all(tasks) {
495
+ const taskArray = Array.from(tasks);
496
+ if (taskArray.length === 0) return Task.resolved([]);
497
+ return Task.fromCallback((resolve, reject, cancel) => {
498
+ const results = new Array(taskArray.length);
499
+ let remaining = taskArray.length;
500
+ taskArray.forEach((task, i) => {
501
+ task.matchTap({
502
+ resolved: (value) => {
503
+ results[i] = value;
504
+ remaining--;
505
+ if (remaining === 0) resolve(results);
506
+ },
507
+ rejected: (error) => {
508
+ reject(error);
509
+ taskArray.forEach((t) => t.cancel());
510
+ },
511
+ canceled: () => {
512
+ cancel();
513
+ taskArray.forEach((t) => t.cancel());
514
+ }
515
+ });
516
+ });
517
+ return taskArray;
518
+ }, (subtasks) => subtasks.forEach((t) => t.cancel()));
519
+ }
520
+ _Task.all = all;
521
+ function any(tasks) {
522
+ const cell = settleCell();
523
+ const taskArray = Array.from(tasks);
524
+ if (taskArray.length < 1) return Task.canceled();
525
+ const cancel = (error) => taskArray.map((task) => task._cancel(error)).every((r) => r);
526
+ const list = taskArray.map(() => Maybe.nothing());
527
+ const resolved = (value) => {
528
+ cell.resolve(value.map(Either.right));
529
+ cancel(Maybe.nothing());
530
+ };
531
+ const rejected = (error, i) => {
532
+ list[i] = Maybe.just(error);
533
+ if (Maybe.everyJust(list)) cell.resolve(Maybe.just(Either.left(list.map((v) => Just.just(v)))));
534
+ };
535
+ taskArray.map((task, i) => {
536
+ return task.matchTap({
537
+ resolved: (value) => resolved(Maybe.just(value)),
538
+ rejected: (error) => rejected(error, i),
539
+ canceled: () => resolved(Maybe.nothing())
540
+ });
541
+ });
542
+ return Task.create(cell.promise, cancel);
543
+ }
544
+ _Task.any = any;
545
+ function limit(task, limitTask) {
546
+ return Task.any([task, limitTask.chain(Task.canceled)]).chainRejected((errors) => Task.rejected(errors[0]));
547
+ }
548
+ _Task.limit = limit;
549
+ function repeat(taskFunction) {
550
+ const sequentor = () => taskFunction().chainRejected(sequentor);
551
+ return sequentor();
552
+ }
553
+ _Task.repeat = repeat;
554
+ })(Task || (Task = {}));
555
+ const stub = () => void 0;
556
+ const settleCell = () => {
557
+ let sink = stub;
558
+ let settled = null;
559
+ const promise = new Promise((resolve) => {
560
+ sink = resolve;
561
+ });
562
+ const resolve = (next) => {
563
+ if (settled) return false;
564
+ settled = { value: next };
565
+ sink(next);
566
+ return true;
567
+ };
568
+ return {
569
+ promise,
570
+ resolve,
571
+ settled: () => settled
572
+ };
573
+ };
574
+ function chainTaskMaybe(parent, op) {
575
+ const cell = settleCell();
576
+ let state = { phase: "parent" };
577
+ parent._invoke((result) => {
578
+ state = {
579
+ phase: "transform",
580
+ error: null
581
+ };
582
+ let child = null;
583
+ try {
584
+ child = op(result);
585
+ } catch (e) {
586
+ cell.resolve(Maybe.just(Either.left(e)));
587
+ state = { phase: "closed" };
588
+ return;
589
+ }
590
+ const transformError = state.phase === "transform" ? state.error : null;
591
+ state = {
592
+ phase: "child",
593
+ cancel: (error) => child._cancel(error)
594
+ };
595
+ if (transformError) {
596
+ cell.resolve(transformError.map(Either.left));
597
+ state = { phase: "closed" };
598
+ child._cancel(transformError);
599
+ return;
600
+ }
601
+ return child._invoke((value) => {
602
+ cell.resolve(value);
603
+ state = { phase: "closed" };
604
+ });
605
+ });
606
+ return Task.create(cell.promise, (error) => {
607
+ if (cell.settled()) return false;
608
+ if (state.phase === "parent") return parent._cancel(error);
609
+ else if (state.phase === "transform") {
610
+ state.error = error;
611
+ return true;
612
+ } else if (state.phase === "child") return state.cancel(error);
613
+ else return false;
614
+ });
615
+ }
616
+ function mapTaskMaybe(parent, op) {
617
+ const cell = settleCell();
618
+ let state = { phase: "parent" };
619
+ parent._invoke((result) => {
620
+ state = {
621
+ phase: "transform",
622
+ error: null
623
+ };
624
+ let value = null;
625
+ try {
626
+ value = op(result);
627
+ } catch (e) {
628
+ cell.resolve(Maybe.just(Either.left(e)));
629
+ state = { phase: "closed" };
630
+ return;
631
+ }
632
+ const transformError = state.phase === "transform" ? state.error : null;
633
+ if (transformError) cell.resolve(transformError.map(Either.left));
634
+ else cell.resolve(value);
635
+ state = { phase: "closed" };
636
+ });
637
+ return Task.create(cell.promise, (error) => {
638
+ if (cell.settled()) return false;
639
+ if (state.phase === "parent") return parent._cancel(error);
640
+ else if (state.phase === "transform") {
641
+ state.error = error;
642
+ return true;
643
+ } else return false;
644
+ });
645
+ }
646
+ function tapTaskMaybe(parent, op) {
647
+ parent._invoke((result) => {
648
+ try {
649
+ op(result);
650
+ } catch (e) {
651
+ reportError(e);
652
+ }
653
+ });
654
+ return parent;
655
+ }
656
+ function chainTaskEither(_task, op) {
657
+ return chainTaskMaybe(_task, (maybe) => maybe.map(op).orMap(Task.canceled).just);
658
+ }
659
+ function mapTaskEither(_task, op) {
660
+ return mapTaskMaybe(_task, (maybe) => maybe.map(op));
661
+ }
662
+ function tapTaskEither(_task, op) {
663
+ return tapTaskMaybe(_task, (maybe) => maybe.tap(op));
664
+ }
665
+ var TaskClass = class {
666
+ constructor(_state) {
667
+ this._state = _state;
668
+ if (!this._state.resolved) this._state.invoke = this._state.invoke.then((result) => {
669
+ if (!this._state.resolved) this._state = {
670
+ resolved: true,
671
+ result
672
+ };
673
+ return result;
674
+ });
675
+ }
676
+ _invoke(op) {
677
+ if (this._state.resolved) return op(this._state.result);
678
+ else return this._state.invoke.then(op);
679
+ }
680
+ _cancel(error) {
681
+ if (this._state.resolved) return false;
682
+ else return this._state.cancel(error);
683
+ }
684
+ tapCanceled(op) {
685
+ return tapTaskMaybe(this, (maybe) => maybe.orTap(op));
686
+ }
687
+ tapRejected(op) {
688
+ return tapTaskEither(this, (either) => either.orTap(op));
689
+ }
690
+ tap(op) {
691
+ return tapTaskEither(this, (either) => either.tap(op));
692
+ }
693
+ mapCanceled(op) {
694
+ return mapTaskMaybe(this, (maybe) => maybe.orMap(() => Either.right(op())));
695
+ }
696
+ mapRejected(op) {
697
+ return mapTaskEither(this, (either) => either.orMap(op));
698
+ }
699
+ map(op) {
700
+ return mapTaskEither(this, (either) => either.map(op));
701
+ }
702
+ chainCanceled(op) {
703
+ const mapEither = (either) => either.map(Task.resolved).orMap(Task.rejected).right;
704
+ return chainTaskMaybe(this, (maybe) => maybe.map(mapEither).orMap(op).just);
705
+ }
706
+ chainRejected(op) {
707
+ return chainTaskEither(this, (either) => either.map(Task.resolved).orMap(op).right);
708
+ }
709
+ chain(op) {
710
+ return chainTaskEither(this, (either) => either.map(op).orMap(Task.rejected).right);
711
+ }
712
+ resolved() {
713
+ if (this._state.resolved) return this._state.result;
714
+ else return null;
715
+ }
716
+ resolve() {
717
+ if (this._state.resolved) return Promise.resolve(this._state.result);
718
+ else return this._state.invoke;
719
+ }
720
+ cancel() {
721
+ if (this._state.resolved) return false;
722
+ else return this._state.cancel(Maybe.nothing());
723
+ }
724
+ reject(error) {
725
+ if (this._state.resolved) return false;
726
+ else return this._state.cancel(Maybe.just(error));
727
+ }
728
+ generator() {
729
+ return (function* (task) {
730
+ return yield task;
731
+ })(this);
732
+ }
733
+ matchTap(op) {
734
+ return tapTaskMaybe(this, (maybe) => {
735
+ return maybe.matchTap({
736
+ just: (either) => {
737
+ return either.matchTap({
738
+ right: op.resolved,
739
+ left: op.rejected
740
+ });
741
+ },
742
+ nothing: op.canceled
743
+ });
744
+ });
745
+ }
746
+ matchMap(op) {
747
+ return mapTaskMaybe(this, (maybe) => {
748
+ return Maybe.just(maybe.match({
749
+ just: (either) => {
750
+ return Either.right(either.match({
751
+ right: op.resolved,
752
+ left: op.rejected
753
+ }));
754
+ },
755
+ nothing: () => Either.right(op.canceled())
756
+ }));
757
+ });
758
+ }
759
+ matchChain(op) {
760
+ return chainTaskMaybe(this, (maybe) => {
761
+ return maybe.match({
762
+ just: (either) => {
763
+ return either.match({
764
+ right: op.resolved,
765
+ left: op.rejected
766
+ });
767
+ },
768
+ nothing: op.canceled
769
+ });
770
+ });
771
+ }
772
+ };
773
+ function reportError(error) {
774
+ if (typeof globalThis.reportError === "function") globalThis.reportError(error);
775
+ }
776
+ //#endregion
777
+ Object.defineProperty(exports, "Either", {
778
+ enumerable: true,
779
+ get: function() {
780
+ return Either;
781
+ }
782
+ });
783
+ Object.defineProperty(exports, "Just", {
784
+ enumerable: true,
785
+ get: function() {
786
+ return Just;
787
+ }
788
+ });
789
+ Object.defineProperty(exports, "Left", {
790
+ enumerable: true,
791
+ get: function() {
792
+ return Left;
793
+ }
794
+ });
795
+ Object.defineProperty(exports, "Maybe", {
796
+ enumerable: true,
797
+ get: function() {
798
+ return Maybe;
799
+ }
800
+ });
801
+ Object.defineProperty(exports, "Right", {
802
+ enumerable: true,
803
+ get: function() {
804
+ return Right;
805
+ }
806
+ });
807
+ Object.defineProperty(exports, "Task", {
808
+ enumerable: true,
809
+ get: function() {
810
+ return Task;
811
+ }
812
+ });
813
+
814
+ //# sourceMappingURL=t-tasks.js.map