@nobrayner/orf
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1345,3 +1345,3 @@ interface IResult<T, E> { | ||
*/ | ||
unwrapOr<U = T>(or_value: Result<T | U, E>): UnwrappedFuture<Result<T | U, E>>; | ||
unwrapOr<U = T, F = E>(or_value: Result<T | U, E | F>): UnwrappedFuture<Result<T | U, E | F>>; | ||
/** | ||
@@ -1365,3 +1365,22 @@ * Returns the resolved `Result<T, E>` from this future, or returns the result | ||
*/ | ||
unwrapOrElse<U = T>(or_fn: () => Result<T | U, E>): UnwrappedFuture<Result<T | U, E>>; | ||
unwrapOrElse<U = T, F = E>(or_fn: () => Result<T | U, E | F>): UnwrappedFuture<Result<T | U, E | F>>; | ||
/** | ||
* Runs the function provided in the `Ok` or `Error` branch, passing the value | ||
* or error respectively | ||
* | ||
* ## Example | ||
* ```ts | ||
* const result = Future.success(1).match({ | ||
* Ok: (value) => value, | ||
* Error: (error) => error, | ||
* }); | ||
* // result === 1 | ||
* | ||
* const result = Future.fail("error").match({ | ||
* Ok: (value) => value, | ||
* Error: (error) => error, | ||
* }); | ||
* // result === "error" | ||
* ``` | ||
*/ | ||
match<U, V = U>(config: { | ||
@@ -1368,0 +1387,0 @@ Ok: (t: T) => U; |
@@ -1,1 +0,1 @@ | ||
"use strict";var W=Object.defineProperty;var re=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var oe=Object.prototype.hasOwnProperty;var ue=(u,e)=>{for(var t in e)W(u,t,{get:e[t],enumerable:!0})},le=(u,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of ne(e))!oe.call(u,l)&&l!==t&&W(u,l,{get:()=>e[l],enumerable:!(n=re(e,l))||n.enumerable});return u};var ae=u=>le(W({},"__esModule",{value:!0}),u);var X=(u,e,t)=>{if(!e.has(u))throw TypeError("Cannot "+t)};var r=(u,e,t)=>(X(u,e,"read from private field"),t?t.call(u):e.get(u)),S=(u,e,t)=>{if(e.has(u))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(u):e.set(u,t)},U=(u,e,t,n)=>(X(u,e,"write to private field"),n?n.call(u,t):e.set(u,t),t);var J=(u,e,t)=>(X(u,e,"access private method"),t);var Z=(u,e,t)=>new Promise((n,l)=>{var R=b=>{try{k(t.next(b))}catch(a){l(a)}},s=b=>{try{k(t.throw(b))}catch(a){l(a)}},k=b=>b.done?n(b.value):Promise.resolve(b.value).then(R,s);k((t=t.apply(u,e)).next())});var se={};ue(se,{Future:()=>v,Option:()=>E,Result:()=>f});module.exports=ae(se);var Y=Error,f;(b=>{function u(a){return Object.freeze(new z(a))}b.Ok=u;function e(a){return Object.freeze(new P(a))}b.Error=e;function t(a){return a instanceof z||a instanceof P}b.isResult=t;function n(a,d){var F;try{return u(a())}catch(x){return e((F=d==null?void 0:d(x))!=null?F:x)}}b.fromExecution=n;function l(a,d){return Z(this,null,function*(){var F;try{return u(yield a)}catch(x){return e((F=d==null?void 0:d(x))!=null?F:x)}})}b.fromAwaitable=l;function R(a){let d=[];for(let F of a){if(F.isError())return F;d.push(F.unwrap())}return u(d)}b.all=R;function s(a){let d={};for(let F in a){let x=a[F];if(x.isError())return x;d[F]=x.unwrap()}return u(d)}b.allFromDict=s;function k(a){if(a.__orf_type__!=="Result"&&!("tag"in a))throw new Y("Invalid Result JSON");if(a.tag==="Ok"&&"value"in a)return u(a.value);if(a.tag==="Error"&&"error"in a)return e(a.error);throw new Y("Invalid Result JSON")}b.fromJSON=k})(f||(f={}));var z=class{constructor(e){this.value=e}isOk(){return!0}isError(){return!this.isOk()}match(e){return e.Ok(this.value)}map(e){return f.Ok(e(this.value))}mapOr(e,t){return t(this.value)}mapOrElse(e,t){return t(this.value)}mapError(e){return this}and(e){return e}andThen(e){return e(this.value)}or(e){return this}orElse(e){return this}tap(e){return e(Object.freeze(structuredClone(this.value))),this}tapError(e){return this}ok(){return E.Some(this.value)}unwrap(){return this.value}unwrapError(){throw new Y("Cannot unwrapError an Ok")}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}toJSON(){return{__orf_type__:"Result",tag:"Ok",value:this.value}}},P=class{constructor(e){this.error=e}isOk(){return!1}isError(){return!this.isOk()}match(e){return e.Error(this.error)}map(e){return this}mapOr(e,t){return e}mapOrElse(e,t){return e()}mapError(e){return f.Error(e(this.error))}and(e){return this}andThen(e){return this}or(e){return e}orElse(e){return e(this.error)}tap(e){return this}tapError(e){return e(Object.freeze(structuredClone(this.error))),this}ok(){return E.None()}unwrap(){throw this.error}unwrapError(){return this.error}unwrapOr(e){return e}unwrapOrElse(e){return e(this.error)}toJSON(){return{__orf_type__:"Result",tag:"Error",error:this.error}}};var E;(R=>{function u(s){return Object.freeze(new q(s))}R.Some=u;function e(){return ie}R.None=e;function t(s){return s instanceof q||s instanceof B}R.isOption=t;function n(s){return s?u(s):e()}R.from=n;function l(s){if(s.__orf_type__!=="Option"&&!("tag"in s))throw new Error("Invalid Option JSON");if(s.tag==="Some"&&"value"in s)return u(s.value);if(s.tag==="None")return e();throw new Error("Invalid Option JSON")}R.fromJSON=l})(E||(E={}));var q=class{constructor(e){this.value=e}isSome(){return!0}isNone(){return!this.isSome()}match(e){return e.Some(this.value)}map(e){return E.Some(e(this.value))}mapOr(e,t){return t(this.value)}mapOrElse(e,t){return t(this.value)}and(e){return e}andThen(e){return e(this.value)}or(e){return this}orElse(e){return this}tap(e){return e(Object.freeze(structuredClone(this.value))),this}unwrap(){return this.value}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}okOr(e){return f.Ok(this.value)}okOrElse(e){return f.Ok(this.value)}toJSON(){return{__orf_type__:"Option",tag:"Some",value:this.value}}},B=class{isSome(){return!1}isNone(){return!this.isSome()}match(e){return e.None()}map(e){return this}mapOr(e,t){return e}mapOrElse(e,t){return e()}and(e){return this}andThen(e){return this}or(e){return e}orElse(e){return e()}tap(e){return this}unwrap(){throw new Error("Cannot unwrap None")}unwrapOr(e){return e}unwrapOrElse(e){return e()}okOr(e){return f.Error(e)}okOrElse(e){return f.Error(e())}toJSON(){return{__orf_type__:"Option",tag:"None"}}},ie=Object.freeze(new B);var v;(V=>{function u(o){return new D({tag:"completed",value:o})}V.value=u;function e(o){return new A({tag:"completed",value:f.Ok(o)})}V.success=e;function t(o){return new A({tag:"completed",value:f.Error(o)})}V.fail=t;function n(o){return o instanceof D}V.isFuture=n;function l(o){return o instanceof A}V.isFallibleFuture=l;function R(o){return o instanceof A?o:o instanceof Promise?V.makeFallible((T,g)=>{o.then(T).catch(g)}):o.match({Ok:T=>e(T),Error:T=>t(T)})}V.fromFallible=R;function s(o,T){return new D({tag:"pending",execute:o},T)}V.make=s;function k(o){return new D({tag:"pending",execute:o},{lazy:!0})}V.lazy=k;function b(o,T){return new A({tag:"pending",execute:o},T)}V.makeFallible=b;function a(o){return new A({tag:"pending",execute:o},{lazy:!0})}V.lazyFallible=a;function d(o){return new D({tag:"pending",execute:T=>{let g=setTimeout(T,o);return()=>{clearTimeout(g)}}})}V.wait=d;function F(o){if(!o.some(V.isFallibleFuture)){let C=V.make(I=>{let y=new Array(o.length),_=0;return o.forEach((h,m)=>{h.onResolved(O=>{y[m]=O,_+=1,_===o.length&&I(y)})}),()=>o.forEach(h=>h.cancel())});return o.forEach(I=>I.onCancelled(C.cancel.bind(C))),C}let g=V.makeFallible((C,I)=>{let y=new Array(o.length),_=0;for(let h=0;h<o.length;++h){let m=o[h];V.isFuture(m)&&(m=m.neverError()),m.onResolved(O=>{y[h]=O,_+=1,_===o.length&&f.all(y).match({Ok:C,Error:I})})}return()=>o.forEach(h=>h.cancel())});return o.forEach(C=>C.onCancelled(g.cancel.bind(g))),g}V.all=F;function x(o){let T=Object.values(o),g=T.length;if(!T.some(V.isFallibleFuture)){let y=V.make(_=>{let h={},m=0;for(let O in o)o[O].onResolved(Q=>{h[O]=Q,m+=1,m===g&&_(h)});return()=>{for(let O of T)O.cancel()}});for(let _ of T)_.onCancelled(y.cancel.bind(y));return y}let I=V.makeFallible((y,_)=>{let h={},m=0;for(let O in o){let K=o[O];V.isFuture(K)&&(K=K.neverError()),K.onResolved(Q=>{h[O]=Q,m+=1,m===g&&f.allFromDict(h).match({Ok:y,Error:_})})}return()=>{for(let O of T)O.cancel()}});for(let y of T)y.onCancelled(I.cancel.bind(I));return I}V.allFromDict=x})(v||(v={}));var c,G,$,D=class{constructor(e,t){S(this,G);S(this,c,void 0);U(this,c,e),t!=null&&t.lazy||this.then()}onResolved(e){var t;r(this,c).tag==="pending"?(r(this,c).resolve_callbacks=(t=r(this,c).resolve_callbacks)!=null?t:[],r(this,c).resolve_callbacks.push(e)):r(this,c).tag==="completed"&&e(r(this,c).value)}onCancelled(e){var t;r(this,c).tag==="pending"?(r(this,c).cancel_callbacks=(t=r(this,c).cancel_callbacks)!=null?t:[],r(this,c).cancel_callbacks.push(e)):r(this,c).tag==="cancelled"&&e()}cancel(){if(r(this,c).tag==="pending"){let{cancel:e,cancel_callbacks:t}=r(this,c);U(this,c,{tag:"cancelled"}),e==null||e(),t==null||t.forEach(n=>n())}}then(e){if(r(this,c).tag==="pending"&&r(this,c).execute){let t=r(this,c).execute(J(this,G,$).bind(this));r(this,c).execute=null,t&&(r(this,c).cancel=t)}return e&&(this.onResolved(t=>e(E.Some(t))),this.onCancelled(()=>e(E.None()))),this}map(e){let t=v.make(n=>(this.onResolved(l=>{n(e(l))}),this.cancel.bind(this)));return this.onCancelled(t.cancel.bind(t)),t}tap(e){return this.onResolved(e),this}neverError(){let e=v.makeFallible((t,n)=>(this.onResolved(t),this.cancel.bind(this)));return this.onCancelled(e.cancel.bind(e)),e}unwrap(){let e=new N({tag:"pending",execute:t=>{this.onResolved(t)}});return this.onCancelled(e.cancel.bind(e)),e}unwrapOr(e){return new N({tag:"pending",execute:n=>(this.onResolved(l=>{n(l)}),this.onCancelled(()=>{n(e)}),()=>{n(e)})})}unwrapOrElse(e){return new N({tag:"pending",execute:n=>(this.onResolved(l=>{n(l)}),this.onCancelled(()=>{n(e())}),()=>{n(e())})})}};c=new WeakMap,G=new WeakSet,$=function(e){if(r(this,c).tag==="pending"){let{resolve_callbacks:t}=r(this,c);U(this,c,{tag:"completed",value:e}),t==null||t.forEach(n=>n(e))}};var i,H,j,L,ee,A=class{constructor(e,t){S(this,H);S(this,L);S(this,i,void 0);U(this,i,e),t!=null&&t.lazy||this.then()}onResolved(e){var t;r(this,i).tag==="pending"?(r(this,i).resolve_callbacks=(t=r(this,i).resolve_callbacks)!=null?t:[],r(this,i).resolve_callbacks.push(e)):r(this,i).tag==="completed"&&e(r(this,i).value)}onSucceeded(e){this.onResolved(t=>{t.match({Ok:e,Error:()=>{}})})}onFailed(e){this.onResolved(t=>{t.match({Ok:()=>{},Error:e})})}onCancelled(e){var t;r(this,i).tag==="pending"?(r(this,i).cancel_callbacks=(t=r(this,i).cancel_callbacks)!=null?t:[],r(this,i).cancel_callbacks.push(e)):r(this,i).tag==="cancelled"&&e()}cancel(){if(r(this,i).tag==="pending"){let{cancel:e,cancel_callbacks:t}=r(this,i);U(this,i,{tag:"cancelled"}),e==null||e(),t==null||t.forEach(n=>n())}}then(e){if(r(this,i).tag==="pending"&&r(this,i).execute){let t=r(this,i).execute(J(this,H,j).bind(this),J(this,L,ee).bind(this));r(this,i).execute=null,t&&(r(this,i).cancel=t)}return e&&(this.onResolved(t=>e(E.Some(t))),this.onCancelled(()=>e(E.None()))),this}map(e){let t=v.makeFallible((n,l)=>(this.onResolved(R=>{R.match({Ok:s=>{n(e(s))},Error:l})}),this.cancel.bind(this)));return this.onCancelled(t.cancel.bind(t)),t}mapError(e){let t=v.makeFallible((n,l)=>(this.onResolved(R=>{R.match({Ok:n,Error:s=>{l(e(s))}})}),this.cancel.bind(this)));return this.onCancelled(t.cancel.bind(t)),t}andThen(e){let t=v.makeFallible((n,l)=>(this.onResolved(R=>{R.match({Ok:s=>{let k=v.fromFallible(e(s));k.onSucceeded(n),k.onFailed(l)},Error:l})}),this.cancel.bind(this)));return this.onCancelled(t.cancel.bind(t)),t}orElse(e){let t=v.makeFallible((n,l)=>(this.onResolved(R=>{R.match({Ok:n,Error:s=>{let k=v.fromFallible(e(s));k.onSucceeded(n),k.onFailed(l)}})}),this.cancel.bind(this)));return this.onCancelled(t.cancel.bind(t)),t}tap(e){return this.onSucceeded(e),this}tapError(e){return this.onFailed(e),this}ok(){let e=v.make(t=>(this.onSucceeded(n=>t(E.Some(n))),this.onFailed(()=>t(E.None())),this.cancel.bind(this)));return this.onCancelled(e.cancel.bind(e)),e}unwrap(){let e=new N({tag:"pending",execute:t=>{this.onResolved(t)}});return this.onCancelled(e.cancel.bind(e)),e}unwrapOr(e){return new N({tag:"pending",execute:n=>(this.onResolved(n),this.onCancelled(()=>{n(e)}),()=>{n(e)})})}unwrapOrElse(e){return new N({tag:"pending",execute:n=>(this.onResolved(n),this.onCancelled(()=>{n(e())}),()=>{n(e())})})}match(e){let t=new N({tag:"pending",execute:n=>{this.onResolved(l=>{n(l.match(e))})}});return this.onCancelled(t.cancel.bind(t)),t}};i=new WeakMap,H=new WeakSet,j=function(e){if(r(this,i).tag==="pending"){let{resolve_callbacks:t}=r(this,i),n=f.Ok(e);U(this,i,{tag:"completed",value:n}),t==null||t.forEach(l=>l(n))}},L=new WeakSet,ee=function(e){if(r(this,i).tag==="pending"){let{resolve_callbacks:t}=r(this,i),n=f.Error(e);U(this,i,{tag:"completed",value:n}),t==null||t.forEach(l=>l(n))}};var p,w,M,te,N=class{constructor(e){S(this,M);S(this,p,void 0);S(this,w,void 0);if(U(this,p,e),r(this,p).tag==="pending"&&r(this,p).execute){let t=r(this,p).execute(J(this,M,te).bind(this));r(this,p).execute=null,t&&!r(this,w)&&U(this,w,t)}}onResolved(e){var t;r(this,p).tag==="pending"?(r(this,p).resolve_callbacks=(t=r(this,p).resolve_callbacks)!=null?t:[],r(this,p).resolve_callbacks.push(e)):r(this,p).tag==="completed"&&e(r(this,p).value)}onCancelled(e){r(this,p).tag==="pending"&&!r(this,w)?U(this,w,e):r(this,p).tag==="cancelled"&&!r(this,w)&&e(new Error("Cannot cancel UnwrappedFuture"))}cancel(){r(this,p).tag==="pending"&&(r(this,w)?(r(this,w).call(this,new Error("Cannot cancel UnwrappedFuture")),r(this,p).tag==="pending"&&U(this,p,{tag:"cancelled"})):U(this,p,{tag:"cancelled"}))}then(e,t){return e&&this.onResolved(e),t&&this.onCancelled(t),this}};p=new WeakMap,w=new WeakMap,M=new WeakSet,te=function(e){if(r(this,p).tag==="pending"){let{resolve_callbacks:t}=r(this,p);U(this,p,{tag:"completed",value:e}),t==null||t.forEach(n=>n(e))}};0&&(module.exports={Future,Option,Result}); | ||
"use strict";var W=Object.defineProperty;var re=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var oe=Object.prototype.hasOwnProperty;var ue=(u,e)=>{for(var t in e)W(u,t,{get:e[t],enumerable:!0})},le=(u,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of ne(e))!oe.call(u,l)&&l!==t&&W(u,l,{get:()=>e[l],enumerable:!(n=re(e,l))||n.enumerable});return u};var ae=u=>le(W({},"__esModule",{value:!0}),u);var X=(u,e,t)=>{if(!e.has(u))throw TypeError("Cannot "+t)};var r=(u,e,t)=>(X(u,e,"read from private field"),t?t.call(u):e.get(u)),S=(u,e,t)=>{if(e.has(u))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(u):e.set(u,t)},F=(u,e,t,n)=>(X(u,e,"write to private field"),n?n.call(u,t):e.set(u,t),t);var J=(u,e,t)=>(X(u,e,"access private method"),t);var Z=(u,e,t)=>new Promise((n,l)=>{var R=b=>{try{k(t.next(b))}catch(a){l(a)}},s=b=>{try{k(t.throw(b))}catch(a){l(a)}},k=b=>b.done?n(b.value):Promise.resolve(b.value).then(R,s);k((t=t.apply(u,e)).next())});var se={};ue(se,{Future:()=>v,Option:()=>E,Result:()=>f});module.exports=ae(se);var Y=Error,f;(b=>{function u(a){return Object.freeze(new z(a))}b.Ok=u;function e(a){return Object.freeze(new P(a))}b.Error=e;function t(a){return a instanceof z||a instanceof P}b.isResult=t;function n(a,d){var U;try{return u(a())}catch(x){return e((U=d==null?void 0:d(x))!=null?U:x)}}b.fromExecution=n;function l(a,d){return Z(this,null,function*(){var U;try{return u(yield a)}catch(x){return e((U=d==null?void 0:d(x))!=null?U:x)}})}b.fromAwaitable=l;function R(a){let d=[];for(let U of a){if(U.isError())return U;d.push(U.unwrap())}return u(d)}b.all=R;function s(a){let d={};for(let U in a){let x=a[U];if(x.isError())return x;d[U]=x.unwrap()}return u(d)}b.allFromDict=s;function k(a){if(a.__orf_type__!=="Result"&&!("tag"in a))throw new Y("Invalid Result JSON");if(a.tag==="Ok"&&"value"in a)return u(a.value);if(a.tag==="Error"&&"error"in a)return e(a.error);throw new Y("Invalid Result JSON")}b.fromJSON=k})(f||(f={}));var z=class{constructor(e){this.value=e}isOk(){return!0}isError(){return!this.isOk()}match(e){return e.Ok(this.value)}map(e){return f.Ok(e(this.value))}mapOr(e,t){return t(this.value)}mapOrElse(e,t){return t(this.value)}mapError(e){return this}and(e){return e}andThen(e){return e(this.value)}or(e){return this}orElse(e){return this}tap(e){return e(Object.freeze(structuredClone(this.value))),this}tapError(e){return this}ok(){return E.Some(this.value)}unwrap(){return this.value}unwrapError(){throw new Y("Cannot unwrapError an Ok")}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}toJSON(){return{__orf_type__:"Result",tag:"Ok",value:this.value}}},P=class{constructor(e){this.error=e}isOk(){return!1}isError(){return!this.isOk()}match(e){return e.Error(this.error)}map(e){return this}mapOr(e,t){return e}mapOrElse(e,t){return e()}mapError(e){return f.Error(e(this.error))}and(e){return this}andThen(e){return this}or(e){return e}orElse(e){return e(this.error)}tap(e){return this}tapError(e){return e(Object.freeze(structuredClone(this.error))),this}ok(){return E.None()}unwrap(){throw this.error}unwrapError(){return this.error}unwrapOr(e){return e}unwrapOrElse(e){return e(this.error)}toJSON(){return{__orf_type__:"Result",tag:"Error",error:this.error}}};var E;(R=>{function u(s){return Object.freeze(new q(s))}R.Some=u;function e(){return ie}R.None=e;function t(s){return s instanceof q||s instanceof B}R.isOption=t;function n(s){return s?u(s):e()}R.from=n;function l(s){if(s.__orf_type__!=="Option"&&!("tag"in s))throw new Error("Invalid Option JSON");if(s.tag==="Some"&&"value"in s)return u(s.value);if(s.tag==="None")return e();throw new Error("Invalid Option JSON")}R.fromJSON=l})(E||(E={}));var q=class{constructor(e){this.value=e}isSome(){return!0}isNone(){return!this.isSome()}match(e){return e.Some(this.value)}map(e){return E.Some(e(this.value))}mapOr(e,t){return t(this.value)}mapOrElse(e,t){return t(this.value)}and(e){return e}andThen(e){return e(this.value)}or(e){return this}orElse(e){return this}tap(e){return e(Object.freeze(structuredClone(this.value))),this}unwrap(){return this.value}unwrapOr(e){return this.value}unwrapOrElse(e){return this.value}okOr(e){return f.Ok(this.value)}okOrElse(e){return f.Ok(this.value)}toJSON(){return{__orf_type__:"Option",tag:"Some",value:this.value}}},B=class{isSome(){return!1}isNone(){return!this.isSome()}match(e){return e.None()}map(e){return this}mapOr(e,t){return e}mapOrElse(e,t){return e()}and(e){return this}andThen(e){return this}or(e){return e}orElse(e){return e()}tap(e){return this}unwrap(){throw new Error("Cannot unwrap None")}unwrapOr(e){return e}unwrapOrElse(e){return e()}okOr(e){return f.Error(e)}okOrElse(e){return f.Error(e())}toJSON(){return{__orf_type__:"Option",tag:"None"}}},ie=Object.freeze(new B);var v;(V=>{function u(o){return new D({tag:"completed",value:o})}V.value=u;function e(o){return new A({tag:"completed",value:f.Ok(o)})}V.success=e;function t(o){return new A({tag:"completed",value:f.Error(o)})}V.fail=t;function n(o){return o instanceof D}V.isFuture=n;function l(o){return o instanceof A}V.isFallibleFuture=l;function R(o){return o instanceof A?o:o instanceof Promise?V.makeFallible((T,g)=>{o.then(T).catch(g)}):o.match({Ok:T=>e(T),Error:T=>t(T)})}V.fromFallible=R;function s(o,T){return new D({tag:"pending",execute:o},T)}V.make=s;function k(o){return new D({tag:"pending",execute:o},{lazy:!0})}V.lazy=k;function b(o,T){return new A({tag:"pending",execute:o},T)}V.makeFallible=b;function a(o){return new A({tag:"pending",execute:o},{lazy:!0})}V.lazyFallible=a;function d(o){return new D({tag:"pending",execute:T=>{let g=setTimeout(T,o);return()=>{clearTimeout(g)}}})}V.wait=d;function U(o){if(!o.some(V.isFallibleFuture)){let C=V.make(I=>{let y=new Array(o.length),_=0;return o.forEach((h,m)=>{h.onResolved(O=>{y[m]=O,_+=1,_===o.length&&I(y)})}),()=>o.forEach(h=>h.cancel())});return o.forEach(I=>I.onCancelled(C.cancel.bind(C))),C}let g=V.makeFallible((C,I)=>{let y=new Array(o.length),_=0;for(let h=0;h<o.length;++h){let m=o[h];V.isFuture(m)&&(m=m.neverError()),m.onResolved(O=>{y[h]=O,_+=1,_===o.length&&f.all(y).match({Ok:C,Error:I})})}return()=>o.forEach(h=>h.cancel())});return o.forEach(C=>C.onCancelled(g.cancel.bind(g))),g}V.all=U;function x(o){let T=Object.values(o),g=T.length;if(!T.some(V.isFallibleFuture)){let y=V.make(_=>{let h={},m=0;for(let O in o)o[O].onResolved(Q=>{h[O]=Q,m+=1,m===g&&_(h)});return()=>{for(let O of T)O.cancel()}});for(let _ of T)_.onCancelled(y.cancel.bind(y));return y}let I=V.makeFallible((y,_)=>{let h={},m=0;for(let O in o){let K=o[O];V.isFuture(K)&&(K=K.neverError()),K.onResolved(Q=>{h[O]=Q,m+=1,m===g&&f.allFromDict(h).match({Ok:y,Error:_})})}return()=>{for(let O of T)O.cancel()}});for(let y of T)y.onCancelled(I.cancel.bind(I));return I}V.allFromDict=x})(v||(v={}));var c,G,$,D=class{constructor(e,t){S(this,G);S(this,c,void 0);F(this,c,e),t!=null&&t.lazy||this.then()}onResolved(e){var t;r(this,c).tag==="pending"?(r(this,c).resolve_callbacks=(t=r(this,c).resolve_callbacks)!=null?t:[],r(this,c).resolve_callbacks.push(e)):r(this,c).tag==="completed"&&e(r(this,c).value)}onCancelled(e){var t;r(this,c).tag==="pending"?(r(this,c).cancel_callbacks=(t=r(this,c).cancel_callbacks)!=null?t:[],r(this,c).cancel_callbacks.push(e)):r(this,c).tag==="cancelled"&&e()}cancel(){if(r(this,c).tag==="pending"){let{cancel:e,cancel_callbacks:t}=r(this,c);F(this,c,{tag:"cancelled"}),e==null||e(),t==null||t.forEach(n=>n())}}then(e){if(r(this,c).tag==="pending"&&r(this,c).execute){let t=r(this,c).execute(J(this,G,$).bind(this));r(this,c).execute=null,t&&(r(this,c).cancel=t)}return e&&(this.onResolved(t=>e(E.Some(t))),this.onCancelled(()=>e(E.None()))),this}map(e){let t=v.make(n=>(this.onResolved(l=>{n(e(l))}),this.cancel.bind(this)));return this.onCancelled(t.cancel.bind(t)),t}tap(e){return this.onResolved(e),this}neverError(){let e=v.makeFallible((t,n)=>(this.onResolved(t),this.cancel.bind(this)));return this.onCancelled(e.cancel.bind(e)),e}unwrap(){let e=new N({tag:"pending",execute:t=>{this.onResolved(t)}});return this.onCancelled(e.cancel.bind(e)),e}unwrapOr(e){return new N({tag:"pending",execute:n=>(this.onResolved(l=>{n(l)}),this.onCancelled(()=>{n(e)}),()=>{n(e)})})}unwrapOrElse(e){return new N({tag:"pending",execute:n=>(this.onResolved(l=>{n(l)}),this.onCancelled(()=>{n(e())}),()=>{n(e())})})}};c=new WeakMap,G=new WeakSet,$=function(e){if(r(this,c).tag==="pending"){let{resolve_callbacks:t}=r(this,c);F(this,c,{tag:"completed",value:e}),t==null||t.forEach(n=>n(e))}};var i,H,j,L,ee,A=class{constructor(e,t){S(this,H);S(this,L);S(this,i,void 0);F(this,i,e),t!=null&&t.lazy||this.then()}onResolved(e){var t;r(this,i).tag==="pending"?(r(this,i).resolve_callbacks=(t=r(this,i).resolve_callbacks)!=null?t:[],r(this,i).resolve_callbacks.push(e)):r(this,i).tag==="completed"&&e(r(this,i).value)}onSucceeded(e){this.onResolved(t=>{t.match({Ok:e,Error:()=>{}})})}onFailed(e){this.onResolved(t=>{t.match({Ok:()=>{},Error:e})})}onCancelled(e){var t;r(this,i).tag==="pending"?(r(this,i).cancel_callbacks=(t=r(this,i).cancel_callbacks)!=null?t:[],r(this,i).cancel_callbacks.push(e)):r(this,i).tag==="cancelled"&&e()}cancel(){if(r(this,i).tag==="pending"){let{cancel:e,cancel_callbacks:t}=r(this,i);F(this,i,{tag:"cancelled"}),e==null||e(),t==null||t.forEach(n=>n())}}then(e){if(r(this,i).tag==="pending"&&r(this,i).execute){let t=r(this,i).execute(J(this,H,j).bind(this),J(this,L,ee).bind(this));r(this,i).execute=null,t&&(r(this,i).cancel=t)}return e&&(this.onResolved(t=>e(E.Some(t))),this.onCancelled(()=>e(E.None()))),this}map(e){let t=v.makeFallible((n,l)=>(this.onResolved(R=>{R.match({Ok:s=>{n(e(s))},Error:l})}),this.cancel.bind(this)));return this.onCancelled(t.cancel.bind(t)),t}mapError(e){let t=v.makeFallible((n,l)=>(this.onResolved(R=>{R.match({Ok:n,Error:s=>{l(e(s))}})}),this.cancel.bind(this)));return this.onCancelled(t.cancel.bind(t)),t}andThen(e){let t=v.makeFallible((n,l)=>(this.onResolved(R=>{R.match({Ok:s=>{let k=v.fromFallible(e(s));k.onSucceeded(n),k.onFailed(l)},Error:l})}),this.cancel.bind(this)));return this.onCancelled(t.cancel.bind(t)),t}orElse(e){let t=v.makeFallible((n,l)=>(this.onResolved(R=>{R.match({Ok:n,Error:s=>{let k=v.fromFallible(e(s));k.onSucceeded(n),k.onFailed(l)}})}),this.cancel.bind(this)));return this.onCancelled(t.cancel.bind(t)),t}tap(e){return this.onSucceeded(e),this}tapError(e){return this.onFailed(e),this}ok(){let e=v.make(t=>(this.onSucceeded(n=>t(E.Some(n))),this.onFailed(()=>t(E.None())),this.cancel.bind(this)));return this.onCancelled(e.cancel.bind(e)),e}unwrap(){let e=new N({tag:"pending",execute:t=>{this.onResolved(t)}});return this.onCancelled(e.cancel.bind(e)),e}unwrapOr(e){return new N({tag:"pending",execute:n=>(this.onResolved(n),this.onCancelled(()=>{n(e)}),()=>{n(e)})})}unwrapOrElse(e){return new N({tag:"pending",execute:n=>(this.onResolved(n),this.onCancelled(()=>{n(e())}),()=>{n(e())})})}match(e){let t=new N({tag:"pending",execute:n=>{this.onResolved(l=>{n(l.match(e))})}});return this.onCancelled(t.cancel.bind(t)),t}};i=new WeakMap,H=new WeakSet,j=function(e){if(r(this,i).tag==="pending"){let{resolve_callbacks:t}=r(this,i),n=f.Ok(e);F(this,i,{tag:"completed",value:n}),t==null||t.forEach(l=>l(n))}},L=new WeakSet,ee=function(e){if(r(this,i).tag==="pending"){let{resolve_callbacks:t}=r(this,i),n=f.Error(e);F(this,i,{tag:"completed",value:n}),t==null||t.forEach(l=>l(n))}};var p,w,M,te,N=class{constructor(e){S(this,M);S(this,p,void 0);S(this,w,void 0);if(F(this,p,e),r(this,p).tag==="pending"&&r(this,p).execute){let t=r(this,p).execute(J(this,M,te).bind(this));r(this,p).execute=null,t&&!r(this,w)&&F(this,w,t)}}onResolved(e){var t;r(this,p).tag==="pending"?(r(this,p).resolve_callbacks=(t=r(this,p).resolve_callbacks)!=null?t:[],r(this,p).resolve_callbacks.push(e)):r(this,p).tag==="completed"&&e(r(this,p).value)}onCancelled(e){r(this,p).tag==="pending"&&!r(this,w)?F(this,w,e):r(this,p).tag==="cancelled"&&!r(this,w)&&e(new Error("Cannot cancel UnwrappedFuture"))}cancel(){r(this,p).tag==="pending"&&(r(this,w)?(r(this,w).call(this,new Error("Cannot cancel UnwrappedFuture")),r(this,p).tag==="pending"&&F(this,p,{tag:"cancelled"})):F(this,p,{tag:"cancelled"}))}then(e,t){return e&&this.onResolved(e),t&&this.onCancelled(t),this}};p=new WeakMap,w=new WeakMap,M=new WeakSet,te=function(e){if(r(this,p).tag==="pending"){let{resolve_callbacks:t}=r(this,p);F(this,p,{tag:"completed",value:e}),t==null||t.forEach(n=>n(e))}};0&&(module.exports={Future,Option,Result}); |
{ | ||
"name": "@nobrayner/orf", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Option. Result. Future", | ||
@@ -34,14 +34,6 @@ "sideEffect": false, | ||
"tsup": "^6.2.3", | ||
"typescript": "^5.7.2", | ||
"typescript": "^5", | ||
"vite-node": "^2.1.6", | ||
"vitest": "^2.1.5" | ||
}, | ||
"peerDependencies": { | ||
"typescript": ">=5.0.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"typescript": { | ||
"optional": true | ||
} | ||
}, | ||
"scripts": { | ||
@@ -48,0 +40,0 @@ "test": "vitest", |
Sorry, the diff of this file is not supported yet
71988
0
1531