UNPKG

react-solid-flow

Version:

[SolidJS](https://www.solidjs.com/docs/latest/api#control-flow)-inspired basic control-flow components and everyday async state hook library for [React](https://reactjs.org/)

2 lines 4.53 kB
import m,{Fragment as x,isValidElement as O}from"react";import b from"react";import{isValidElement as U}from"react";function i(e){return e==null?null:U(e)?e:b.createElement(b.Fragment,null,e)}function V({children:e,each:r,fallback:t=null}){if(!Array.isArray(r)||!r.length||e==null)return i(t);if(typeof e!="function")return m.createElement(m.Fragment,null,r.map((l,n)=>m.createElement(x,{key:n},e)));let o=[];for(let l=0;l<r.length;l++){let n=e(r[l],l);n!=null&&(!O(n)||!n.key?o.push(m.createElement(x,{key:l},n)):o.push(n))}return o.length?m.createElement(m.Fragment,null,o):i(t)}function s(e,...r){return typeof e=="function"?i(e(...r)):i(e)}function B({fallback:e=null,...r}){return r.when?s(r.children,r.when):i(e)}import{Children as J,isValidElement as M}from"react";function Y(e){for(let r of J.toArray(e.children))if(M(r)&&r.props.when)return r;return i(e.fallback)}function j({when:e,children:r}){return e?s(r,e):null}import{Component as q}from"react";var A=class extends q{constructor(r){super(r),this.resetError=this.resetError.bind(this)}state={error:void 0};static getDerivedStateFromError(r){return{error:r}}componentDidCatch(r,t){this.props.onCatch?.(r,t),this.setState({error:r})}resetError(){this.setState({error:void 0})}render(){return this.state.error===void 0?this.props.children:typeof this.props.fallback=="function"?this.props.fallback(this.state.error,this.resetError):this.props.fallback}};import X,{forwardRef as G}from"react";var H=G,K=H(function({component:r,...t},o){return r?X.createElement(r,{...t,ref:o}):null});import{createPortal as Q}from"react-dom";function W({mount:e,...r}){let t=e==null||e instanceof Element||e instanceof DocumentFragment?e:document.querySelector(e);return t?Q(r.children,t):null}function Z(e){return e.for==null?null:e.for.loading?s(e.fallback):e.for.error!=null?s(e.catch,e.for.error):s(e.children,e.for.data)}var u=class e{loading;data;error;state;latest;constructor(r,t){this.data=r?.data,this.error=r?.error,this.loading=!!r?.loading,this.data!==void 0?this.latest=this.data:this.latest=t?.latest,this.state=e.getState(this)}static from(r,t){let o=r instanceof Promise;return new e(o?{loading:!0}:{data:r,loading:!!t})}static getState(r){return r.data!==void 0&&r.loading?"refreshing":r.loading?"pending":r.error!==void 0?"errored":r.data!==void 0?"ready":"unresolved"}};var N=class extends Error{name="AbortError";code=20;constructor(r="The operation was aborted."){super(r)}};var T=class extends Error{name="NullishError"};import{useCallback as E,useEffect as k,useRef as S}from"react";import{useReducer as _}from"react";function P(e,r){return _(ee,[e,r],$)}function $(e){let[r,t=!1]=e,o=typeof r=="function"?r():r;return u.from(o,!t)}function ee(e,r){let t=r?.type;switch(t){case"PEND":return new u({loading:!0,error:void 0,data:e.data},e);case"RESOLVE":return new u({...e,loading:!1,error:void 0,data:r.payload},e);case"SYNC-RESULT":return new u({loading:!1,error:void 0,data:r.payload},e);case"REJECT":return new u({loading:!1,error:r.payload??new T("resource rejected with a nullish error",{cause:r.payload}),data:void 0},e);default:re(t)}}function re(e){throw new Error("Invalid action type")}function te(e,r=[],{initialValue:t,onCompleted:o,onError:l,skipFirstRun:n=!1,skip:w=!1,skipFnMemoization:C}={}){let a=S(),g=S(n),[v,d]=P(t,w||n),F=E(c=>{a.current?.abort(),a.current=new AbortController,d({type:"SYNC-RESULT",payload:c})},[d]),R=E((c,...I)=>{let p,y=a.current;try{if(y==null)throw new Error("resource state error, abort controller is null during the fetch operation");return p=e(...I,{signal:y.signal,refetching:c}),p instanceof Promise?z(p):d({type:"SYNC-RESULT",payload:p}),p}catch(h){if(d({type:"REJECT",payload:h}),c)throw h;return}async function z(h){d({type:"PEND"});try{let f=await h;if(y!==a.current)return;d({type:"RESOLVE",payload:f}),o?.(f)}catch(f){if(oe(f)||y!==a.current)return;d({type:"REJECT",payload:f}),l?.(f)}}},C?[e]:[]),L=E((...c)=>(a.current?.abort(),a.current=new AbortController,R(!0,...c)),[R]),D=E(c=>{a.current?.abort(c)},[]);return k(()=>{g.current=n,a.current||(a.current=new AbortController)},[n]),k(()=>{if(g.current){g.current=!1;return}if(!w)return R(!1,...r),()=>{a.current?.abort(),a.current=new AbortController}},[...r,w,R]),[v,{mutate:F,refetch:L,abort:D}]}function oe(e){return e!=null&&e.name==="AbortError"}export{N as AbortError,Z as Await,K as Dynamic,A as ErrorBoundary,V as For,j as Match,T as NullishError,W as Portal,u as Resource,B as Show,Y as Switch,te as useResource}; //# sourceMappingURL=lib.js.map