react-solid-flow
Advanced tools
Comparing version 0.2.0 to 0.2.1
module.exports = { | ||
"env": { | ||
"browser": true, | ||
"es2021": true | ||
"env": { | ||
"browser": true, | ||
"es2021": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:react/recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"settings": { | ||
"react": { | ||
"version": "detect" | ||
} | ||
}, | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaFeatures": { | ||
"jsx": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:react/recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaFeatures": { | ||
"jsx": true | ||
}, | ||
"ecmaVersion": "latest", | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"react", | ||
"@typescript-eslint" | ||
], | ||
"rules": { | ||
} | ||
"ecmaVersion": "latest", | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"react", | ||
"@typescript-eslint" | ||
], | ||
"ignorePatterns": [".eslintrc.js", "dist/**/*"], | ||
"rules": { | ||
"@typescript-eslint/no-non-null-assertion": "off", | ||
// we have a lot of any to be compatible with Solid api, where they're also used | ||
"@typescript-eslint/no-explicit-any": "off", | ||
quotes: ["error", "double"], | ||
semi: ["error", "always", { "omitLastInOneLineBlock": true }], | ||
"comma-dangle": "off", | ||
"@typescript-eslint/comma-dangle": ["warn", "always-multiline"], | ||
} | ||
} |
@@ -8,3 +8,12 @@ # managed-timeout changelog | ||
## [0.2.1] - 2023-01-24 | ||
### Changed | ||
- eslint check was added to the build-test pipeline, so it's required for lint to pass | ||
- Stricter error verification rules, resource error types were changed from any to unknown | ||
- jsx-runtime settings changed to react-jsx | ||
### Fixed | ||
- fixed or muted all the previous eslint warnings | ||
## [0.2.0] - 2023-01-23 | ||
First public release. |
import type { Resource } from "../models/Resource"; | ||
export declare type ResourceReturn<T, TArgs extends readonly any[]> = [ | ||
export declare type ResourceReturn<T, TArgs extends readonly unknown[]> = [ | ||
Resource<T>, | ||
@@ -4,0 +4,0 @@ { |
@@ -7,3 +7,3 @@ export interface ResourceLike<T> { | ||
/** Rejected resource error */ | ||
error: any; | ||
error: unknown; | ||
} | ||
@@ -14,3 +14,3 @@ export declare type ResourceState = "unresolved" | "pending" | "ready" | "refreshing" | "errored"; | ||
data: Awaited<T> | undefined; | ||
error: any; | ||
error: unknown; | ||
/** State name | ||
@@ -51,3 +51,3 @@ * | ||
*/ | ||
static getState(r: ResourceLike<any>): ResourceState; | ||
static getState(r: ResourceLike<unknown>): ResourceState; | ||
} |
@@ -1,1 +0,1 @@ | ||
(function(t,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("react/jsx-runtime"),require("react"),require("react-dom")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react","react-dom"],o):(t=typeof globalThis<"u"?globalThis:t||self,o(t["react-solid-flow"]={},t.jsxRuntime,t.React,t.ReactDOM))})(this,function(t,o,a,T){"use strict";var Q=Object.defineProperty;var W=(t,o,a)=>o in t?Q(t,o,{enumerable:!0,configurable:!0,writable:!0,value:a}):t[o]=a;var d=(t,o,a)=>(W(t,typeof o!="symbol"?o+"":o,a),a);function E(e){return e==null?null:a.isValidElement(e)?e:o.jsx(o.Fragment,{children:e})}function R({children:e,each:r,fallback:n=null}){if(!Array.isArray(r)||!r.length||e==null)return E(n);if(typeof e!="function")return o.jsx(o.Fragment,{children:r.map((s,l)=>o.jsx(a.Fragment,{children:e},l))});const u=[];for(let s=0;s<r.length;s++){const l=e(r[s],s);l!=null&&(!a.isValidElement(l)||!l.key?u.push(o.jsx(a.Fragment,{children:l},s)):u.push(l))}return u.length?o.jsx(o.Fragment,{children:u}):E(n)}function w(e,...r){return e instanceof Function?E(e(...r)):E(e)}function D({fallback:e=null,...r}){return r.when?w(r.children,r.when):E(e)}function P(e){for(const r of a.Children.toArray(e.children))if(a.isValidElement(r)&&r.props.when)return r;return E(e.fallback)}function N({when:e,children:r}){return e?w(r,e):null}class k extends a.Component{constructor(n){super(n);d(this,"state",{error:void 0});this.resetError=this.resetError.bind(this)}static getDerivedStateFromError(n){return{error:n}}componentDidCatch(n,u){var s,l;(l=(s=this.props).onCatch)==null||l.call(s,n,u),this.setState({error:n})}resetError(){this.setState({error:void 0})}render(){return this.state.error===void 0?this.props.children:this.props.fallback instanceof Function?this.props.fallback(this.state.error,this.resetError):this.props.fallback}}const L=a.forwardRef,V=L(function({component:r,...n},u){return r?o.jsx(r,{...n,ref:u}):null});function j({mount:e,...r}){const n=e==null||e instanceof Element||e instanceof DocumentFragment?e:document.querySelector(e);return n?T.createPortal(r.children,n):null}function q(e){return e.for.loading?w(e.fallback):e.for.error!=null?w(e.catch,e.for.error):w(e.children,e.for.data)}class h{constructor(r,n){d(this,"loading");d(this,"data");d(this,"error");d(this,"state");d(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=h.getState(this)}static from(r,n){const u=r instanceof Promise;return new h(u?{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 M extends Error{constructor(n="The operation was aborted."){super(n);d(this,"name","AbortError");d(this,"code",20)}}class F extends Error{constructor(){super(...arguments);d(this,"name","NullishError")}}function O(e,r){return a.useReducer(U,[e,r],J)}function J(e){const[r,n=!1]=e,u=r instanceof Function?r():r;return h.from(u,!n)}function U(e,r){switch(r==null?void 0:r.type){case"PEND":return new h({loading:!0,error:void 0,data:e.data},e);case"RESOLVE":return new h({...e,loading:!1,error:void 0,data:r.payload},e);case"SYNC-RESULT":return new h({loading:!1,error:void 0,data:r.payload},e);case"REJECT":return new h({loading:!1,error:r.payload??new F("resource rejected with a nullish error",{cause:r.payload}),data:void 0},e);default:return e}}function Y(e,r=[],{initialValue:n,onCompleted:u,onError:s,skipFirstRun:l=!1,skip:v=!1,skipFnMemoization:B}={}){const i=a.useRef(),C=a.useRef(l),[z,y]=O(n,v||l),I=a.useCallback(c=>{var f;(f=i.current)==null||f.abort(),i.current=new AbortController,y({type:"SYNC-RESULT",payload:c})},[y]),m=a.useCallback((c,...f)=>{let b;const S=i.current;try{return b=e(...f,{signal:S==null?void 0:S.signal,refetching:c}),b instanceof Promise?K(b):y({type:"SYNC-RESULT",payload:b}),b}catch(A){if(y({type:"REJECT",payload:A}),c)throw A;return}async function K(A){y({type:"PEND"});try{const g=await A;if(S!==i.current)return;y({type:"RESOLVE",payload:g}),u==null||u(g)}catch(g){if(p(g)||S!==i.current)return;y({type:"REJECT",payload:g}),s==null||s(g)}}},B?[e]:[]),G=a.useCallback((...c)=>{var f;return(f=i.current)==null||f.abort(),i.current=new AbortController,m(!0,...c)},[m]),H=a.useCallback(c=>{var f;(f=i.current)==null||f.abort(c)},[]);return a.useEffect(()=>{C.current=l,i.current||(i.current=new AbortController)},[l]),a.useEffect(()=>{if(C.current){C.current=!1;return}if(!v)return m(!1,...r),()=>{var c;(c=i.current)==null||c.abort(),i.current=new AbortController}},[...r,v,m]),[z,{mutate:I,refetch:G,abort:H}]}function p(e){return e!=null&&e.name==="AbortError"}t.AbortError=M,t.Await=q,t.Dynamic=V,t.ErrorBoundary=k,t.For=R,t.Match=N,t.NullishError=F,t.Portal=j,t.Resource=h,t.Show=D,t.Switch=P,t.useResource=Y,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}); | ||
(function(t,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("react/jsx-runtime"),require("react"),require("react-dom")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react","react-dom"],a):(t=typeof globalThis<"u"?globalThis:t||self,a(t["react-solid-flow"]={},t.jsxRuntime,t.React,t.ReactDOM))})(this,function(t,a,o,T){"use strict";var Q=Object.defineProperty;var W=(t,a,o)=>a in t?Q(t,a,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[a]=o;var d=(t,a,o)=>(W(t,typeof a!="symbol"?a+"":a,o),o);function E(e){return e==null?null:o.isValidElement(e)?e:a.jsx(a.Fragment,{children:e})}function R({children:e,each:r,fallback:n=null}){if(!Array.isArray(r)||!r.length||e==null)return E(n);if(typeof e!="function")return a.jsx(a.Fragment,{children:r.map((s,l)=>a.jsx(o.Fragment,{children:e},l))});const u=[];for(let s=0;s<r.length;s++){const l=e(r[s],s);l!=null&&(!o.isValidElement(l)||!l.key?u.push(a.jsx(o.Fragment,{children:l},s)):u.push(l))}return u.length?a.jsx(a.Fragment,{children:u}):E(n)}function w(e,...r){return e instanceof Function?E(e(...r)):E(e)}function D({fallback:e=null,...r}){return r.when?w(r.children,r.when):E(e)}function P(e){for(const r of o.Children.toArray(e.children))if(o.isValidElement(r)&&r.props.when)return r;return E(e.fallback)}function N({when:e,children:r}){return e?w(r,e):null}class k extends o.Component{constructor(n){super(n);d(this,"state",{error:void 0});this.resetError=this.resetError.bind(this)}static getDerivedStateFromError(n){return{error:n}}componentDidCatch(n,u){var s,l;(l=(s=this.props).onCatch)==null||l.call(s,n,u),this.setState({error:n})}resetError(){this.setState({error:void 0})}render(){return this.state.error===void 0?this.props.children:this.props.fallback instanceof Function?this.props.fallback(this.state.error,this.resetError):this.props.fallback}}const L=o.forwardRef,V=L(function({component:r,...n},u){return r?a.jsx(r,{...n,ref:u}):null});function j({mount:e,...r}){const n=e==null||e instanceof Element||e instanceof DocumentFragment?e:document.querySelector(e);return n?T.createPortal(r.children,n):null}function q(e){return e.for.loading?w(e.fallback):e.for.error!=null?w(e.catch,e.for.error):w(e.children,e.for.data)}class h{constructor(r,n){d(this,"loading");d(this,"data");d(this,"error");d(this,"state");d(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=h.getState(this)}static from(r,n){const u=r instanceof Promise;return new h(u?{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 M extends Error{constructor(n="The operation was aborted."){super(n);d(this,"name","AbortError");d(this,"code",20)}}class F extends Error{constructor(){super(...arguments);d(this,"name","NullishError")}}function O(e,r){return o.useReducer(J,[e,r],p)}function p(e){const[r,n=!1]=e,u=r instanceof Function?r():r;return h.from(u,!n)}function J(e,r){switch(r==null?void 0:r.type){case"PEND":return new h({loading:!0,error:void 0,data:e.data},e);case"RESOLVE":return new h({...e,loading:!1,error:void 0,data:r.payload},e);case"SYNC-RESULT":return new h({loading:!1,error:void 0,data:r.payload},e);case"REJECT":return new h({loading:!1,error:r.payload??new F("resource rejected with a nullish error",{cause:r.payload}),data:void 0},e);default:return e}}function U(e,r=[],{initialValue:n,onCompleted:u,onError:s,skipFirstRun:l=!1,skip:v=!1,skipFnMemoization:B}={}){const i=o.useRef(),C=o.useRef(l),[z,y]=O(n,v||l),I=o.useCallback(c=>{var f;(f=i.current)==null||f.abort(),i.current=new AbortController,y({type:"SYNC-RESULT",payload:c})},[y]),S=o.useCallback((c,...f)=>{let b;const m=i.current;try{if(m==null)throw new Error("resource state error, abort controller is null during the fetch operation");return b=e(...f,{signal:m.signal,refetching:c}),b instanceof Promise?K(b):y({type:"SYNC-RESULT",payload:b}),b}catch(A){if(y({type:"REJECT",payload:A}),c)throw A;return}async function K(A){y({type:"PEND"});try{const g=await A;if(m!==i.current)return;y({type:"RESOLVE",payload:g}),u==null||u(g)}catch(g){if(Y(g)||m!==i.current)return;y({type:"REJECT",payload:g}),s==null||s(g)}}},B?[e]:[]),G=o.useCallback((...c)=>{var f;return(f=i.current)==null||f.abort(),i.current=new AbortController,S(!0,...c)},[S]),H=o.useCallback(c=>{var f;(f=i.current)==null||f.abort(c)},[]);return o.useEffect(()=>{C.current=l,i.current||(i.current=new AbortController)},[l]),o.useEffect(()=>{if(C.current){C.current=!1;return}if(!v)return S(!1,...r),()=>{var c;(c=i.current)==null||c.abort(),i.current=new AbortController}},[...r,v,S]),[z,{mutate:I,refetch:G,abort:H}]}function Y(e){return e!=null&&e.name==="AbortError"}t.AbortError=M,t.Await=q,t.Dynamic=V,t.ErrorBoundary=k,t.For=R,t.Match=N,t.NullishError=F,t.Portal=j,t.Resource=h,t.Show=D,t.Switch=P,t.useResource=U,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}); |
{ | ||
"name": "react-solid-flow", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"license": "MIT", | ||
"scripts": { | ||
@@ -8,3 +9,4 @@ "dev": "vite", | ||
"preview": "vite preview", | ||
"test": "vitest --run --coverage" | ||
"test": "vitest --run --coverage", | ||
"lint": "eslint ." | ||
}, | ||
@@ -16,4 +18,4 @@ "repository": { | ||
"peerDependencies": { | ||
"react": ">=16.8", | ||
"react-dom": ">=16.8" | ||
"react": ">=16.14", | ||
"react-dom": ">=16.14" | ||
}, | ||
@@ -20,0 +22,0 @@ "devDependencies": { |
@@ -13,3 +13,3 @@ # react-solid-flow | ||
- Zero third-party dependencies, except React and React-DOM | ||
- Modern: React 16.8+ .. 18.x, no legacy APIs or weird hacks | ||
- Modern: React 16.14+ .. 18.x, no legacy APIs or weird hacks | ||
- Fully tested | ||
@@ -144,3 +144,3 @@ - Easy to use | ||
data: Awaited<T> | undefined; | ||
error: any; | ||
error: unknown; | ||
} | ||
@@ -293,3 +293,3 @@ | ||
data: Awaited<T> | undefined; | ||
error: any; | ||
error: unknown; | ||
latest: Awaited<T> | undefined; | ||
@@ -300,3 +300,3 @@ state: ResourceState; | ||
static from<T>(data: Promise<T> | Awaited<T> | undefined): Resource<T>; | ||
static getState(r: ResourceLike<any>): ResourceState; | ||
static getState(r: ResourceLike<unknown>): ResourceState; | ||
} | ||
@@ -303,0 +303,0 @@ |
@@ -21,3 +21,3 @@ import { it, describe, beforeAll, afterAll, expect, vi } from "vitest"; | ||
const controller = new AbortController(); | ||
const prms = pause(t, { signal: controller.signal }) | ||
const prms = pause(t, { signal: controller.signal }); | ||
controller.abort(); | ||
@@ -24,0 +24,0 @@ await expect(prms).rejects.toMatchObject({ name: "AbortError" }); |
@@ -23,4 +23,4 @@ interface PauseOpts { | ||
} | ||
rej(this.reason) | ||
}; | ||
rej(this.reason); | ||
} | ||
@@ -27,0 +27,0 @@ if (signal?.addEventListener instanceof Function) { |
@@ -15,3 +15,3 @@ import { describe, it, expect } from "vitest"; | ||
expect(resource.latest).toBeUndefined(); | ||
expect(resource.state).toBe('pending'); | ||
expect(resource.state).toBe("pending"); | ||
expect(dispatch).toBeInstanceOf(Function); | ||
@@ -25,3 +25,3 @@ }); | ||
expect(resource.data).toBe(true); | ||
expect(resource.state).toBe('refreshing'); | ||
expect(resource.state).toBe("refreshing"); | ||
}); | ||
@@ -34,3 +34,3 @@ | ||
expect(resource.data).toBe(false); | ||
expect(resource.state).toBe('refreshing'); | ||
expect(resource.state).toBe("refreshing"); | ||
}); | ||
@@ -47,3 +47,3 @@ | ||
expect(resource.latest).toBeUndefined(); | ||
expect(resource.state).toBe('pending'); | ||
expect(resource.state).toBe("pending"); | ||
}); | ||
@@ -63,3 +63,3 @@ | ||
expect(resource.latest).toBe(true); | ||
expect(resource.state).toBe('ready'); | ||
expect(resource.state).toBe("ready"); | ||
}); | ||
@@ -80,3 +80,3 @@ | ||
expect(resource.latest).toBe(true); | ||
expect(resource.state).toBe('refreshing'); | ||
expect(resource.state).toBe("refreshing"); | ||
}); | ||
@@ -96,3 +96,3 @@ | ||
expect(resource.latest).toBeUndefined(); | ||
expect(resource.state).toBe('errored'); | ||
expect(resource.state).toBe("errored"); | ||
}); | ||
@@ -107,6 +107,7 @@ | ||
const [ resource ] = result.current; | ||
expect(resource.error).toBeInstanceOf(NullishError); | ||
const { error } = resource; | ||
expect(error).toBeInstanceOf(NullishError); | ||
// As error object doesn't have a cause field in node <= 14, don't test for that | ||
if (process.version.slice(1).split('.')[0] > '14') { | ||
expect(resource.error.cause).toBeNull(); | ||
if (process.version.slice(1).split(".")[0] > "14") { | ||
expect(error instanceof NullishError && error.cause).toBeNull(); | ||
} | ||
@@ -113,0 +114,0 @@ }); |
@@ -5,3 +5,3 @@ import { useCallback, useEffect, useRef } from "react"; | ||
export type ResourceReturn<T, TArgs extends readonly any[]> = [ | ||
export type ResourceReturn<T, TArgs extends readonly unknown[]> = [ | ||
Resource<T>, | ||
@@ -30,3 +30,3 @@ { | ||
abort: (reason?: any) => void; | ||
} | ||
}, | ||
]; | ||
@@ -74,3 +74,3 @@ | ||
skipFnMemoization, | ||
}: ResourceOptions<T> = {} | ||
}: ResourceOptions<T> = {}, | ||
): ResourceReturn<T, TArgs> { | ||
@@ -87,3 +87,3 @@ // it's actually initialized in the effect bellow, so we don't create empty controllers | ||
controller.current = new AbortController(); | ||
dispatch({ type: "SYNC-RESULT", payload: val }) | ||
dispatch({ type: "SYNC-RESULT", payload: val }); | ||
}, [dispatch]); | ||
@@ -96,4 +96,8 @@ | ||
try { | ||
// in theory, this error should never happen, but better be on the safe side | ||
if (cont == null) { | ||
throw new Error("resource state error, abort controller is null during the fetch operation"); | ||
} | ||
val = fetcher(...args, { | ||
signal: cont?.signal!, | ||
signal: cont.signal, | ||
refetching, | ||
@@ -122,8 +126,8 @@ }); | ||
// instance hasn't changed between calls. | ||
if (cont !== controller.current) { return; } | ||
if (cont !== controller.current) { return } | ||
dispatch({ type: "RESOLVE", payload: result }); | ||
onCompleted?.(result); | ||
} catch (e) { | ||
if (isAbortError(e)) { return; } | ||
if (cont !== controller.current) { return; } | ||
if (isAbortError(e)) { return } | ||
if (cont !== controller.current) { return } | ||
dispatch({ type: "REJECT", payload: e }); | ||
@@ -134,3 +138,3 @@ onError?.(e); | ||
}, | ||
skipFnMemoization ? [ fetcher ] : [] | ||
skipFnMemoization ? [ fetcher ] : [], | ||
); | ||
@@ -168,5 +172,5 @@ | ||
controller.current = new AbortController(); | ||
} | ||
// onCompleted and onError are intentionally ommited, as we don't want to | ||
// retrigger the fetching, if someone forgot to memoize it | ||
}; | ||
// onCompleted and onError are intentionally ommited, as we don't want to | ||
// retrigger the fetching, if someone forgot to memoize it | ||
}, [ ...deps, skip, fetcherFn ]); | ||
@@ -177,3 +181,3 @@ | ||
function isAbortError(e: any): boolean { | ||
function isAbortError(e: any): e is { name: "AbortError" } { | ||
// We can't really check if it's an instanceof DOMException as it doesn't | ||
@@ -180,0 +184,0 @@ // exist in older node version, and we can't check if it's an instanceof |
@@ -7,8 +7,8 @@ import { Resource } from "../models/Resource"; | ||
initialValue?: Awaited<T> | (() => Awaited<T>), | ||
skipFirstRun?: boolean | ||
skipFirstRun?: boolean, | ||
) { | ||
return useReducer( | ||
resourceReducer as Reducer<Resource<T>, Action<T>>, | ||
[ initialValue, skipFirstRun ] as const, | ||
resourceInitializer, | ||
resourceReducer as Reducer<Resource<T>, Action<T>>, | ||
[ initialValue, skipFirstRun ] as const, | ||
resourceInitializer, | ||
); | ||
@@ -18,4 +18,4 @@ } | ||
function resourceInitializer<T>(init: readonly [ | ||
val: Awaited<T> | (() => Awaited<T>) | undefined, | ||
skip: boolean | undefined, | ||
val: Awaited<T> | (() => Awaited<T>) | undefined, | ||
skip: boolean | undefined, | ||
]): Resource<T> { | ||
@@ -28,40 +28,40 @@ const [ val, skip = false] = init; | ||
type Action<T> = | ||
| { type: "PEND" /* -> "pending" | "refreshing" */ } | ||
| { type: "RESOLVE" /* -> "ready" */, payload: Awaited<T> } | ||
| { type: "SYNC-RESULT" /* -> "ready" */, payload: Awaited<T> } | ||
| { type: "REJECT" /* -> "errored" */, payload: any } | ||
| { type: "PEND" /* -> "pending" | "refreshing" */ } | ||
| { type: "RESOLVE" /* -> "ready" */, payload: Awaited<T> } | ||
| { type: "SYNC-RESULT" /* -> "ready" */, payload: Awaited<T> } | ||
| { type: "REJECT" /* -> "errored" */, payload: any } | ||
export function resourceReducer<T>(resource: Resource<T>, action: Action<T>): Resource<T> { | ||
switch (action?.type) { | ||
case "PEND": | ||
return new Resource({ | ||
loading: true, | ||
error: undefined, | ||
data: resource.data, | ||
}, resource); | ||
case "RESOLVE": | ||
return new Resource({ | ||
...resource, | ||
loading: false, | ||
error: undefined, | ||
data: action.payload, | ||
}, resource); | ||
case "SYNC-RESULT": | ||
return new Resource({ | ||
loading: false, | ||
error: undefined, | ||
data: action.payload, | ||
}, resource); | ||
case "REJECT": | ||
return new Resource<T>({ | ||
loading: false, | ||
error: action.payload ?? new NullishError( | ||
"resource rejected with a nullish error", | ||
{ cause: action.payload } | ||
), | ||
data: undefined, | ||
}, resource); | ||
default: | ||
return resource; | ||
case "PEND": | ||
return new Resource({ | ||
loading: true, | ||
error: undefined, | ||
data: resource.data, | ||
}, resource); | ||
case "RESOLVE": | ||
return new Resource({ | ||
...resource, | ||
loading: false, | ||
error: undefined, | ||
data: action.payload, | ||
}, resource); | ||
case "SYNC-RESULT": | ||
return new Resource({ | ||
loading: false, | ||
error: undefined, | ||
data: action.payload, | ||
}, resource); | ||
case "REJECT": | ||
return new Resource<T>({ | ||
loading: false, | ||
error: action.payload ?? new NullishError( | ||
"resource rejected with a nullish error", | ||
{ cause: action.payload }, | ||
), | ||
data: undefined, | ||
}, resource); | ||
default: | ||
return resource; | ||
} | ||
} |
@@ -18,3 +18,3 @@ import { describe, expect, it, vi } from "vitest"; | ||
it("correctly sets core data", () => { | ||
let res = new Resource({ | ||
const res = new Resource({ | ||
data: "test1", | ||
@@ -75,3 +75,3 @@ loading: true, | ||
latest: "test", | ||
state: "ready" | ||
state: "ready", | ||
}); | ||
@@ -82,3 +82,3 @@ expect(res).toBeInstanceOf(Resource); | ||
it("creates a resource with async data", () => { | ||
const res = Resource.from(new Promise(() => {})); | ||
const res = Resource.from(Promise.resolve()); | ||
expect(res).toEqual({ | ||
@@ -85,0 +85,0 @@ data: undefined, |
@@ -9,5 +9,5 @@ /* | ||
constructor(message: string = "The operation was aborted.") { | ||
constructor(message = "The operation was aborted.") { | ||
super(message); | ||
} | ||
} |
@@ -8,3 +8,3 @@ | ||
/** Rejected resource error */ | ||
error: any; | ||
error: unknown; | ||
} | ||
@@ -17,3 +17,3 @@ | ||
data: Awaited<T> | undefined; | ||
error: any; | ||
error: unknown; | ||
@@ -55,3 +55,3 @@ /** State name | ||
return new Resource(isAsync ? { | ||
loading: true | ||
loading: true, | ||
} : { | ||
@@ -77,3 +77,3 @@ data, | ||
*/ | ||
static getState(r: ResourceLike<any>): ResourceState { | ||
static getState(r: ResourceLike<unknown>): ResourceState { | ||
if (r.data !== undefined && r.loading) { | ||
@@ -80,0 +80,0 @@ return "refreshing"; |
@@ -1,8 +0,8 @@ | ||
import { afterEach } from 'vitest'; | ||
import matchers from '@testing-library/jest-dom/matchers'; | ||
import { afterEach } from "vitest"; | ||
import matchers from "@testing-library/jest-dom/matchers"; | ||
import { cleanup } from "@testing-library/react"; | ||
import { expect } from 'vitest'; | ||
import { expect } from "vitest"; | ||
// see: https://github.com/nknapp/vitest-react-18-testing-library-missing-act-workaround/blob/master/src/setupVitest.js | ||
globalThis.IS_REACT_ACT_ENVIRONMENT = true | ||
globalThis.IS_REACT_ACT_ENVIRONMENT = true; | ||
@@ -9,0 +9,0 @@ afterEach(() => cleanup()); |
@@ -30,3 +30,3 @@ /// <reference types="vitest" /> | ||
"react/jsx-runtime", | ||
"react-dom" | ||
"react-dom", | ||
], | ||
@@ -42,2 +42,2 @@ output: { | ||
}, | ||
}) | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
104063
2284