reactive-query-z 2.0.0 → 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/build/hooks/types.d.ts +30 -0
- package/build/hooks/useGraphQLMutation.d.ts +1 -10
- package/build/hooks/useGraphQLQuery.d.ts +1 -14
- package/build/hooks/useHybridQuery.d.ts +2 -14
- package/build/hooks/useMutation.d.ts +1 -10
- package/build/hooks/useQuery.d.ts +1 -13
- package/build/index.cjs +1 -1
- package/build/index.d.ts +2 -1
- package/build/index.esm.js +1 -1
- package/build/index.umd.min.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
interface BaseOptions<T = any> {
|
|
2
|
+
cacheKey?: string;
|
|
3
|
+
headers?: Record<string, string>;
|
|
4
|
+
timeout?: number;
|
|
5
|
+
retry?: number;
|
|
6
|
+
retryDelay?: number;
|
|
7
|
+
optimisticUpdate?: (prevData: T | null, newData: T) => T;
|
|
8
|
+
}
|
|
9
|
+
export interface QueryOptions<T> extends BaseOptions<T> {
|
|
10
|
+
variables?: Record<string, any>;
|
|
11
|
+
staleTime?: number;
|
|
12
|
+
autoFetch?: boolean;
|
|
13
|
+
prefetch?: boolean;
|
|
14
|
+
method?: "GET" | "POST";
|
|
15
|
+
}
|
|
16
|
+
export interface GraphQueryOptions<T> extends QueryOptions<T> {
|
|
17
|
+
query: string;
|
|
18
|
+
}
|
|
19
|
+
export interface HybridQueryOptions<T> extends QueryOptions<T> {
|
|
20
|
+
subscriptionUrl?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface MutationOptions<T> extends BaseOptions<T> {
|
|
23
|
+
onSuccess?: (data: T) => void;
|
|
24
|
+
onError?: (error: any) => void;
|
|
25
|
+
}
|
|
26
|
+
export interface GraphQLMutationOptions<TData, TVariables = any> extends MutationOptions<TData> {
|
|
27
|
+
mutation: string;
|
|
28
|
+
variables?: TVariables;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
mutation: string;
|
|
3
|
-
variables?: TVariables;
|
|
4
|
-
cacheKey?: string;
|
|
5
|
-
retry?: number;
|
|
6
|
-
retryDelay?: number;
|
|
7
|
-
headers?: Record<string, string>;
|
|
8
|
-
timeout?: number;
|
|
9
|
-
optimisticUpdate?: (prev: TData | undefined, newData: TData) => TData;
|
|
10
|
-
}
|
|
1
|
+
import { GraphQLMutationOptions } from "./types";
|
|
11
2
|
export declare function useGraphQLMutation<TData, TVariables = any>(endpoint: string, options: GraphQLMutationOptions<TData, TVariables>): {
|
|
12
3
|
mutate: () => Promise<TData>;
|
|
13
4
|
cancel: () => void;
|
|
@@ -1,18 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
|
|
3
|
-
query: string;
|
|
4
|
-
variables?: Record<string, any>;
|
|
5
|
-
cacheKey?: string;
|
|
6
|
-
staleTime?: number;
|
|
7
|
-
headers?: Record<string, string>;
|
|
8
|
-
timeout?: number;
|
|
9
|
-
retry?: number;
|
|
10
|
-
retryDelay?: number;
|
|
11
|
-
autoFetch?: boolean;
|
|
12
|
-
prefetch?: boolean;
|
|
13
|
-
method?: "GET" | "POST";
|
|
14
|
-
optimisticUpdate?: (prevData: T | null, newData: T) => T;
|
|
15
|
-
}
|
|
2
|
+
import { GraphQueryOptions } from "./types";
|
|
16
3
|
export declare function useGraphQLQuery<T>(endpoint: string, options: GraphQueryOptions<T>): {
|
|
17
4
|
data: T | null;
|
|
18
5
|
error: Error | null;
|
|
@@ -1,23 +1,10 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
|
|
3
|
-
cacheKey?: string;
|
|
4
|
-
optimisticUpdate?: (prevData: T | null, newData: T) => T;
|
|
5
|
-
staleTime?: number;
|
|
6
|
-
subscriptionUrl?: string;
|
|
7
|
-
retry?: number;
|
|
8
|
-
retryDelay?: number;
|
|
9
|
-
headers?: Record<string, string>;
|
|
10
|
-
timeout?: number;
|
|
11
|
-
autoFetch?: boolean;
|
|
12
|
-
prefetch?: boolean;
|
|
13
|
-
method?: "GET" | "POST";
|
|
14
|
-
}
|
|
2
|
+
import { HybridQueryOptions } from "./types";
|
|
15
3
|
export interface HybridQueryParams<T> {
|
|
16
4
|
query?: string;
|
|
17
5
|
variables?: Record<string, any>;
|
|
18
6
|
options?: HybridQueryOptions<T>;
|
|
19
7
|
}
|
|
20
|
-
export declare function prefetchQuery<T>(endpoint: string, query?: string, variables?: Record<string, any>, cacheKey?: string, headers?: Record<string, string>, method?: "GET" | "POST"): Promise<T | null>;
|
|
21
8
|
export declare function useHybridQuery<T>(endpoint: string, { query, variables, options }: HybridQueryParams<T>): {
|
|
22
9
|
data: T | null;
|
|
23
10
|
error: Error | null;
|
|
@@ -26,3 +13,4 @@ export declare function useHybridQuery<T>(endpoint: string, { query, variables,
|
|
|
26
13
|
mutate: import("react").Dispatch<import("react").SetStateAction<T | null>>;
|
|
27
14
|
cancel: () => void | undefined;
|
|
28
15
|
};
|
|
16
|
+
export declare function prefetchQuery<T>(endpoint: string, { query, variables, options }: HybridQueryParams<T>): Promise<T | null>;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
cacheKey?: string;
|
|
3
|
-
optimisticUpdate?: (prevData: any, newData: T) => any;
|
|
4
|
-
onSuccess?: (data: T) => void;
|
|
5
|
-
onError?: (error: any) => void;
|
|
6
|
-
retry?: number;
|
|
7
|
-
retryDelay?: number;
|
|
8
|
-
headers?: Record<string, string>;
|
|
9
|
-
timeout?: number;
|
|
10
|
-
}
|
|
1
|
+
import { MutationOptions } from "./types";
|
|
11
2
|
export declare function useMutation<T = any>(endpoint: string, options?: MutationOptions<T>): {
|
|
12
3
|
mutate: (body: any, method?: "POST" | "PUT" | "PATCH" | "DELETE") => Promise<T>;
|
|
13
4
|
loading: boolean;
|
|
@@ -1,17 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
|
|
3
|
-
variables?: Record<string, any>;
|
|
4
|
-
cacheKey?: string;
|
|
5
|
-
staleTime?: number;
|
|
6
|
-
headers?: Record<string, string>;
|
|
7
|
-
timeout?: number;
|
|
8
|
-
retry?: number;
|
|
9
|
-
retryDelay?: number;
|
|
10
|
-
autoFetch?: boolean;
|
|
11
|
-
prefetch?: boolean;
|
|
12
|
-
method?: "GET" | "POST";
|
|
13
|
-
optimisticUpdate?: (prevData: T | null, newData: T) => T;
|
|
14
|
-
}
|
|
2
|
+
import { QueryOptions } from "./types";
|
|
15
3
|
export declare function useQuery<T>(endpoint: string, options: QueryOptions<T>): {
|
|
16
4
|
data: T | null;
|
|
17
5
|
error: Error | null;
|
package/build/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=function(){return t=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},t.apply(this,arguments)};function r(e,t,r,n){return new(r||(r=Promise))(function(i,a){function c(e){try{o(n.next(e))}catch(e){a(e)}}function u(e){try{o(n.throw(e))}catch(e){a(e)}}function o(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(c,u)}o((n=n.apply(e,t||[])).next())})}function n(e,t){var r,n,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},c=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return c.next=u(0),c.throw=u(1),c.return=u(2),"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function u(u){return function(o){return function(u){if(r)throw new TypeError("Generator is already executing.");for(;c&&(c=0,u[0]&&(a=0)),a;)try{if(r=1,n&&(i=2&u[0]?n.return:u[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,u[1])).done)return i;switch(n=0,i&&(u=[2&u[0],i.value]),u[0]){case 0:case 1:i=u;break;case 4:return a.label++,{value:u[1],done:!1};case 5:a.label++,n=u[1],u=[0];continue;case 7:u=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==u[0]&&2!==u[0])){a=0;continue}if(3===u[0]&&(!i||u[1]>i[0]&&u[1]<i[3])){a.label=u[1];break}if(6===u[0]&&a.label<i[1]){a.label=i[1],i=u;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(u);break}i[2]&&a.ops.pop(),a.trys.pop();continue}u=t.call(e,a)}catch(e){u=[6,e],n=0}finally{r=i=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,o])}}}"function"==typeof SuppressedError&&SuppressedError;var i=new Map,a={get:function(e){var t=i.get(e);if(t)return t.data},set:function(e,t){i.set(e,{data:t,timestamp:Date.now()})},delete:function(e){i.delete(e)},clear:function(){return i.clear()}},c=new Map;function u(e,t){return void 0===t&&(t={}),r(this,void 0,void 0,function(){var i,u,o,s=this;return n(this,function(l){return i=t.cacheKey||e,c.has(i)?[2,c.get(i)]:(u=a.get(i))?[2,u]:(o=fetch(e,t).then(function(e){return r(s,void 0,void 0,function(){var t;return n(this,function(r){switch(r.label){case 0:if(!e.ok)throw new Error(e.statusText);return[4,e.json()];case 1:return t=r.sent(),a.set(i,t),[2,t]}})})}).finally(function(){return c.delete(i)}),c.set(i,o),[2,o])})})}var o=new Map,s={register:function(e,t,r,n,i){o.set(e,{cacheKey:e,refetch:t,setData:r,cancel:n,cache:i})},getCache:function(e){var t;return null===(t=o.get(e))||void 0===t?void 0:t.cache},setCache:function(e,t){var r,n=o.get(e);n&&(n.cache=t,null===(r=n.setData)||void 0===r||r.call(n,function(){return t}))},setData:function(e,t){var r,n=o.get(e);if(n){var i=t(n.cache);n.cache=i,null===(r=n.setData)||void 0===r||r.call(n,function(){return i})}},cancel:function(e){var t,r;null===(r=null===(t=o.get(e))||void 0===t?void 0:t.cancel)||void 0===r||r.call(t)},invalidate:function(e){var t;e?null===(t=o.get(e))||void 0===t||t.refetch():o.forEach(function(e){return e.refetch()})},unregister:function(e){var t,r;null===(r=null===(t=o.get(e))||void 0===t?void 0:t.cancel)||void 0===r||r.call(t),o.delete(e)}},l=function(){function e(e){this.url=e,this.callbacks=new Set,this.ws=null}return e.prototype.connect=function(){var e=this;this.ws||(this.ws=new WebSocket(this.url),this.ws.onmessage=function(t){var r=JSON.parse(t.data);e.callbacks.forEach(function(e){return e(r)})})},e.prototype.subscribe=function(e){var t=this;return this.callbacks.add(e),this.connect(),function(){return t.callbacks.delete(e)}},e.prototype.disconnect=function(){var e;null===(e=this.ws)||void 0===e||e.close(),this.ws=null,this.callbacks.clear()},e}();function h(e,t,i){return r(this,void 0,void 0,function(){var r;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,4]),[4,e()];case 1:return[2,n.sent()];case 2:if(r=n.sent(),t<=0)throw r;return[4,new Promise(function(e){return setTimeout(e,i)})];case 3:return n.sent(),[2,h(e,t-1,2*i)];case 4:return[2]}})})}function d(e){var t=new AbortController,r=setTimeout(function(){return t.abort()},e);return t.signal.addEventListener("abort",function(){return clearTimeout(r)}),t.signal}function f(e,t,r){e&&(s.setData(e,function(e){return t?t(e,r):r}),s.invalidate(e))}function v(e,t,i,a,c,u){return r(this,void 0,void 0,function(){var r,o;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,y(e,t,i,{headers:c,cacheKey:a,method:u})];case 1:return r=n.sent(),a&&s.setCache(a,r),[2,r];case 2:return o=n.sent(),console.error("Prefetch failed",o),[2,null];case 3:return[2]}})})}function p(t,i){var a=this,c=i.query,u=i.variables,o=i.options,p=void 0===o?{}:o,b=e.useState(null),m=b[0],w=b[1],g=e.useState(null),K=g[0],S=g[1],x=e.useState(!1),C=x[0],T=x[1],k=e.useRef(null),U=e.useCallback(function(){return r(a,void 0,void 0,function(){var e,i,a,o,s,l=this;return n(this,function(v){switch(v.label){case 0:T(!0),S(null),null===(o=k.current)||void 0===o||o.abort(),e=new AbortController,k.current=e,i=d(null!==(s=p.timeout)&&void 0!==s?s:1e4),a=function(e,d){return void 0===e&&(e=null!==(o=p.retry)&&void 0!==o?o:3),void 0===d&&(d=null!==(s=p.retryDelay)&&void 0!==s?s:500),r(l,void 0,void 0,function(){var r,o;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,y(t,c,u,{headers:p.headers,cacheKey:p.cacheKey,method:p.method,signal:i})];case 1:return r=n.sent(),w(function(e){return p.optimisticUpdate?p.optimisticUpdate(e,r):r}),f(p.cacheKey,p.optimisticUpdate,r),[2,r];case 2:if("AbortError"===(o=n.sent()).name)throw o;return[2,h(function(){return a(e-1,2*d)},e-1,2*d)];case 3:return[2]}})})},v.label=1;case 1:return v.trys.push([1,,3,4]),[4,a()];case 2:return[2,v.sent()];case 3:return T(!1),[7];case 4:return[2]}})})},[t,c,u,p.cacheKey,p.optimisticUpdate,p.retry,p.retryDelay,p.headers,p.timeout,p.method]),D=e.useCallback(function(){var e;return null===(e=k.current)||void 0===e?void 0:e.abort()},[]);return e.useEffect(function(){var e=!0;return r(a,void 0,void 0,function(){var r;return n(this,function(n){switch(n.label){case 0:return p.prefetch&&p.cacheKey?(r=s.getCache(p.cacheKey))?(w(r),[3,3]):[3,1]:[3,3];case 1:return[4,v(t,c,u,p.cacheKey,p.headers,p.method)];case 2:n.sent(),n.label=3;case 3:return p.autoFetch&&e&&U(),[2]}})}),p.cacheKey&&s.register(p.cacheKey,U,w,D),function(){p.cacheKey&&s.unregister(p.cacheKey),D(),e=!1}},[t,c,u,p.prefetch,p.autoFetch,p.cacheKey,p.method]),e.useEffect(function(){if(p.staleTime){var e=setInterval(U,p.staleTime);return function(){return clearInterval(e)}}},[U,p.staleTime]),e.useEffect(function(){if(p.subscriptionUrl){var e=new l(p.subscriptionUrl).subscribe(function(e){w(function(t){return p.optimisticUpdate?p.optimisticUpdate(t,e):e}),f(p.cacheKey,p.optimisticUpdate,e)});return function(){return e()}}},[p.subscriptionUrl,p.optimisticUpdate,p.cacheKey]),{data:m,error:K,loading:C,refetch:U,mutate:w,cancel:D}}function y(e,i,a,c){var o;return r(this,void 0,void 0,function(){var r;return n(this,function(n){switch(n.label){case 0:return i?[4,u(e,{method:"POST",headers:t({"Content-Type":"application/json"},null==c?void 0:c.headers),body:JSON.stringify({query:i,variables:a}),cacheKey:null==c?void 0:c.cacheKey,signal:null==c?void 0:c.signal})]:[3,2];case 1:return[2,n.sent().data];case 2:return r=null!==(o=null==c?void 0:c.method)&&void 0!==o?o:"GET",[4,u(e,{method:r,headers:null==c?void 0:c.headers,cacheKey:null==c?void 0:c.cacheKey,signal:null==c?void 0:c.signal})];case 3:return[2,n.sent()]}})})}exports.SubscriptionManager=l,exports.cache=a,exports.createTimeoutSignal=d,exports.queryRegistry=s,exports.retryOperation=h,exports.updateCache=f,exports.useGraphQLMutation=function(i,a){var c=this,o=a.mutation,l=a.variables,f=a.cacheKey,v=a.optimisticUpdate,p=a.retry,y=void 0===p?3:p,b=a.retryDelay,m=void 0===b?500:b,w=a.headers,g=a.timeout,K=void 0===g?1e4:g,S=e.useRef(null),x=e.useCallback(function(){return r(c,void 0,void 0,function(){var e,a,c,p,b=this;return n(this,function(g){switch(g.label){case 0:null===(p=S.current)||void 0===p||p.abort(),e=new AbortController,S.current=e,a=d(K),c=function(e,d){return r(b,void 0,void 0,function(){var r,p;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,u(i,{method:"POST",headers:t({"Content-Type":"application/json"},w),body:JSON.stringify({query:o,variables:l}),signal:a,cacheKey:f})];case 1:return r=n.sent(),v&&f&&s.setData(f,function(e){return v(e,r.data)}),f&&s.invalidate(f),[2,r.data];case 2:if("AbortError"===(p=n.sent()).name)throw p;return[2,h(function(){return c(e-1,2*d)},e-1,2*d)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,c(y,m)];case 2:return[2,g.sent()];case 3:return[7];case 4:return[2]}})})},[i,o,l,f,v,y,m,w,K]),C=e.useCallback(function(){var e;null===(e=S.current)||void 0===e||e.abort()},[]);return{mutate:x,cancel:C}},exports.useGraphQLQuery=function(e,t){return p(e,{query:t.query,variables:t.variables,options:{cacheKey:t.cacheKey,staleTime:t.staleTime,headers:t.headers,timeout:t.timeout,retry:t.retry,retryDelay:t.retryDelay,optimisticUpdate:t.optimisticUpdate}})},exports.useHybridQuery=p,exports.useMutation=function(i,a){var c=this,o=e.useState(!1),l=o[0],f=o[1],v=e.useState(null),p=v[0],y=v[1],b=e.useRef(null),m=e.useCallback(function(e,o){return void 0===o&&(o="POST"),r(c,void 0,void 0,function(){var c,l,v,p,m,w=this;return n(this,function(g){switch(g.label){case 0:f(!0),y(null),null===(p=b.current)||void 0===p||p.abort(),c=new AbortController,b.current=c,l=d(null!==(m=null==a?void 0:a.timeout)&&void 0!==m?m:1e4),v=function(c,d){return void 0===c&&(c=null!==(p=null==a?void 0:a.retry)&&void 0!==p?p:3),void 0===d&&(d=null!==(m=null==a?void 0:a.retryDelay)&&void 0!==m?m:500),r(w,void 0,void 0,function(){var r,f,p;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,u(i,{method:o,headers:t({"Content-Type":"application/json"},null==a?void 0:a.headers),body:JSON.stringify(e),cacheKey:null==a?void 0:a.cacheKey,signal:l})];case 1:return r=n.sent(),(null==a?void 0:a.optimisticUpdate)&&a.cacheKey&&(s.setData(a.cacheKey,function(e){return a.optimisticUpdate(e,r)}),s.invalidate(a.cacheKey)),null===(p=null==a?void 0:a.onSuccess)||void 0===p||p.call(a,r),[2,r];case 2:if("AbortError"===(f=n.sent()).name)throw f;return[2,h(function(){return v(c-1,2*d)},c-1,2*d)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,v()];case 2:return[2,g.sent()];case 3:return f(!1),[7];case 4:return[2]}})})},[i,a]),w=e.useCallback(function(){var e;return null===(e=b.current)||void 0===e?void 0:e.abort()},[]);return{mutate:m,loading:l,error:p,cancel:w}},exports.useQuery=function(e,t){return p(e,t)};
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=function(){return t=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var a in t=arguments[r])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e},t.apply(this,arguments)};function r(e,t,r,n){return new(r||(r=Promise))(function(a,i){function c(e){try{o(n.next(e))}catch(e){i(e)}}function u(e){try{o(n.throw(e))}catch(e){i(e)}}function o(e){var t;e.done?a(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(c,u)}o((n=n.apply(e,t||[])).next())})}function n(e,t){var r,n,a,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},c=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return c.next=u(0),c.throw=u(1),c.return=u(2),"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function u(u){return function(o){return function(u){if(r)throw new TypeError("Generator is already executing.");for(;c&&(c=0,u[0]&&(i=0)),i;)try{if(r=1,n&&(a=2&u[0]?n.return:u[0]?n.throw||((a=n.return)&&a.call(n),0):n.next)&&!(a=a.call(n,u[1])).done)return a;switch(n=0,a&&(u=[2&u[0],a.value]),u[0]){case 0:case 1:a=u;break;case 4:return i.label++,{value:u[1],done:!1};case 5:i.label++,n=u[1],u=[0];continue;case 7:u=i.ops.pop(),i.trys.pop();continue;default:if(!(a=i.trys,(a=a.length>0&&a[a.length-1])||6!==u[0]&&2!==u[0])){i=0;continue}if(3===u[0]&&(!a||u[1]>a[0]&&u[1]<a[3])){i.label=u[1];break}if(6===u[0]&&i.label<a[1]){i.label=a[1],a=u;break}if(a&&i.label<a[2]){i.label=a[2],i.ops.push(u);break}a[2]&&i.ops.pop(),i.trys.pop();continue}u=t.call(e,i)}catch(e){u=[6,e],n=0}finally{r=a=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,o])}}}"function"==typeof SuppressedError&&SuppressedError;var a=new Map,i={get:function(e){var t=a.get(e);if(t)return t.data},set:function(e,t){a.set(e,{data:t,timestamp:Date.now()})},delete:function(e){a.delete(e)},clear:function(){return a.clear()}},c=new Map;function u(e,t){return void 0===t&&(t={}),r(this,void 0,void 0,function(){var a,u,o,s=this;return n(this,function(l){return a=t.cacheKey||e,c.has(a)?[2,c.get(a)]:(u=i.get(a))?[2,u]:(o=fetch(e,t).then(function(e){return r(s,void 0,void 0,function(){var t;return n(this,function(r){switch(r.label){case 0:if(!e.ok)throw new Error(e.statusText);return[4,e.json()];case 1:return t=r.sent(),i.set(a,t),[2,t]}})})}).finally(function(){return c.delete(a)}),c.set(a,o),[2,o])})})}var o=new Map,s={register:function(e,t,r,n,a){o.set(e,{cacheKey:e,refetch:t,setData:r,cancel:n,cache:a})},getCache:function(e){var t;return null===(t=o.get(e))||void 0===t?void 0:t.cache},setCache:function(e,t){var r,n=o.get(e);n&&(n.cache=t,null===(r=n.setData)||void 0===r||r.call(n,function(){return t}))},setData:function(e,t){var r,n=o.get(e);if(n){var a=t(n.cache);n.cache=a,null===(r=n.setData)||void 0===r||r.call(n,function(){return a})}},cancel:function(e){var t,r;null===(r=null===(t=o.get(e))||void 0===t?void 0:t.cancel)||void 0===r||r.call(t)},invalidate:function(e){var t;e?null===(t=o.get(e))||void 0===t||t.refetch():o.forEach(function(e){return e.refetch()})},unregister:function(e){var t,r;null===(r=null===(t=o.get(e))||void 0===t?void 0:t.cancel)||void 0===r||r.call(t),o.delete(e)}},l=function(){function e(e){this.url=e,this.callbacks=new Set,this.ws=null}return e.prototype.connect=function(){var e=this;this.ws||(this.ws=new WebSocket(this.url),this.ws.onmessage=function(t){var r=JSON.parse(t.data);e.callbacks.forEach(function(e){return e(r)})})},e.prototype.subscribe=function(e){var t=this;return this.callbacks.add(e),this.connect(),function(){return t.callbacks.delete(e)}},e.prototype.disconnect=function(){var e;null===(e=this.ws)||void 0===e||e.close(),this.ws=null,this.callbacks.clear()},e}();function h(e,t,a){return r(this,void 0,void 0,function(){var r;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,4]),[4,e()];case 1:return[2,n.sent()];case 2:if(r=n.sent(),t<=0)throw r;return[4,new Promise(function(e){return setTimeout(e,a)})];case 3:return n.sent(),[2,h(e,t-1,2*a)];case 4:return[2]}})})}function d(e){var t=new AbortController,r=setTimeout(function(){return t.abort()},e);return t.signal.addEventListener("abort",function(){return clearTimeout(r)}),t.signal}function f(e,t,r){e&&(s.setData(e,function(e){return t?t(e,r):r}),s.invalidate(e))}function v(t,a){var i=this,c=a.query,u=a.variables,o=a.options,v=void 0===o?{}:o,b=e.useState(null),m=b[0],w=b[1],g=e.useState(null),K=g[0],S=g[1],x=e.useState(!1),C=x[0],T=x[1],k=e.useRef(null),U=e.useCallback(function(){return r(i,void 0,void 0,function(){var e,a,i,o,s,l=this;return n(this,function(y){switch(y.label){case 0:T(!0),S(null),null===(o=k.current)||void 0===o||o.abort(),e=new AbortController,k.current=e,a=d(null!==(s=v.timeout)&&void 0!==s?s:1e4),i=function(e,d){return void 0===e&&(e=null!==(o=v.retry)&&void 0!==o?o:3),void 0===d&&(d=null!==(s=v.retryDelay)&&void 0!==s?s:500),r(l,void 0,void 0,function(){var r,o;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,p(t,c,u,{headers:v.headers,cacheKey:v.cacheKey,method:v.method,signal:a})];case 1:return r=n.sent(),w(function(e){return v.optimisticUpdate?v.optimisticUpdate(e,r):r}),f(v.cacheKey,v.optimisticUpdate,r),[2,r];case 2:if("AbortError"===(o=n.sent()).name)throw o;return[2,h(function(){return i(e-1,2*d)},e-1,2*d)];case 3:return[2]}})})},y.label=1;case 1:return y.trys.push([1,,3,4]),[4,i()];case 2:return[2,y.sent()];case 3:return T(!1),[7];case 4:return[2]}})})},[t,c,u,v.cacheKey,v.optimisticUpdate,v.retry,v.retryDelay,v.headers,v.timeout,v.method]),D=e.useCallback(function(){var e;return null===(e=k.current)||void 0===e?void 0:e.abort()},[]);return e.useEffect(function(){var e=!0;return r(i,void 0,void 0,function(){var r;return n(this,function(n){switch(n.label){case 0:return v.prefetch&&v.cacheKey?(r=s.getCache(v.cacheKey))?(w(r),[3,3]):[3,1]:[3,3];case 1:return[4,y(t,{query:c,variables:u,options:{cacheKey:v.cacheKey,headers:v.headers,method:v.method}})];case 2:n.sent(),n.label=3;case 3:return v.autoFetch&&e&&U(),[2]}})}),v.cacheKey&&s.register(v.cacheKey,U,w,D),function(){v.cacheKey&&s.unregister(v.cacheKey),D(),e=!1}},[t,c,u,v.prefetch,v.autoFetch,v.cacheKey,v.method]),e.useEffect(function(){if(v.staleTime){var e=setInterval(U,v.staleTime);return function(){return clearInterval(e)}}},[U,v.staleTime]),e.useEffect(function(){if(v.subscriptionUrl){var e=new l(v.subscriptionUrl).subscribe(function(e){w(function(t){return v.optimisticUpdate?v.optimisticUpdate(t,e):e}),f(v.cacheKey,v.optimisticUpdate,e)});return function(){return e()}}},[v.subscriptionUrl,v.optimisticUpdate,v.cacheKey]),{data:m,error:K,loading:C,refetch:U,mutate:w,cancel:D}}function p(e,a,i,c){var o;return r(this,void 0,void 0,function(){var r;return n(this,function(n){switch(n.label){case 0:return a?[4,u(e,{method:"POST",headers:t({"Content-Type":"application/json"},null==c?void 0:c.headers),body:JSON.stringify({query:a,variables:i}),cacheKey:null==c?void 0:c.cacheKey,signal:null==c?void 0:c.signal})]:[3,2];case 1:return[2,n.sent().data];case 2:return r=null!==(o=null==c?void 0:c.method)&&void 0!==o?o:"GET",[4,u(e,{method:r,headers:null==c?void 0:c.headers,cacheKey:null==c?void 0:c.cacheKey,signal:null==c?void 0:c.signal})];case 3:return[2,n.sent()]}})})}function y(e,t){var a=t.query,i=t.variables,c=t.options,u=void 0===c?{}:c;return r(this,void 0,void 0,function(){var t,r,c,o,l;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),t=u.cacheKey,r=u.headers,c=u.method,[4,p(e,a,i,{headers:r,cacheKey:t,method:c})];case 1:return o=n.sent(),t&&s.setCache(t,o),[2,o];case 2:return l=n.sent(),console.error("Prefetch failed",l),[2,null];case 3:return[2]}})})}exports.SubscriptionManager=l,exports.cache=i,exports.createTimeoutSignal=d,exports.prefetchQuery=y,exports.queryRegistry=s,exports.retryOperation=h,exports.updateCache=f,exports.useGraphQLMutation=function(a,i){var c=this,o=i.mutation,l=i.variables,f=i.cacheKey,v=i.optimisticUpdate,p=i.retry,y=void 0===p?3:p,b=i.retryDelay,m=void 0===b?500:b,w=i.headers,g=i.timeout,K=void 0===g?1e4:g,S=e.useRef(null),x=e.useCallback(function(){return r(c,void 0,void 0,function(){var e,i,c,p,b=this;return n(this,function(g){switch(g.label){case 0:null===(p=S.current)||void 0===p||p.abort(),e=new AbortController,S.current=e,i=d(K),c=function(e,d){return r(b,void 0,void 0,function(){var r,p;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,u(a,{method:"POST",headers:t({"Content-Type":"application/json"},w),body:JSON.stringify({query:o,variables:l}),signal:i,cacheKey:f})];case 1:return r=n.sent(),v&&f&&s.setData(f,function(e){return v(e,r.data)}),f&&s.invalidate(f),[2,r.data];case 2:if("AbortError"===(p=n.sent()).name)throw p;return[2,h(function(){return c(e-1,2*d)},e-1,2*d)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,c(y,m)];case 2:return[2,g.sent()];case 3:return[7];case 4:return[2]}})})},[a,o,l,f,v,y,m,w,K]),C=e.useCallback(function(){var e;null===(e=S.current)||void 0===e||e.abort()},[]);return{mutate:x,cancel:C}},exports.useGraphQLQuery=function(e,t){return v(e,{query:t.query,variables:t.variables,options:{cacheKey:t.cacheKey,staleTime:t.staleTime,headers:t.headers,timeout:t.timeout,retry:t.retry,retryDelay:t.retryDelay,optimisticUpdate:t.optimisticUpdate}})},exports.useHybridQuery=v,exports.useMutation=function(a,i){var c=this,o=e.useState(!1),l=o[0],f=o[1],v=e.useState(null),p=v[0],y=v[1],b=e.useRef(null),m=e.useCallback(function(e,o){return void 0===o&&(o="POST"),r(c,void 0,void 0,function(){var c,l,v,p,m,w=this;return n(this,function(g){switch(g.label){case 0:f(!0),y(null),null===(p=b.current)||void 0===p||p.abort(),c=new AbortController,b.current=c,l=d(null!==(m=null==i?void 0:i.timeout)&&void 0!==m?m:1e4),v=function(c,d){return void 0===c&&(c=null!==(p=null==i?void 0:i.retry)&&void 0!==p?p:3),void 0===d&&(d=null!==(m=null==i?void 0:i.retryDelay)&&void 0!==m?m:500),r(w,void 0,void 0,function(){var r,f,p;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,u(a,{method:o,headers:t({"Content-Type":"application/json"},null==i?void 0:i.headers),body:JSON.stringify(e),cacheKey:null==i?void 0:i.cacheKey,signal:l})];case 1:return r=n.sent(),(null==i?void 0:i.optimisticUpdate)&&i.cacheKey&&(s.setData(i.cacheKey,function(e){return i.optimisticUpdate(e,r)}),s.invalidate(i.cacheKey)),null===(p=null==i?void 0:i.onSuccess)||void 0===p||p.call(i,r),[2,r];case 2:if("AbortError"===(f=n.sent()).name)throw f;return[2,h(function(){return v(c-1,2*d)},c-1,2*d)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,v()];case 2:return[2,g.sent()];case 3:return f(!1),[7];case 4:return[2]}})})},[a,i]),w=e.useCallback(function(){var e;return null===(e=b.current)||void 0===e?void 0:e.abort()},[]);return{mutate:m,loading:l,error:p,cancel:w}},exports.useQuery=function(e,t){return v(e,t)};
|
package/build/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from "./hooks/types";
|
|
2
|
+
export { useHybridQuery, prefetchQuery } from "./hooks/useHybridQuery";
|
|
2
3
|
export { useQuery } from "./hooks/useQuery";
|
|
3
4
|
export { useMutation } from "./hooks/useMutation";
|
|
4
5
|
export { useGraphQLQuery } from "./hooks/useGraphQLQuery";
|
package/build/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useState as e,useRef as t,useCallback as n,useEffect as r}from"react";var i=function(){return i=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},i.apply(this,arguments)};function a(e,t,n,r){return new(n||(n=Promise))(function(i,a){function c(e){try{u(r.next(e))}catch(e){a(e)}}function o(e){try{u(r.throw(e))}catch(e){a(e)}}function u(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(c,o)}u((r=r.apply(e,t||[])).next())})}function c(e,t){var n,r,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},c=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return c.next=o(0),c.throw=o(1),c.return=o(2),"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function o(o){return function(u){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;c&&(c=0,o[0]&&(a=0)),a;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,r=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,u])}}}"function"==typeof SuppressedError&&SuppressedError;var o=new Map,u={get:function(e){var t=o.get(e);if(t)return t.data},set:function(e,t){o.set(e,{data:t,timestamp:Date.now()})},delete:function(e){o.delete(e)},clear:function(){return o.clear()}},s=new Map;function l(e,t){return void 0===t&&(t={}),a(this,void 0,void 0,function(){var n,r,i,o=this;return c(this,function(l){return n=t.cacheKey||e,s.has(n)?[2,s.get(n)]:(r=u.get(n))?[2,r]:(i=fetch(e,t).then(function(e){return a(o,void 0,void 0,function(){var t;return c(this,function(r){switch(r.label){case 0:if(!e.ok)throw new Error(e.statusText);return[4,e.json()];case 1:return t=r.sent(),u.set(n,t),[2,t]}})})}).finally(function(){return s.delete(n)}),s.set(n,i),[2,i])})})}var h=new Map,d={register:function(e,t,n,r,i){h.set(e,{cacheKey:e,refetch:t,setData:n,cancel:r,cache:i})},getCache:function(e){var t;return null===(t=h.get(e))||void 0===t?void 0:t.cache},setCache:function(e,t){var n,r=h.get(e);r&&(r.cache=t,null===(n=r.setData)||void 0===n||n.call(r,function(){return t}))},setData:function(e,t){var n,r=h.get(e);if(r){var i=t(r.cache);r.cache=i,null===(n=r.setData)||void 0===n||n.call(r,function(){return i})}},cancel:function(e){var t,n;null===(n=null===(t=h.get(e))||void 0===t?void 0:t.cancel)||void 0===n||n.call(t)},invalidate:function(e){var t;e?null===(t=h.get(e))||void 0===t||t.refetch():h.forEach(function(e){return e.refetch()})},unregister:function(e){var t,n;null===(n=null===(t=h.get(e))||void 0===t?void 0:t.cancel)||void 0===n||n.call(t),h.delete(e)}},f=function(){function e(e){this.url=e,this.callbacks=new Set,this.ws=null}return e.prototype.connect=function(){var e=this;this.ws||(this.ws=new WebSocket(this.url),this.ws.onmessage=function(t){var n=JSON.parse(t.data);e.callbacks.forEach(function(e){return e(n)})})},e.prototype.subscribe=function(e){var t=this;return this.callbacks.add(e),this.connect(),function(){return t.callbacks.delete(e)}},e.prototype.disconnect=function(){var e;null===(e=this.ws)||void 0===e||e.close(),this.ws=null,this.callbacks.clear()},e}();function v(e,t,n){return a(this,void 0,void 0,function(){var r;return c(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,4]),[4,e()];case 1:return[2,i.sent()];case 2:if(r=i.sent(),t<=0)throw r;return[4,new Promise(function(e){return setTimeout(e,n)})];case 3:return i.sent(),[2,v(e,t-1,2*n)];case 4:return[2]}})})}function y(e){var t=new AbortController,n=setTimeout(function(){return t.abort()},e);return t.signal.addEventListener("abort",function(){return clearTimeout(n)}),t.signal}function p(e,t,n){e&&(d.setData(e,function(e){return t?t(e,n):n}),d.invalidate(e))}function b(e,t,n,r,i,o){return a(this,void 0,void 0,function(){var a,u;return c(this,function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,w(e,t,n,{headers:i,cacheKey:r,method:o})];case 1:return a=c.sent(),r&&d.setCache(r,a),[2,a];case 2:return u=c.sent(),console.error("Prefetch failed",u),[2,null];case 3:return[2]}})})}function m(i,o){var u=this,s=o.query,l=o.variables,h=o.options,m=void 0===h?{}:h,g=e(null),K=g[0],T=g[1],U=e(null),D=U[0],S=U[1],O=e(!1),k=O[0],C=O[1],E=t(null),j=n(function(){return a(u,void 0,void 0,function(){var e,t,n,r,o,u=this;return c(this,function(h){switch(h.label){case 0:C(!0),S(null),null===(r=E.current)||void 0===r||r.abort(),e=new AbortController,E.current=e,t=y(null!==(o=m.timeout)&&void 0!==o?o:1e4),n=function(e,h){return void 0===e&&(e=null!==(r=m.retry)&&void 0!==r?r:3),void 0===h&&(h=null!==(o=m.retryDelay)&&void 0!==o?o:500),a(u,void 0,void 0,function(){var r,a;return c(this,function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,w(i,s,l,{headers:m.headers,cacheKey:m.cacheKey,method:m.method,signal:t})];case 1:return r=c.sent(),T(function(e){return m.optimisticUpdate?m.optimisticUpdate(e,r):r}),p(m.cacheKey,m.optimisticUpdate,r),[2,r];case 2:if("AbortError"===(a=c.sent()).name)throw a;return[2,v(function(){return n(e-1,2*h)},e-1,2*h)];case 3:return[2]}})})},h.label=1;case 1:return h.trys.push([1,,3,4]),[4,n()];case 2:return[2,h.sent()];case 3:return C(!1),[7];case 4:return[2]}})})},[i,s,l,m.cacheKey,m.optimisticUpdate,m.retry,m.retryDelay,m.headers,m.timeout,m.method]),x=n(function(){var e;return null===(e=E.current)||void 0===e?void 0:e.abort()},[]);return r(function(){var e=!0;return a(u,void 0,void 0,function(){var t;return c(this,function(n){switch(n.label){case 0:return m.prefetch&&m.cacheKey?(t=d.getCache(m.cacheKey))?(T(t),[3,3]):[3,1]:[3,3];case 1:return[4,b(i,s,l,m.cacheKey,m.headers,m.method)];case 2:n.sent(),n.label=3;case 3:return m.autoFetch&&e&&j(),[2]}})}),m.cacheKey&&d.register(m.cacheKey,j,T,x),function(){m.cacheKey&&d.unregister(m.cacheKey),x(),e=!1}},[i,s,l,m.prefetch,m.autoFetch,m.cacheKey,m.method]),r(function(){if(m.staleTime){var e=setInterval(j,m.staleTime);return function(){return clearInterval(e)}}},[j,m.staleTime]),r(function(){if(m.subscriptionUrl){var e=new f(m.subscriptionUrl).subscribe(function(e){T(function(t){return m.optimisticUpdate?m.optimisticUpdate(t,e):e}),p(m.cacheKey,m.optimisticUpdate,e)});return function(){return e()}}},[m.subscriptionUrl,m.optimisticUpdate,m.cacheKey]),{data:K,error:D,loading:k,refetch:j,mutate:T,cancel:x}}function w(e,t,n,r){var o;return a(this,void 0,void 0,function(){var a;return c(this,function(c){switch(c.label){case 0:return t?[4,l(e,{method:"POST",headers:i({"Content-Type":"application/json"},null==r?void 0:r.headers),body:JSON.stringify({query:t,variables:n}),cacheKey:null==r?void 0:r.cacheKey,signal:null==r?void 0:r.signal})]:[3,2];case 1:return[2,c.sent().data];case 2:return a=null!==(o=null==r?void 0:r.method)&&void 0!==o?o:"GET",[4,l(e,{method:a,headers:null==r?void 0:r.headers,cacheKey:null==r?void 0:r.cacheKey,signal:null==r?void 0:r.signal})];case 3:return[2,c.sent()]}})})}function g(e,t){return m(e,t)}function K(r,o){var u=this,s=e(!1),h=s[0],f=s[1],p=e(null),b=p[0],m=p[1],w=t(null),g=n(function(e,t){return void 0===t&&(t="POST"),a(u,void 0,void 0,function(){var n,u,s,h,p,b=this;return c(this,function(g){switch(g.label){case 0:f(!0),m(null),null===(h=w.current)||void 0===h||h.abort(),n=new AbortController,w.current=n,u=y(null!==(p=null==o?void 0:o.timeout)&&void 0!==p?p:1e4),s=function(n,f){return void 0===n&&(n=null!==(h=null==o?void 0:o.retry)&&void 0!==h?h:3),void 0===f&&(f=null!==(p=null==o?void 0:o.retryDelay)&&void 0!==p?p:500),a(b,void 0,void 0,function(){var a,h,y;return c(this,function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,l(r,{method:t,headers:i({"Content-Type":"application/json"},null==o?void 0:o.headers),body:JSON.stringify(e),cacheKey:null==o?void 0:o.cacheKey,signal:u})];case 1:return a=c.sent(),(null==o?void 0:o.optimisticUpdate)&&o.cacheKey&&(d.setData(o.cacheKey,function(e){return o.optimisticUpdate(e,a)}),d.invalidate(o.cacheKey)),null===(y=null==o?void 0:o.onSuccess)||void 0===y||y.call(o,a),[2,a];case 2:if("AbortError"===(h=c.sent()).name)throw h;return[2,v(function(){return s(n-1,2*f)},n-1,2*f)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,s()];case 2:return[2,g.sent()];case 3:return f(!1),[7];case 4:return[2]}})})},[r,o]),K=n(function(){var e;return null===(e=w.current)||void 0===e?void 0:e.abort()},[]);return{mutate:g,loading:h,error:b,cancel:K}}function T(e,t){return m(e,{query:t.query,variables:t.variables,options:{cacheKey:t.cacheKey,staleTime:t.staleTime,headers:t.headers,timeout:t.timeout,retry:t.retry,retryDelay:t.retryDelay,optimisticUpdate:t.optimisticUpdate}})}function U(e,r){var o=this,u=r.mutation,s=r.variables,h=r.cacheKey,f=r.optimisticUpdate,p=r.retry,b=void 0===p?3:p,m=r.retryDelay,w=void 0===m?500:m,g=r.headers,K=r.timeout,T=void 0===K?1e4:K,U=t(null),D=n(function(){return a(o,void 0,void 0,function(){var t,n,r,o,p=this;return c(this,function(m){switch(m.label){case 0:null===(o=U.current)||void 0===o||o.abort(),t=new AbortController,U.current=t,n=y(T),r=function(t,o){return a(p,void 0,void 0,function(){var a,y;return c(this,function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,l(e,{method:"POST",headers:i({"Content-Type":"application/json"},g),body:JSON.stringify({query:u,variables:s}),signal:n,cacheKey:h})];case 1:return a=c.sent(),f&&h&&d.setData(h,function(e){return f(e,a.data)}),h&&d.invalidate(h),[2,a.data];case 2:if("AbortError"===(y=c.sent()).name)throw y;return[2,v(function(){return r(t-1,2*o)},t-1,2*o)];case 3:return[2]}})})},m.label=1;case 1:return m.trys.push([1,,3,4]),[4,r(b,w)];case 2:return[2,m.sent()];case 3:return[7];case 4:return[2]}})})},[e,u,s,h,f,b,w,g,T]),S=n(function(){var e;null===(e=U.current)||void 0===e||e.abort()},[]);return{mutate:D,cancel:S}}export{f as SubscriptionManager,u as cache,y as createTimeoutSignal,d as queryRegistry,v as retryOperation,p as updateCache,U as useGraphQLMutation,T as useGraphQLQuery,m as useHybridQuery,K as useMutation,g as useQuery};
|
|
1
|
+
import{useState as e,useRef as t,useCallback as n,useEffect as r}from"react";var i=function(){return i=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},i.apply(this,arguments)};function a(e,t,n,r){return new(n||(n=Promise))(function(i,a){function c(e){try{u(r.next(e))}catch(e){a(e)}}function o(e){try{u(r.throw(e))}catch(e){a(e)}}function u(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(c,o)}u((r=r.apply(e,t||[])).next())})}function c(e,t){var n,r,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},c=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return c.next=o(0),c.throw=o(1),c.return=o(2),"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function o(o){return function(u){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;c&&(c=0,o[0]&&(a=0)),a;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,r=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,u])}}}"function"==typeof SuppressedError&&SuppressedError;var o=new Map,u={get:function(e){var t=o.get(e);if(t)return t.data},set:function(e,t){o.set(e,{data:t,timestamp:Date.now()})},delete:function(e){o.delete(e)},clear:function(){return o.clear()}},s=new Map;function l(e,t){return void 0===t&&(t={}),a(this,void 0,void 0,function(){var n,r,i,o=this;return c(this,function(l){return n=t.cacheKey||e,s.has(n)?[2,s.get(n)]:(r=u.get(n))?[2,r]:(i=fetch(e,t).then(function(e){return a(o,void 0,void 0,function(){var t;return c(this,function(r){switch(r.label){case 0:if(!e.ok)throw new Error(e.statusText);return[4,e.json()];case 1:return t=r.sent(),u.set(n,t),[2,t]}})})}).finally(function(){return s.delete(n)}),s.set(n,i),[2,i])})})}var h=new Map,d={register:function(e,t,n,r,i){h.set(e,{cacheKey:e,refetch:t,setData:n,cancel:r,cache:i})},getCache:function(e){var t;return null===(t=h.get(e))||void 0===t?void 0:t.cache},setCache:function(e,t){var n,r=h.get(e);r&&(r.cache=t,null===(n=r.setData)||void 0===n||n.call(r,function(){return t}))},setData:function(e,t){var n,r=h.get(e);if(r){var i=t(r.cache);r.cache=i,null===(n=r.setData)||void 0===n||n.call(r,function(){return i})}},cancel:function(e){var t,n;null===(n=null===(t=h.get(e))||void 0===t?void 0:t.cancel)||void 0===n||n.call(t)},invalidate:function(e){var t;e?null===(t=h.get(e))||void 0===t||t.refetch():h.forEach(function(e){return e.refetch()})},unregister:function(e){var t,n;null===(n=null===(t=h.get(e))||void 0===t?void 0:t.cancel)||void 0===n||n.call(t),h.delete(e)}},f=function(){function e(e){this.url=e,this.callbacks=new Set,this.ws=null}return e.prototype.connect=function(){var e=this;this.ws||(this.ws=new WebSocket(this.url),this.ws.onmessage=function(t){var n=JSON.parse(t.data);e.callbacks.forEach(function(e){return e(n)})})},e.prototype.subscribe=function(e){var t=this;return this.callbacks.add(e),this.connect(),function(){return t.callbacks.delete(e)}},e.prototype.disconnect=function(){var e;null===(e=this.ws)||void 0===e||e.close(),this.ws=null,this.callbacks.clear()},e}();function v(e,t,n){return a(this,void 0,void 0,function(){var r;return c(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,4]),[4,e()];case 1:return[2,i.sent()];case 2:if(r=i.sent(),t<=0)throw r;return[4,new Promise(function(e){return setTimeout(e,n)})];case 3:return i.sent(),[2,v(e,t-1,2*n)];case 4:return[2]}})})}function y(e){var t=new AbortController,n=setTimeout(function(){return t.abort()},e);return t.signal.addEventListener("abort",function(){return clearTimeout(n)}),t.signal}function p(e,t,n){e&&(d.setData(e,function(e){return t?t(e,n):n}),d.invalidate(e))}function b(i,o){var u=this,s=o.query,l=o.variables,h=o.options,b=void 0===h?{}:h,g=e(null),K=g[0],T=g[1],U=e(null),D=U[0],S=U[1],O=e(!1),k=O[0],C=O[1],E=t(null),j=n(function(){return a(u,void 0,void 0,function(){var e,t,n,r,o,u=this;return c(this,function(h){switch(h.label){case 0:C(!0),S(null),null===(r=E.current)||void 0===r||r.abort(),e=new AbortController,E.current=e,t=y(null!==(o=b.timeout)&&void 0!==o?o:1e4),n=function(e,h){return void 0===e&&(e=null!==(r=b.retry)&&void 0!==r?r:3),void 0===h&&(h=null!==(o=b.retryDelay)&&void 0!==o?o:500),a(u,void 0,void 0,function(){var r,a;return c(this,function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,m(i,s,l,{headers:b.headers,cacheKey:b.cacheKey,method:b.method,signal:t})];case 1:return r=c.sent(),T(function(e){return b.optimisticUpdate?b.optimisticUpdate(e,r):r}),p(b.cacheKey,b.optimisticUpdate,r),[2,r];case 2:if("AbortError"===(a=c.sent()).name)throw a;return[2,v(function(){return n(e-1,2*h)},e-1,2*h)];case 3:return[2]}})})},h.label=1;case 1:return h.trys.push([1,,3,4]),[4,n()];case 2:return[2,h.sent()];case 3:return C(!1),[7];case 4:return[2]}})})},[i,s,l,b.cacheKey,b.optimisticUpdate,b.retry,b.retryDelay,b.headers,b.timeout,b.method]),q=n(function(){var e;return null===(e=E.current)||void 0===e?void 0:e.abort()},[]);return r(function(){var e=!0;return a(u,void 0,void 0,function(){var t;return c(this,function(n){switch(n.label){case 0:return b.prefetch&&b.cacheKey?(t=d.getCache(b.cacheKey))?(T(t),[3,3]):[3,1]:[3,3];case 1:return[4,w(i,{query:s,variables:l,options:{cacheKey:b.cacheKey,headers:b.headers,method:b.method}})];case 2:n.sent(),n.label=3;case 3:return b.autoFetch&&e&&j(),[2]}})}),b.cacheKey&&d.register(b.cacheKey,j,T,q),function(){b.cacheKey&&d.unregister(b.cacheKey),q(),e=!1}},[i,s,l,b.prefetch,b.autoFetch,b.cacheKey,b.method]),r(function(){if(b.staleTime){var e=setInterval(j,b.staleTime);return function(){return clearInterval(e)}}},[j,b.staleTime]),r(function(){if(b.subscriptionUrl){var e=new f(b.subscriptionUrl).subscribe(function(e){T(function(t){return b.optimisticUpdate?b.optimisticUpdate(t,e):e}),p(b.cacheKey,b.optimisticUpdate,e)});return function(){return e()}}},[b.subscriptionUrl,b.optimisticUpdate,b.cacheKey]),{data:K,error:D,loading:k,refetch:j,mutate:T,cancel:q}}function m(e,t,n,r){var o;return a(this,void 0,void 0,function(){var a;return c(this,function(c){switch(c.label){case 0:return t?[4,l(e,{method:"POST",headers:i({"Content-Type":"application/json"},null==r?void 0:r.headers),body:JSON.stringify({query:t,variables:n}),cacheKey:null==r?void 0:r.cacheKey,signal:null==r?void 0:r.signal})]:[3,2];case 1:return[2,c.sent().data];case 2:return a=null!==(o=null==r?void 0:r.method)&&void 0!==o?o:"GET",[4,l(e,{method:a,headers:null==r?void 0:r.headers,cacheKey:null==r?void 0:r.cacheKey,signal:null==r?void 0:r.signal})];case 3:return[2,c.sent()]}})})}function w(e,t){var n=t.query,r=t.variables,i=t.options,o=void 0===i?{}:i;return a(this,void 0,void 0,function(){var t,i,a,u,s;return c(this,function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),t=o.cacheKey,i=o.headers,a=o.method,[4,m(e,n,r,{headers:i,cacheKey:t,method:a})];case 1:return u=c.sent(),t&&d.setCache(t,u),[2,u];case 2:return s=c.sent(),console.error("Prefetch failed",s),[2,null];case 3:return[2]}})})}function g(e,t){return b(e,t)}function K(r,o){var u=this,s=e(!1),h=s[0],f=s[1],p=e(null),b=p[0],m=p[1],w=t(null),g=n(function(e,t){return void 0===t&&(t="POST"),a(u,void 0,void 0,function(){var n,u,s,h,p,b=this;return c(this,function(g){switch(g.label){case 0:f(!0),m(null),null===(h=w.current)||void 0===h||h.abort(),n=new AbortController,w.current=n,u=y(null!==(p=null==o?void 0:o.timeout)&&void 0!==p?p:1e4),s=function(n,f){return void 0===n&&(n=null!==(h=null==o?void 0:o.retry)&&void 0!==h?h:3),void 0===f&&(f=null!==(p=null==o?void 0:o.retryDelay)&&void 0!==p?p:500),a(b,void 0,void 0,function(){var a,h,y;return c(this,function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,l(r,{method:t,headers:i({"Content-Type":"application/json"},null==o?void 0:o.headers),body:JSON.stringify(e),cacheKey:null==o?void 0:o.cacheKey,signal:u})];case 1:return a=c.sent(),(null==o?void 0:o.optimisticUpdate)&&o.cacheKey&&(d.setData(o.cacheKey,function(e){return o.optimisticUpdate(e,a)}),d.invalidate(o.cacheKey)),null===(y=null==o?void 0:o.onSuccess)||void 0===y||y.call(o,a),[2,a];case 2:if("AbortError"===(h=c.sent()).name)throw h;return[2,v(function(){return s(n-1,2*f)},n-1,2*f)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,s()];case 2:return[2,g.sent()];case 3:return f(!1),[7];case 4:return[2]}})})},[r,o]),K=n(function(){var e;return null===(e=w.current)||void 0===e?void 0:e.abort()},[]);return{mutate:g,loading:h,error:b,cancel:K}}function T(e,t){return b(e,{query:t.query,variables:t.variables,options:{cacheKey:t.cacheKey,staleTime:t.staleTime,headers:t.headers,timeout:t.timeout,retry:t.retry,retryDelay:t.retryDelay,optimisticUpdate:t.optimisticUpdate}})}function U(e,r){var o=this,u=r.mutation,s=r.variables,h=r.cacheKey,f=r.optimisticUpdate,p=r.retry,b=void 0===p?3:p,m=r.retryDelay,w=void 0===m?500:m,g=r.headers,K=r.timeout,T=void 0===K?1e4:K,U=t(null),D=n(function(){return a(o,void 0,void 0,function(){var t,n,r,o,p=this;return c(this,function(m){switch(m.label){case 0:null===(o=U.current)||void 0===o||o.abort(),t=new AbortController,U.current=t,n=y(T),r=function(t,o){return a(p,void 0,void 0,function(){var a,y;return c(this,function(c){switch(c.label){case 0:return c.trys.push([0,2,,3]),[4,l(e,{method:"POST",headers:i({"Content-Type":"application/json"},g),body:JSON.stringify({query:u,variables:s}),signal:n,cacheKey:h})];case 1:return a=c.sent(),f&&h&&d.setData(h,function(e){return f(e,a.data)}),h&&d.invalidate(h),[2,a.data];case 2:if("AbortError"===(y=c.sent()).name)throw y;return[2,v(function(){return r(t-1,2*o)},t-1,2*o)];case 3:return[2]}})})},m.label=1;case 1:return m.trys.push([1,,3,4]),[4,r(b,w)];case 2:return[2,m.sent()];case 3:return[7];case 4:return[2]}})})},[e,u,s,h,f,b,w,g,T]),S=n(function(){var e;null===(e=U.current)||void 0===e||e.abort()},[]);return{mutate:D,cancel:S}}export{f as SubscriptionManager,u as cache,y as createTimeoutSignal,w as prefetchQuery,d as queryRegistry,v as retryOperation,p as updateCache,U as useGraphQLMutation,T as useGraphQLQuery,b as useHybridQuery,K as useMutation,g as useQuery};
|
package/build/index.umd.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactiveQuery={},e.React)}(this,function(e,t){"use strict";var n=function(){return n=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},n.apply(this,arguments)};function r(e,t,n,r){return new(n||(n=Promise))(function(i,a){function c(e){try{o(r.next(e))}catch(e){a(e)}}function u(e){try{o(r.throw(e))}catch(e){a(e)}}function o(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(c,u)}o((r=r.apply(e,t||[])).next())})}function i(e,t){var n,r,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},c=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return c.next=u(0),c.throw=u(1),c.return=u(2),"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function u(u){return function(o){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;c&&(c=0,u[0]&&(a=0)),a;)try{if(n=1,r&&(i=2&u[0]?r.return:u[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,u[1])).done)return i;switch(r=0,i&&(u=[2&u[0],i.value]),u[0]){case 0:case 1:i=u;break;case 4:return a.label++,{value:u[1],done:!1};case 5:a.label++,r=u[1],u=[0];continue;case 7:u=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==u[0]&&2!==u[0])){a=0;continue}if(3===u[0]&&(!i||u[1]>i[0]&&u[1]<i[3])){a.label=u[1];break}if(6===u[0]&&a.label<i[1]){a.label=i[1],i=u;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(u);break}i[2]&&a.ops.pop(),a.trys.pop();continue}u=t.call(e,a)}catch(e){u=[6,e],r=0}finally{n=i=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,o])}}}"function"==typeof SuppressedError&&SuppressedError;var a=new Map,c={get:function(e){var t=a.get(e);if(t)return t.data},set:function(e,t){a.set(e,{data:t,timestamp:Date.now()})},delete:function(e){a.delete(e)},clear:function(){return a.clear()}},u=new Map;function o(e,t){return void 0===t&&(t={}),r(this,void 0,void 0,function(){var n,a,o,s=this;return i(this,function(l){return n=t.cacheKey||e,u.has(n)?[2,u.get(n)]:(a=c.get(n))?[2,a]:(o=fetch(e,t).then(function(e){return r(s,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:if(!e.ok)throw new Error(e.statusText);return[4,e.json()];case 1:return t=r.sent(),c.set(n,t),[2,t]}})})}).finally(function(){return u.delete(n)}),u.set(n,o),[2,o])})})}var s=new Map,l={register:function(e,t,n,r,i){s.set(e,{cacheKey:e,refetch:t,setData:n,cancel:r,cache:i})},getCache:function(e){var t;return null===(t=s.get(e))||void 0===t?void 0:t.cache},setCache:function(e,t){var n,r=s.get(e);r&&(r.cache=t,null===(n=r.setData)||void 0===n||n.call(r,function(){return t}))},setData:function(e,t){var n,r=s.get(e);if(r){var i=t(r.cache);r.cache=i,null===(n=r.setData)||void 0===n||n.call(r,function(){return i})}},cancel:function(e){var t,n;null===(n=null===(t=s.get(e))||void 0===t?void 0:t.cancel)||void 0===n||n.call(t)},invalidate:function(e){var t;e?null===(t=s.get(e))||void 0===t||t.refetch():s.forEach(function(e){return e.refetch()})},unregister:function(e){var t,n;null===(n=null===(t=s.get(e))||void 0===t?void 0:t.cancel)||void 0===n||n.call(t),s.delete(e)}},f=function(){function e(e){this.url=e,this.callbacks=new Set,this.ws=null}return e.prototype.connect=function(){var e=this;this.ws||(this.ws=new WebSocket(this.url),this.ws.onmessage=function(t){var n=JSON.parse(t.data);e.callbacks.forEach(function(e){return e(n)})})},e.prototype.subscribe=function(e){var t=this;return this.callbacks.add(e),this.connect(),function(){return t.callbacks.delete(e)}},e.prototype.disconnect=function(){var e;null===(e=this.ws)||void 0===e||e.close(),this.ws=null,this.callbacks.clear()},e}();function h(e,t,n){return r(this,void 0,void 0,function(){var r;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,4]),[4,e()];case 1:return[2,i.sent()];case 2:if(r=i.sent(),t<=0)throw r;return[4,new Promise(function(e){return setTimeout(e,n)})];case 3:return i.sent(),[2,h(e,t-1,2*n)];case 4:return[2]}})})}function d(e){var t=new AbortController,n=setTimeout(function(){return t.abort()},e);return t.signal.addEventListener("abort",function(){return clearTimeout(n)}),t.signal}function v(e,t,n){e&&(l.setData(e,function(e){return t?t(e,n):n}),l.invalidate(e))}function y(e,t,n,a,c,u){return r(this,void 0,void 0,function(){var r,o;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,b(e,t,n,{headers:c,cacheKey:a,method:u})];case 1:return r=i.sent(),a&&l.setCache(a,r),[2,r];case 2:return o=i.sent(),console.error("Prefetch failed",o),[2,null];case 3:return[2]}})})}function p(e,n){var a=this,c=n.query,u=n.variables,o=n.options,s=void 0===o?{}:o,p=t.useState(null),m=p[0],w=p[1],g=t.useState(null),K=g[0],S=g[1],T=t.useState(!1),C=T[0],k=T[1],U=t.useRef(null),D=t.useCallback(function(){return r(a,void 0,void 0,function(){var t,n,a,o,l,f=this;return i(this,function(y){switch(y.label){case 0:k(!0),S(null),null===(o=U.current)||void 0===o||o.abort(),t=new AbortController,U.current=t,n=d(null!==(l=s.timeout)&&void 0!==l?l:1e4),a=function(t,d){return void 0===t&&(t=null!==(o=s.retry)&&void 0!==o?o:3),void 0===d&&(d=null!==(l=s.retryDelay)&&void 0!==l?l:500),r(f,void 0,void 0,function(){var r,o;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,b(e,c,u,{headers:s.headers,cacheKey:s.cacheKey,method:s.method,signal:n})];case 1:return r=i.sent(),w(function(e){return s.optimisticUpdate?s.optimisticUpdate(e,r):r}),v(s.cacheKey,s.optimisticUpdate,r),[2,r];case 2:if("AbortError"===(o=i.sent()).name)throw o;return[2,h(function(){return a(t-1,2*d)},t-1,2*d)];case 3:return[2]}})})},y.label=1;case 1:return y.trys.push([1,,3,4]),[4,a()];case 2:return[2,y.sent()];case 3:return k(!1),[7];case 4:return[2]}})})},[e,c,u,s.cacheKey,s.optimisticUpdate,s.retry,s.retryDelay,s.headers,s.timeout,s.method]),E=t.useCallback(function(){var e;return null===(e=U.current)||void 0===e?void 0:e.abort()},[]);return t.useEffect(function(){var t=!0;return r(a,void 0,void 0,function(){var n;return i(this,function(r){switch(r.label){case 0:return s.prefetch&&s.cacheKey?(n=l.getCache(s.cacheKey))?(w(n),[3,3]):[3,1]:[3,3];case 1:return[4,y(e,c,u,s.cacheKey,s.headers,s.method)];case 2:r.sent(),r.label=3;case 3:return s.autoFetch&&t&&D(),[2]}})}),s.cacheKey&&l.register(s.cacheKey,D,w,E),function(){s.cacheKey&&l.unregister(s.cacheKey),E(),t=!1}},[e,c,u,s.prefetch,s.autoFetch,s.cacheKey,s.method]),t.useEffect(function(){if(s.staleTime){var e=setInterval(D,s.staleTime);return function(){return clearInterval(e)}}},[D,s.staleTime]),t.useEffect(function(){if(s.subscriptionUrl){var e=new f(s.subscriptionUrl).subscribe(function(e){w(function(t){return s.optimisticUpdate?s.optimisticUpdate(t,e):e}),v(s.cacheKey,s.optimisticUpdate,e)});return function(){return e()}}},[s.subscriptionUrl,s.optimisticUpdate,s.cacheKey]),{data:m,error:K,loading:C,refetch:D,mutate:w,cancel:E}}function b(e,t,a,c){var u;return r(this,void 0,void 0,function(){var r;return i(this,function(i){switch(i.label){case 0:return t?[4,o(e,{method:"POST",headers:n({"Content-Type":"application/json"},null==c?void 0:c.headers),body:JSON.stringify({query:t,variables:a}),cacheKey:null==c?void 0:c.cacheKey,signal:null==c?void 0:c.signal})]:[3,2];case 1:return[2,i.sent().data];case 2:return r=null!==(u=null==c?void 0:c.method)&&void 0!==u?u:"GET",[4,o(e,{method:r,headers:null==c?void 0:c.headers,cacheKey:null==c?void 0:c.cacheKey,signal:null==c?void 0:c.signal})];case 3:return[2,i.sent()]}})})}e.SubscriptionManager=f,e.cache=c,e.createTimeoutSignal=d,e.queryRegistry=l,e.retryOperation=h,e.updateCache=v,e.useGraphQLMutation=function(e,a){var c=this,u=a.mutation,s=a.variables,f=a.cacheKey,v=a.optimisticUpdate,y=a.retry,p=void 0===y?3:y,b=a.retryDelay,m=void 0===b?500:b,w=a.headers,g=a.timeout,K=void 0===g?1e4:g,S=t.useRef(null),T=t.useCallback(function(){return r(c,void 0,void 0,function(){var t,a,c,y,b=this;return i(this,function(g){switch(g.label){case 0:null===(y=S.current)||void 0===y||y.abort(),t=new AbortController,S.current=t,a=d(K),c=function(t,d){return r(b,void 0,void 0,function(){var r,y;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,o(e,{method:"POST",headers:n({"Content-Type":"application/json"},w),body:JSON.stringify({query:u,variables:s}),signal:a,cacheKey:f})];case 1:return r=i.sent(),v&&f&&l.setData(f,function(e){return v(e,r.data)}),f&&l.invalidate(f),[2,r.data];case 2:if("AbortError"===(y=i.sent()).name)throw y;return[2,h(function(){return c(t-1,2*d)},t-1,2*d)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,c(p,m)];case 2:return[2,g.sent()];case 3:return[7];case 4:return[2]}})})},[e,u,s,f,v,p,m,w,K]),C=t.useCallback(function(){var e;null===(e=S.current)||void 0===e||e.abort()},[]);return{mutate:T,cancel:C}},e.useGraphQLQuery=function(e,t){return p(e,{query:t.query,variables:t.variables,options:{cacheKey:t.cacheKey,staleTime:t.staleTime,headers:t.headers,timeout:t.timeout,retry:t.retry,retryDelay:t.retryDelay,optimisticUpdate:t.optimisticUpdate}})},e.useHybridQuery=p,e.useMutation=function(e,a){var c=this,u=t.useState(!1),s=u[0],f=u[1],v=t.useState(null),y=v[0],p=v[1],b=t.useRef(null),m=t.useCallback(function(t,u){return void 0===u&&(u="POST"),r(c,void 0,void 0,function(){var c,s,v,y,m,w=this;return i(this,function(g){switch(g.label){case 0:f(!0),p(null),null===(y=b.current)||void 0===y||y.abort(),c=new AbortController,b.current=c,s=d(null!==(m=null==a?void 0:a.timeout)&&void 0!==m?m:1e4),v=function(c,f){return void 0===c&&(c=null!==(y=null==a?void 0:a.retry)&&void 0!==y?y:3),void 0===f&&(f=null!==(m=null==a?void 0:a.retryDelay)&&void 0!==m?m:500),r(w,void 0,void 0,function(){var r,d,y;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,o(e,{method:u,headers:n({"Content-Type":"application/json"},null==a?void 0:a.headers),body:JSON.stringify(t),cacheKey:null==a?void 0:a.cacheKey,signal:s})];case 1:return r=i.sent(),(null==a?void 0:a.optimisticUpdate)&&a.cacheKey&&(l.setData(a.cacheKey,function(e){return a.optimisticUpdate(e,r)}),l.invalidate(a.cacheKey)),null===(y=null==a?void 0:a.onSuccess)||void 0===y||y.call(a,r),[2,r];case 2:if("AbortError"===(d=i.sent()).name)throw d;return[2,h(function(){return v(c-1,2*f)},c-1,2*f)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,v()];case 2:return[2,g.sent()];case 3:return f(!1),[7];case 4:return[2]}})})},[e,a]),w=t.useCallback(function(){var e;return null===(e=b.current)||void 0===e?void 0:e.abort()},[]);return{mutate:m,loading:s,error:y,cancel:w}},e.useQuery=function(e,t){return p(e,t)},Object.defineProperty(e,"__esModule",{value:!0})});
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactiveQuery={},e.React)}(this,function(e,t){"use strict";var n=function(){return n=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},n.apply(this,arguments)};function r(e,t,n,r){return new(n||(n=Promise))(function(i,a){function c(e){try{o(r.next(e))}catch(e){a(e)}}function u(e){try{o(r.throw(e))}catch(e){a(e)}}function o(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(c,u)}o((r=r.apply(e,t||[])).next())})}function i(e,t){var n,r,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},c=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return c.next=u(0),c.throw=u(1),c.return=u(2),"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c;function u(u){return function(o){return function(u){if(n)throw new TypeError("Generator is already executing.");for(;c&&(c=0,u[0]&&(a=0)),a;)try{if(n=1,r&&(i=2&u[0]?r.return:u[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,u[1])).done)return i;switch(r=0,i&&(u=[2&u[0],i.value]),u[0]){case 0:case 1:i=u;break;case 4:return a.label++,{value:u[1],done:!1};case 5:a.label++,r=u[1],u=[0];continue;case 7:u=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==u[0]&&2!==u[0])){a=0;continue}if(3===u[0]&&(!i||u[1]>i[0]&&u[1]<i[3])){a.label=u[1];break}if(6===u[0]&&a.label<i[1]){a.label=i[1],i=u;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(u);break}i[2]&&a.ops.pop(),a.trys.pop();continue}u=t.call(e,a)}catch(e){u=[6,e],r=0}finally{n=i=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,o])}}}"function"==typeof SuppressedError&&SuppressedError;var a=new Map,c={get:function(e){var t=a.get(e);if(t)return t.data},set:function(e,t){a.set(e,{data:t,timestamp:Date.now()})},delete:function(e){a.delete(e)},clear:function(){return a.clear()}},u=new Map;function o(e,t){return void 0===t&&(t={}),r(this,void 0,void 0,function(){var n,a,o,s=this;return i(this,function(l){return n=t.cacheKey||e,u.has(n)?[2,u.get(n)]:(a=c.get(n))?[2,a]:(o=fetch(e,t).then(function(e){return r(s,void 0,void 0,function(){var t;return i(this,function(r){switch(r.label){case 0:if(!e.ok)throw new Error(e.statusText);return[4,e.json()];case 1:return t=r.sent(),c.set(n,t),[2,t]}})})}).finally(function(){return u.delete(n)}),u.set(n,o),[2,o])})})}var s=new Map,l={register:function(e,t,n,r,i){s.set(e,{cacheKey:e,refetch:t,setData:n,cancel:r,cache:i})},getCache:function(e){var t;return null===(t=s.get(e))||void 0===t?void 0:t.cache},setCache:function(e,t){var n,r=s.get(e);r&&(r.cache=t,null===(n=r.setData)||void 0===n||n.call(r,function(){return t}))},setData:function(e,t){var n,r=s.get(e);if(r){var i=t(r.cache);r.cache=i,null===(n=r.setData)||void 0===n||n.call(r,function(){return i})}},cancel:function(e){var t,n;null===(n=null===(t=s.get(e))||void 0===t?void 0:t.cancel)||void 0===n||n.call(t)},invalidate:function(e){var t;e?null===(t=s.get(e))||void 0===t||t.refetch():s.forEach(function(e){return e.refetch()})},unregister:function(e){var t,n;null===(n=null===(t=s.get(e))||void 0===t?void 0:t.cancel)||void 0===n||n.call(t),s.delete(e)}},h=function(){function e(e){this.url=e,this.callbacks=new Set,this.ws=null}return e.prototype.connect=function(){var e=this;this.ws||(this.ws=new WebSocket(this.url),this.ws.onmessage=function(t){var n=JSON.parse(t.data);e.callbacks.forEach(function(e){return e(n)})})},e.prototype.subscribe=function(e){var t=this;return this.callbacks.add(e),this.connect(),function(){return t.callbacks.delete(e)}},e.prototype.disconnect=function(){var e;null===(e=this.ws)||void 0===e||e.close(),this.ws=null,this.callbacks.clear()},e}();function d(e,t,n){return r(this,void 0,void 0,function(){var r;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,4]),[4,e()];case 1:return[2,i.sent()];case 2:if(r=i.sent(),t<=0)throw r;return[4,new Promise(function(e){return setTimeout(e,n)})];case 3:return i.sent(),[2,d(e,t-1,2*n)];case 4:return[2]}})})}function f(e){var t=new AbortController,n=setTimeout(function(){return t.abort()},e);return t.signal.addEventListener("abort",function(){return clearTimeout(n)}),t.signal}function v(e,t,n){e&&(l.setData(e,function(e){return t?t(e,n):n}),l.invalidate(e))}function y(e,n){var a=this,c=n.query,u=n.variables,o=n.options,s=void 0===o?{}:o,y=t.useState(null),m=y[0],w=y[1],g=t.useState(null),K=g[0],S=g[1],T=t.useState(!1),C=T[0],k=T[1],U=t.useRef(null),D=t.useCallback(function(){return r(a,void 0,void 0,function(){var t,n,a,o,l,h=this;return i(this,function(y){switch(y.label){case 0:k(!0),S(null),null===(o=U.current)||void 0===o||o.abort(),t=new AbortController,U.current=t,n=f(null!==(l=s.timeout)&&void 0!==l?l:1e4),a=function(t,f){return void 0===t&&(t=null!==(o=s.retry)&&void 0!==o?o:3),void 0===f&&(f=null!==(l=s.retryDelay)&&void 0!==l?l:500),r(h,void 0,void 0,function(){var r,o;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,p(e,c,u,{headers:s.headers,cacheKey:s.cacheKey,method:s.method,signal:n})];case 1:return r=i.sent(),w(function(e){return s.optimisticUpdate?s.optimisticUpdate(e,r):r}),v(s.cacheKey,s.optimisticUpdate,r),[2,r];case 2:if("AbortError"===(o=i.sent()).name)throw o;return[2,d(function(){return a(t-1,2*f)},t-1,2*f)];case 3:return[2]}})})},y.label=1;case 1:return y.trys.push([1,,3,4]),[4,a()];case 2:return[2,y.sent()];case 3:return k(!1),[7];case 4:return[2]}})})},[e,c,u,s.cacheKey,s.optimisticUpdate,s.retry,s.retryDelay,s.headers,s.timeout,s.method]),E=t.useCallback(function(){var e;return null===(e=U.current)||void 0===e?void 0:e.abort()},[]);return t.useEffect(function(){var t=!0;return r(a,void 0,void 0,function(){var n;return i(this,function(r){switch(r.label){case 0:return s.prefetch&&s.cacheKey?(n=l.getCache(s.cacheKey))?(w(n),[3,3]):[3,1]:[3,3];case 1:return[4,b(e,{query:c,variables:u,options:{cacheKey:s.cacheKey,headers:s.headers,method:s.method}})];case 2:r.sent(),r.label=3;case 3:return s.autoFetch&&t&&D(),[2]}})}),s.cacheKey&&l.register(s.cacheKey,D,w,E),function(){s.cacheKey&&l.unregister(s.cacheKey),E(),t=!1}},[e,c,u,s.prefetch,s.autoFetch,s.cacheKey,s.method]),t.useEffect(function(){if(s.staleTime){var e=setInterval(D,s.staleTime);return function(){return clearInterval(e)}}},[D,s.staleTime]),t.useEffect(function(){if(s.subscriptionUrl){var e=new h(s.subscriptionUrl).subscribe(function(e){w(function(t){return s.optimisticUpdate?s.optimisticUpdate(t,e):e}),v(s.cacheKey,s.optimisticUpdate,e)});return function(){return e()}}},[s.subscriptionUrl,s.optimisticUpdate,s.cacheKey]),{data:m,error:K,loading:C,refetch:D,mutate:w,cancel:E}}function p(e,t,a,c){var u;return r(this,void 0,void 0,function(){var r;return i(this,function(i){switch(i.label){case 0:return t?[4,o(e,{method:"POST",headers:n({"Content-Type":"application/json"},null==c?void 0:c.headers),body:JSON.stringify({query:t,variables:a}),cacheKey:null==c?void 0:c.cacheKey,signal:null==c?void 0:c.signal})]:[3,2];case 1:return[2,i.sent().data];case 2:return r=null!==(u=null==c?void 0:c.method)&&void 0!==u?u:"GET",[4,o(e,{method:r,headers:null==c?void 0:c.headers,cacheKey:null==c?void 0:c.cacheKey,signal:null==c?void 0:c.signal})];case 3:return[2,i.sent()]}})})}function b(e,t){var n=t.query,a=t.variables,c=t.options,u=void 0===c?{}:c;return r(this,void 0,void 0,function(){var t,r,c,o,s;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),t=u.cacheKey,r=u.headers,c=u.method,[4,p(e,n,a,{headers:r,cacheKey:t,method:c})];case 1:return o=i.sent(),t&&l.setCache(t,o),[2,o];case 2:return s=i.sent(),console.error("Prefetch failed",s),[2,null];case 3:return[2]}})})}e.SubscriptionManager=h,e.cache=c,e.createTimeoutSignal=f,e.prefetchQuery=b,e.queryRegistry=l,e.retryOperation=d,e.updateCache=v,e.useGraphQLMutation=function(e,a){var c=this,u=a.mutation,s=a.variables,h=a.cacheKey,v=a.optimisticUpdate,y=a.retry,p=void 0===y?3:y,b=a.retryDelay,m=void 0===b?500:b,w=a.headers,g=a.timeout,K=void 0===g?1e4:g,S=t.useRef(null),T=t.useCallback(function(){return r(c,void 0,void 0,function(){var t,a,c,y,b=this;return i(this,function(g){switch(g.label){case 0:null===(y=S.current)||void 0===y||y.abort(),t=new AbortController,S.current=t,a=f(K),c=function(t,f){return r(b,void 0,void 0,function(){var r,y;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,o(e,{method:"POST",headers:n({"Content-Type":"application/json"},w),body:JSON.stringify({query:u,variables:s}),signal:a,cacheKey:h})];case 1:return r=i.sent(),v&&h&&l.setData(h,function(e){return v(e,r.data)}),h&&l.invalidate(h),[2,r.data];case 2:if("AbortError"===(y=i.sent()).name)throw y;return[2,d(function(){return c(t-1,2*f)},t-1,2*f)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,c(p,m)];case 2:return[2,g.sent()];case 3:return[7];case 4:return[2]}})})},[e,u,s,h,v,p,m,w,K]),C=t.useCallback(function(){var e;null===(e=S.current)||void 0===e||e.abort()},[]);return{mutate:T,cancel:C}},e.useGraphQLQuery=function(e,t){return y(e,{query:t.query,variables:t.variables,options:{cacheKey:t.cacheKey,staleTime:t.staleTime,headers:t.headers,timeout:t.timeout,retry:t.retry,retryDelay:t.retryDelay,optimisticUpdate:t.optimisticUpdate}})},e.useHybridQuery=y,e.useMutation=function(e,a){var c=this,u=t.useState(!1),s=u[0],h=u[1],v=t.useState(null),y=v[0],p=v[1],b=t.useRef(null),m=t.useCallback(function(t,u){return void 0===u&&(u="POST"),r(c,void 0,void 0,function(){var c,s,v,y,m,w=this;return i(this,function(g){switch(g.label){case 0:h(!0),p(null),null===(y=b.current)||void 0===y||y.abort(),c=new AbortController,b.current=c,s=f(null!==(m=null==a?void 0:a.timeout)&&void 0!==m?m:1e4),v=function(c,h){return void 0===c&&(c=null!==(y=null==a?void 0:a.retry)&&void 0!==y?y:3),void 0===h&&(h=null!==(m=null==a?void 0:a.retryDelay)&&void 0!==m?m:500),r(w,void 0,void 0,function(){var r,f,y;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,o(e,{method:u,headers:n({"Content-Type":"application/json"},null==a?void 0:a.headers),body:JSON.stringify(t),cacheKey:null==a?void 0:a.cacheKey,signal:s})];case 1:return r=i.sent(),(null==a?void 0:a.optimisticUpdate)&&a.cacheKey&&(l.setData(a.cacheKey,function(e){return a.optimisticUpdate(e,r)}),l.invalidate(a.cacheKey)),null===(y=null==a?void 0:a.onSuccess)||void 0===y||y.call(a,r),[2,r];case 2:if("AbortError"===(f=i.sent()).name)throw f;return[2,d(function(){return v(c-1,2*h)},c-1,2*h)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,v()];case 2:return[2,g.sent()];case 3:return h(!1),[7];case 4:return[2]}})})},[e,a]),w=t.useCallback(function(){var e;return null===(e=b.current)||void 0===e?void 0:e.abort()},[]);return{mutate:m,loading:s,error:y,cancel:w}},e.useQuery=function(e,t){return y(e,t)},Object.defineProperty(e,"__esModule",{value:!0})});
|