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 (1 loc) • 5.07 kB
JavaScript
(function(o,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("react"),require("react-dom")):typeof define=="function"&&define.amd?define(["exports","react","react-dom"],t):(o=typeof globalThis<"u"?globalThis:o||self,t(o["react-solid-flow"]={},o.React,o.ReactDOM))})(this,function(o,t,h){"use strict";var Q=Object.defineProperty;var W=(o,t,h)=>t in o?Q(o,t,{enumerable:!0,configurable:!0,writable:!0,value:h}):o[t]=h;var f=(o,t,h)=>(W(o,typeof t!="symbol"?t+"":t,h),h);function y(e){return e==null?null:t.isValidElement(e)?e:t.createElement(t.Fragment,null,e)}function D({children:e,each:r,fallback:n=null}){if(!Array.isArray(r)||!r.length||e==null)return y(n);if(typeof e!="function")return t.createElement(t.Fragment,null,r.map((u,l)=>t.createElement(t.Fragment,{key:l},e)));const a=[];for(let u=0;u<r.length;u++){const l=e(r[u],u);l!=null&&(!t.isValidElement(l)||!l.key?a.push(t.createElement(t.Fragment,{key:u},l)):a.push(l))}return a.length?t.createElement(t.Fragment,null,a):y(n)}function m(e,...r){return y(typeof e=="function"?e(...r):e)}function F({fallback:e=null,...r}){return r.when?m(r.children,r.when):y(e)}function P(e){for(const r of t.Children.toArray(e.children))if(t.isValidElement(r)&&r.props.when)return r;return y(e.fallback)}function k({when:e,children:r}){return e?m(r,e):null}class N extends t.Component{constructor(n){super(n);f(this,"state",{error:void 0});this.resetError=this.resetError.bind(this)}static getDerivedStateFromError(n){return{error:n}}componentDidCatch(n,a){var u,l;(l=(u=this.props).onCatch)==null||l.call(u,n,a),this.setState({error:n})}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}}const p=t.forwardRef,L=p(function({component:r,...n},a){return r?t.createElement(r,{...n,ref:a}):null});function V({mount:e,...r}){const n=e==null||e instanceof Element||e instanceof DocumentFragment?e:document.querySelector(e);return n?h.createPortal(r.children,n):null}function M(e){return e.for.loading?m(e.fallback):e.for.error!=null?m(e.catch,e.for.error):m(e.children,e.for.data)}class d{constructor(r,n){f(this,"loading");f(this,"data");f(this,"error");f(this,"state");f(this,"latest");this.data=r==null?void 0:r.data,this.error=r==null?void 0:r.error,this.loading=!!(r!=null&&r.loading),this.data!==void 0?this.latest=this.data:this.latest=n==null?void 0:n.latest,this.state=d.getState(this)}static from(r,n){const a=r instanceof Promise;return new d(a?{loading:!0}:{data:r,loading:!!n})}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"}}class O extends Error{constructor(n="The operation was aborted."){super(n);f(this,"name","AbortError");f(this,"code",20)}}class T extends Error{constructor(){super(...arguments);f(this,"name","NullishError")}}function j(e,r){return t.useReducer(J,[e,r],q)}function q(e){const[r,n=!1]=e,a=typeof r=="function"?r():r;return d.from(a,!n)}function J(e,r){switch(r==null?void 0:r.type){case"PEND":return new d({loading:!0,error:void 0,data:e.data},e);case"RESOLVE":return new d({...e,loading:!1,error:void 0,data:r.payload},e);case"SYNC-RESULT":return new d({loading:!1,error:void 0,data:r.payload},e);case"REJECT":return new d({loading:!1,error:r.payload??new T("resource rejected with a nullish error",{cause:r.payload}),data:void 0},e);default:return e}}function U(e,r=[],{initialValue:n,onCompleted:a,onError:u,skipFirstRun:l=!1,skip:v=!1,skipFnMemoization:B}={}){const i=t.useRef(),C=t.useRef(l),[z,E]=j(n,v||l),I=t.useCallback(s=>{var c;(c=i.current)==null||c.abort(),i.current=new AbortController,E({type:"SYNC-RESULT",payload:s})},[E]),b=t.useCallback((s,...c)=>{let w;const S=i.current;try{if(S==null)throw new Error("resource state error, abort controller is null during the fetch operation");return w=e(...c,{signal:S.signal,refetching:s}),w instanceof Promise?K(w):E({type:"SYNC-RESULT",payload:w}),w}catch(A){if(E({type:"REJECT",payload:A}),s)throw A;return}async function K(A){E({type:"PEND"});try{const g=await A;if(S!==i.current)return;E({type:"RESOLVE",payload:g}),a==null||a(g)}catch(g){if(Y(g)||S!==i.current)return;E({type:"REJECT",payload:g}),u==null||u(g)}}},B?[e]:[]),G=t.useCallback((...s)=>{var c;return(c=i.current)==null||c.abort(),i.current=new AbortController,b(!0,...s)},[b]),H=t.useCallback(s=>{var c;(c=i.current)==null||c.abort(s)},[]);return t.useEffect(()=>{C.current=l,i.current||(i.current=new AbortController)},[l]),t.useEffect(()=>{if(C.current){C.current=!1;return}if(!v)return b(!1,...r),()=>{var s;(s=i.current)==null||s.abort(),i.current=new AbortController}},[...r,v,b]),[z,{mutate:I,refetch:G,abort:H}]}function Y(e){return e!=null&&e.name==="AbortError"}o.AbortError=O,o.Await=M,o.Dynamic=L,o.ErrorBoundary=N,o.For=D,o.Match=k,o.NullishError=T,o.Portal=V,o.Resource=d,o.Show=F,o.Switch=P,o.useResource=U,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});