reactive-query-z 1.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/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  ---
7
7
 
8
- **reactive-fetch** is a lightweight, reactive, and fully-featured data-fetching and state orchestration library for React.
8
+ **reactive-query-z** is a lightweight, reactive, and fully-featured data-fetching and state orchestration library for React.
9
9
  It is a **React Query alternative** with built-in support for REST, GraphQL, real-time subscriptions, caching, optimistic updates, and global query invalidation.
10
10
 
11
11
  ***Note:*** For full-featured query management, see [React Query](https://tanstack.com/query/v4)
@@ -1,6 +1,8 @@
1
1
  export declare const queryRegistry: {
2
- register: <T = any>(cacheKey: string, refetch: () => void, setData?: ((updater: (prev?: T | undefined) => T) => void) | undefined, cancel?: () => void) => void;
3
- setData: <T_1 = any>(cacheKey: string, updater: (prev?: T_1 | undefined) => T_1) => void;
2
+ register: <T = any>(cacheKey: string, refetch: () => void, setData?: ((updater: (prev?: T | undefined) => T) => void) | undefined, cancel?: () => void, initialCache?: T | undefined) => void;
3
+ getCache: <T_1 = any>(cacheKey: string) => T_1 | undefined;
4
+ setCache: <T_2 = any>(cacheKey: string, value: T_2) => void;
5
+ setData: <T_3 = any>(cacheKey: string, updater: (prev?: T_3 | undefined) => T_3) => void;
4
6
  cancel: (cacheKey: string) => void;
5
7
  invalidate: (cacheKey?: string) => void;
6
8
  unregister: (cacheKey: string) => void;
@@ -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
- export interface GraphQLMutationOptions<TData, TVariables = any> {
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,15 +1,5 @@
1
1
  /// <reference types="react" />
2
- export interface GraphQueryOptions<T> {
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
- optimisticUpdate?: (prevData: T | null, newData: T) => T;
12
- }
2
+ import { GraphQueryOptions } from "./types";
13
3
  export declare function useGraphQLQuery<T>(endpoint: string, options: GraphQueryOptions<T>): {
14
4
  data: T | null;
15
5
  error: Error | null;
@@ -1,14 +1,5 @@
1
1
  /// <reference types="react" />
2
- export interface HybridQueryOptions<T> {
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
- }
2
+ import { HybridQueryOptions } from "./types";
12
3
  export interface HybridQueryParams<T> {
13
4
  query?: string;
14
5
  variables?: Record<string, any>;
@@ -22,3 +13,4 @@ export declare function useHybridQuery<T>(endpoint: string, { query, variables,
22
13
  mutate: import("react").Dispatch<import("react").SetStateAction<T | null>>;
23
14
  cancel: () => void | undefined;
24
15
  };
16
+ export declare function prefetchQuery<T>(endpoint: string, { query, variables, options }: HybridQueryParams<T>): Promise<T | null>;
@@ -1,13 +1,4 @@
1
- export interface MutationOptions<T> {
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,14 +1,5 @@
1
1
  /// <reference types="react" />
2
- export interface QueryOptions<T> {
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
- optimisticUpdate?: (prevData: T | null, newData: T) => T;
11
- }
2
+ import { QueryOptions } from "./types";
12
3
  export declare function useQuery<T>(endpoint: string, options: QueryOptions<T>): {
13
4
  data: T | null;
14
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 u(e){try{c(n.next(e))}catch(e){a(e)}}function o(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(u,o)}c((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:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=o(0),u.throw=o(1),u.return=o(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function o(o){return function(c){return function(o){if(r)throw new TypeError("Generator is already executing.");for(;u&&(u=0,o[0]&&(a=0)),a;)try{if(r=1,n&&(i=2&o[0]?n.return:o[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,o[1])).done)return i;switch(n=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++,n=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],n=0}finally{r=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,c])}}}"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()}},u=new Map;function o(e,t){return void 0===t&&(t={}),r(this,void 0,void 0,function(){var i,o,c,s=this;return n(this,function(l){return i=t.cacheKey||e,u.has(i)?[2,u.get(i)]:(o=a.get(i))?[2,o]:(c=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 u.delete(i)}),u.set(i,c),[2,c])})})}var c=new Map,s={register:function(e,t,r,n){c.set(e,{cacheKey:e,refetch:t,setData:r,cancel:n})},setData:function(e,t){var r=c.get(e);(null==r?void 0:r.setData)&&r.setData(t)},cancel:function(e){var t,r=c.get(e);null===(t=null==r?void 0:r.cancel)||void 0===t||t.call(r)},invalidate:function(e){var t;e?null===(t=c.get(e))||void 0===t||t.refetch():c.forEach(function(e){return e.refetch()})},unregister:function(e){var t,r;null===(r=null===(t=c.get(e))||void 0===t?void 0:t.cancel)||void 0===r||r.call(t),c.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 f(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,f(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 h(e,t,r){e&&(s.setData(e,function(e){return t?t(e,r):r}),s.invalidate(e))}function v(i,a){var u=this,c=a.query,v=a.variables,p=a.options,y=void 0===p?{}:p,b=e.useState(null),m=b[0],w=b[1],g=e.useState(null),K=g[0],S=g[1],x=e.useState(!1),k=x[0],T=x[1],U=e.useRef(null),C=e.useCallback(function(){return r(u,void 0,void 0,function(){var e,a,u,s,l,p=this;return n(this,function(b){switch(b.label){case 0:T(!0),S(null),null===(s=U.current)||void 0===s||s.abort(),e=new AbortController,U.current=e,a=d(null!==(l=y.timeout)&&void 0!==l?l:1e4),u=function(e,d){return void 0===e&&(e=null!==(s=y.retry)&&void 0!==s?s:3),void 0===d&&(d=null!==(l=y.retryDelay)&&void 0!==l?l:500),r(p,void 0,void 0,function(){var r,s,l;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,5,,6]),c?[4,o(i,{method:"POST",headers:t({"Content-Type":"application/json"},y.headers),body:JSON.stringify({query:c,variables:v}),cacheKey:y.cacheKey,signal:a})]:[3,2];case 1:return s=n.sent(),r=s.data,[3,4];case 2:return[4,o(i,{cacheKey:y.cacheKey,signal:a,headers:y.headers})];case 3:r=n.sent(),n.label=4;case 4:return w(function(e){return y.optimisticUpdate?y.optimisticUpdate(e,r):r}),h(y.cacheKey,y.optimisticUpdate,r),[2,r];case 5:if("AbortError"===(l=n.sent()).name)throw l;return[2,f(function(){return u(e-1,2*d)},e-1,2*d)];case 6:return[2]}})})},b.label=1;case 1:return b.trys.push([1,,3,4]),[4,u()];case 2:return[2,b.sent()];case 3:return T(!1),[7];case 4:return[2]}})})},[i,c,v,y.cacheKey,y.optimisticUpdate,y.retry,y.retryDelay,y.headers,y.timeout]),D=e.useCallback(function(){var e;return null===(e=U.current)||void 0===e?void 0:e.abort()},[]);return e.useEffect(function(){return y.cacheKey&&s.register(y.cacheKey,C,w,D),function(){y.cacheKey&&s.unregister(y.cacheKey),D()}},[y.cacheKey,C,w,D]),e.useEffect(function(){if(y.staleTime){var e=setInterval(C,y.staleTime);return function(){return clearInterval(e)}}},[C,y.staleTime]),e.useEffect(function(){if(y.subscriptionUrl){var e=new l(y.subscriptionUrl).subscribe(function(e){w(function(t){return y.optimisticUpdate?y.optimisticUpdate(t,e):e}),h(y.cacheKey,y.optimisticUpdate,e)});return function(){return e()}}},[y.subscriptionUrl,y.optimisticUpdate,y.cacheKey]),{data:m,error:K,loading:k,refetch:C,mutate:w,cancel:D}}exports.SubscriptionManager=l,exports.cache=a,exports.createTimeoutSignal=d,exports.queryRegistry=s,exports.retryOperation=f,exports.updateCache=h,exports.useGraphQLMutation=function(i,a){var u=this,c=a.mutation,l=a.variables,h=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(u,void 0,void 0,function(){var e,a,u,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),u=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,o(i,{method:"POST",headers:t({"Content-Type":"application/json"},w),body:JSON.stringify({query:c,variables:l}),signal:a,cacheKey:h})];case 1:return r=n.sent(),v&&h&&s.setData(h,function(e){return v(e,r.data)}),h&&s.invalidate(h),[2,r.data];case 2:if("AbortError"===(p=n.sent()).name)throw p;return[2,f(function(){return u(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,u(y,m)];case 2:return[2,g.sent()];case 3:return[7];case 4:return[2]}})})},[i,c,l,h,v,y,m,w,K]),k=e.useCallback(function(){var e;null===(e=S.current)||void 0===e||e.abort()},[]);return{mutate:x,cancel:k}},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(i,a){var u=this,c=e.useState(!1),l=c[0],h=c[1],v=e.useState(null),p=v[0],y=v[1],b=e.useRef(null),m=e.useCallback(function(e,c){return void 0===c&&(c="POST"),r(u,void 0,void 0,function(){var u,l,v,p,m,w=this;return n(this,function(g){switch(g.label){case 0:h(!0),y(null),null===(p=b.current)||void 0===p||p.abort(),u=new AbortController,b.current=u,l=d(null!==(m=null==a?void 0:a.timeout)&&void 0!==m?m:1e4),v=function(u,d){return void 0===u&&(u=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,h,p;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),[4,o(i,{method:c,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"===(h=n.sent()).name)throw h;return[2,f(function(){return v(u-1,2*d)},u-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 h(!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 v(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 { useHybridQuery } from "./hooks/useHybridQuery";
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";
@@ -1 +1 @@
1
- import{useState as t,useRef as e,useCallback as n,useEffect as r}from"react";var i=function(){return i=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t},i.apply(this,arguments)};function a(t,e,n,r){return new(n||(n=Promise))(function(i,a){function o(t){try{u(r.next(t))}catch(t){a(t)}}function c(t){try{u(r.throw(t))}catch(t){a(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(o,c)}u((r=r.apply(t,e||[])).next())})}function o(t,e){var n,r,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},o=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return o.next=c(0),o.throw=c(1),o.return=c(2),"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function c(c){return function(u){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,c[0]&&(a=0)),a;)try{if(n=1,r&&(i=2&c[0]?r.return:c[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,c[1])).done)return i;switch(r=0,i&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return a.label++,{value:c[1],done:!1};case 5:a.label++,r=c[1],c=[0];continue;case 7:c=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==c[0]&&2!==c[0])){a=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){a.label=c[1];break}if(6===c[0]&&a.label<i[1]){a.label=i[1],i=c;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(c);break}i[2]&&a.ops.pop(),a.trys.pop();continue}c=e.call(t,a)}catch(t){c=[6,t],r=0}finally{n=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}}"function"==typeof SuppressedError&&SuppressedError;var c=new Map,u={get:function(t){var e=c.get(t);if(e)return e.data},set:function(t,e){c.set(t,{data:e,timestamp:Date.now()})},delete:function(t){c.delete(t)},clear:function(){return c.clear()}},s=new Map;function l(t,e){return void 0===e&&(e={}),a(this,void 0,void 0,function(){var n,r,i,c=this;return o(this,function(l){return n=e.cacheKey||t,s.has(n)?[2,s.get(n)]:(r=u.get(n))?[2,r]:(i=fetch(t,e).then(function(t){return a(c,void 0,void 0,function(){var e;return o(this,function(r){switch(r.label){case 0:if(!t.ok)throw new Error(t.statusText);return[4,t.json()];case 1:return e=r.sent(),u.set(n,e),[2,e]}})})}).finally(function(){return s.delete(n)}),s.set(n,i),[2,i])})})}var f=new Map,d={register:function(t,e,n,r){f.set(t,{cacheKey:t,refetch:e,setData:n,cancel:r})},setData:function(t,e){var n=f.get(t);(null==n?void 0:n.setData)&&n.setData(e)},cancel:function(t){var e,n=f.get(t);null===(e=null==n?void 0:n.cancel)||void 0===e||e.call(n)},invalidate:function(t){var e;t?null===(e=f.get(t))||void 0===e||e.refetch():f.forEach(function(t){return t.refetch()})},unregister:function(t){var e,n;null===(n=null===(e=f.get(t))||void 0===e?void 0:e.cancel)||void 0===n||n.call(e),f.delete(t)}},h=function(){function t(t){this.url=t,this.callbacks=new Set,this.ws=null}return t.prototype.connect=function(){var t=this;this.ws||(this.ws=new WebSocket(this.url),this.ws.onmessage=function(e){var n=JSON.parse(e.data);t.callbacks.forEach(function(t){return t(n)})})},t.prototype.subscribe=function(t){var e=this;return this.callbacks.add(t),this.connect(),function(){return e.callbacks.delete(t)}},t.prototype.disconnect=function(){var t;null===(t=this.ws)||void 0===t||t.close(),this.ws=null,this.callbacks.clear()},t}();function v(t,e,n){return a(this,void 0,void 0,function(){var r;return o(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,4]),[4,t()];case 1:return[2,i.sent()];case 2:if(r=i.sent(),e<=0)throw r;return[4,new Promise(function(t){return setTimeout(t,n)})];case 3:return i.sent(),[2,v(t,e-1,2*n)];case 4:return[2]}})})}function p(t){var e=new AbortController,n=setTimeout(function(){return e.abort()},t);return e.signal.addEventListener("abort",function(){return clearTimeout(n)}),e.signal}function y(t,e,n){t&&(d.setData(t,function(t){return e?e(t,n):n}),d.invalidate(t))}function b(c,u){var s=this,f=u.query,b=u.variables,m=u.options,w=void 0===m?{}:m,g=t(null),K=g[0],T=g[1],U=t(null),D=U[0],S=U[1],O=t(!1),k=O[0],E=O[1],j=e(null),x=n(function(){return a(s,void 0,void 0,function(){var t,e,n,r,u,s=this;return o(this,function(d){switch(d.label){case 0:E(!0),S(null),null===(r=j.current)||void 0===r||r.abort(),t=new AbortController,j.current=t,e=p(null!==(u=w.timeout)&&void 0!==u?u:1e4),n=function(t,d){return void 0===t&&(t=null!==(r=w.retry)&&void 0!==r?r:3),void 0===d&&(d=null!==(u=w.retryDelay)&&void 0!==u?u:500),a(s,void 0,void 0,function(){var r,a,u;return o(this,function(o){switch(o.label){case 0:return o.trys.push([0,5,,6]),f?[4,l(c,{method:"POST",headers:i({"Content-Type":"application/json"},w.headers),body:JSON.stringify({query:f,variables:b}),cacheKey:w.cacheKey,signal:e})]:[3,2];case 1:return a=o.sent(),r=a.data,[3,4];case 2:return[4,l(c,{cacheKey:w.cacheKey,signal:e,headers:w.headers})];case 3:r=o.sent(),o.label=4;case 4:return T(function(t){return w.optimisticUpdate?w.optimisticUpdate(t,r):r}),y(w.cacheKey,w.optimisticUpdate,r),[2,r];case 5:if("AbortError"===(u=o.sent()).name)throw u;return[2,v(function(){return n(t-1,2*d)},t-1,2*d)];case 6:return[2]}})})},d.label=1;case 1:return d.trys.push([1,,3,4]),[4,n()];case 2:return[2,d.sent()];case 3:return E(!1),[7];case 4:return[2]}})})},[c,f,b,w.cacheKey,w.optimisticUpdate,w.retry,w.retryDelay,w.headers,w.timeout]),A=n(function(){var t;return null===(t=j.current)||void 0===t?void 0:t.abort()},[]);return r(function(){return w.cacheKey&&d.register(w.cacheKey,x,T,A),function(){w.cacheKey&&d.unregister(w.cacheKey),A()}},[w.cacheKey,x,T,A]),r(function(){if(w.staleTime){var t=setInterval(x,w.staleTime);return function(){return clearInterval(t)}}},[x,w.staleTime]),r(function(){if(w.subscriptionUrl){var t=new h(w.subscriptionUrl).subscribe(function(t){T(function(e){return w.optimisticUpdate?w.optimisticUpdate(e,t):t}),y(w.cacheKey,w.optimisticUpdate,t)});return function(){return t()}}},[w.subscriptionUrl,w.optimisticUpdate,w.cacheKey]),{data:K,error:D,loading:k,refetch:x,mutate:T,cancel:A}}function m(t,e){return b(t,e)}function w(r,c){var u=this,s=t(!1),f=s[0],h=s[1],y=t(null),b=y[0],m=y[1],w=e(null),g=n(function(t,e){return void 0===e&&(e="POST"),a(u,void 0,void 0,function(){var n,u,s,f,y,b=this;return o(this,function(g){switch(g.label){case 0:h(!0),m(null),null===(f=w.current)||void 0===f||f.abort(),n=new AbortController,w.current=n,u=p(null!==(y=null==c?void 0:c.timeout)&&void 0!==y?y:1e4),s=function(n,h){return void 0===n&&(n=null!==(f=null==c?void 0:c.retry)&&void 0!==f?f:3),void 0===h&&(h=null!==(y=null==c?void 0:c.retryDelay)&&void 0!==y?y:500),a(b,void 0,void 0,function(){var a,f,p;return o(this,function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,l(r,{method:e,headers:i({"Content-Type":"application/json"},null==c?void 0:c.headers),body:JSON.stringify(t),cacheKey:null==c?void 0:c.cacheKey,signal:u})];case 1:return a=o.sent(),(null==c?void 0:c.optimisticUpdate)&&c.cacheKey&&(d.setData(c.cacheKey,function(t){return c.optimisticUpdate(t,a)}),d.invalidate(c.cacheKey)),null===(p=null==c?void 0:c.onSuccess)||void 0===p||p.call(c,a),[2,a];case 2:if("AbortError"===(f=o.sent()).name)throw f;return[2,v(function(){return s(n-1,2*h)},n-1,2*h)];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 h(!1),[7];case 4:return[2]}})})},[r,c]),K=n(function(){var t;return null===(t=w.current)||void 0===t?void 0:t.abort()},[]);return{mutate:g,loading:f,error:b,cancel:K}}function g(t,e){return b(t,{query:e.query,variables:e.variables,options:{cacheKey:e.cacheKey,staleTime:e.staleTime,headers:e.headers,timeout:e.timeout,retry:e.retry,retryDelay:e.retryDelay,optimisticUpdate:e.optimisticUpdate}})}function K(t,r){var c=this,u=r.mutation,s=r.variables,f=r.cacheKey,h=r.optimisticUpdate,y=r.retry,b=void 0===y?3:y,m=r.retryDelay,w=void 0===m?500:m,g=r.headers,K=r.timeout,T=void 0===K?1e4:K,U=e(null),D=n(function(){return a(c,void 0,void 0,function(){var e,n,r,c,y=this;return o(this,function(m){switch(m.label){case 0:null===(c=U.current)||void 0===c||c.abort(),e=new AbortController,U.current=e,n=p(T),r=function(e,c){return a(y,void 0,void 0,function(){var a,p;return o(this,function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,l(t,{method:"POST",headers:i({"Content-Type":"application/json"},g),body:JSON.stringify({query:u,variables:s}),signal:n,cacheKey:f})];case 1:return a=o.sent(),h&&f&&d.setData(f,function(t){return h(t,a.data)}),f&&d.invalidate(f),[2,a.data];case 2:if("AbortError"===(p=o.sent()).name)throw p;return[2,v(function(){return r(e-1,2*c)},e-1,2*c)];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]}})})},[t,u,s,f,h,b,w,g,T]),S=n(function(){var t;null===(t=U.current)||void 0===t||t.abort()},[]);return{mutate:D,cancel:S}}export{h as SubscriptionManager,u as cache,p as createTimeoutSignal,d as queryRegistry,v as retryOperation,y as updateCache,K as useGraphQLMutation,g as useGraphQLQuery,b as useHybridQuery,w as useMutation,m 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};
@@ -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 u(e){try{c(r.next(e))}catch(e){a(e)}}function o(e){try{c(r.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(u,o)}c((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:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=o(0),u.throw=o(1),u.return=o(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function o(o){return function(c){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;u&&(u=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,c])}}}"function"==typeof SuppressedError&&SuppressedError;var a=new Map,u={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()}},o=new Map;function c(e,t){return void 0===t&&(t={}),r(this,void 0,void 0,function(){var n,a,c,s=this;return i(this,function(l){return n=t.cacheKey||e,o.has(n)?[2,o.get(n)]:(a=u.get(n))?[2,a]:(c=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(),u.set(n,t),[2,t]}})})}).finally(function(){return o.delete(n)}),o.set(n,c),[2,c])})})}var s=new Map,l={register:function(e,t,n,r){s.set(e,{cacheKey:e,refetch:t,setData:n,cancel:r})},setData:function(e,t){var n=s.get(e);(null==n?void 0:n.setData)&&n.setData(t)},cancel:function(e){var t,n=s.get(e);null===(t=null==n?void 0:n.cancel)||void 0===t||t.call(n)},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 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 h(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 p(e,a){var u=this,o=a.query,s=a.variables,p=a.options,y=void 0===p?{}:p,b=t.useState(null),m=b[0],w=b[1],g=t.useState(null),K=g[0],S=g[1],T=t.useState(!1),k=T[0],U=T[1],C=t.useRef(null),D=t.useCallback(function(){return r(u,void 0,void 0,function(){var t,a,u,l,f,p=this;return i(this,function(b){switch(b.label){case 0:U(!0),S(null),null===(l=C.current)||void 0===l||l.abort(),t=new AbortController,C.current=t,a=h(null!==(f=y.timeout)&&void 0!==f?f:1e4),u=function(t,h){return void 0===t&&(t=null!==(l=y.retry)&&void 0!==l?l:3),void 0===h&&(h=null!==(f=y.retryDelay)&&void 0!==f?f:500),r(p,void 0,void 0,function(){var r,l,f;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,5,,6]),o?[4,c(e,{method:"POST",headers:n({"Content-Type":"application/json"},y.headers),body:JSON.stringify({query:o,variables:s}),cacheKey:y.cacheKey,signal:a})]:[3,2];case 1:return l=i.sent(),r=l.data,[3,4];case 2:return[4,c(e,{cacheKey:y.cacheKey,signal:a,headers:y.headers})];case 3:r=i.sent(),i.label=4;case 4:return w(function(e){return y.optimisticUpdate?y.optimisticUpdate(e,r):r}),v(y.cacheKey,y.optimisticUpdate,r),[2,r];case 5:if("AbortError"===(f=i.sent()).name)throw f;return[2,d(function(){return u(t-1,2*h)},t-1,2*h)];case 6:return[2]}})})},b.label=1;case 1:return b.trys.push([1,,3,4]),[4,u()];case 2:return[2,b.sent()];case 3:return U(!1),[7];case 4:return[2]}})})},[e,o,s,y.cacheKey,y.optimisticUpdate,y.retry,y.retryDelay,y.headers,y.timeout]),O=t.useCallback(function(){var e;return null===(e=C.current)||void 0===e?void 0:e.abort()},[]);return t.useEffect(function(){return y.cacheKey&&l.register(y.cacheKey,D,w,O),function(){y.cacheKey&&l.unregister(y.cacheKey),O()}},[y.cacheKey,D,w,O]),t.useEffect(function(){if(y.staleTime){var e=setInterval(D,y.staleTime);return function(){return clearInterval(e)}}},[D,y.staleTime]),t.useEffect(function(){if(y.subscriptionUrl){var e=new f(y.subscriptionUrl).subscribe(function(e){w(function(t){return y.optimisticUpdate?y.optimisticUpdate(t,e):e}),v(y.cacheKey,y.optimisticUpdate,e)});return function(){return e()}}},[y.subscriptionUrl,y.optimisticUpdate,y.cacheKey]),{data:m,error:K,loading:k,refetch:D,mutate:w,cancel:O}}e.SubscriptionManager=f,e.cache=u,e.createTimeoutSignal=h,e.queryRegistry=l,e.retryOperation=d,e.updateCache=v,e.useGraphQLMutation=function(e,a){var u=this,o=a.mutation,s=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=t.useRef(null),T=t.useCallback(function(){return r(u,void 0,void 0,function(){var t,a,u,p,b=this;return i(this,function(g){switch(g.label){case 0:null===(p=S.current)||void 0===p||p.abort(),t=new AbortController,S.current=t,a=h(K),u=function(t,h){return r(b,void 0,void 0,function(){var r,p;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,c(e,{method:"POST",headers:n({"Content-Type":"application/json"},w),body:JSON.stringify({query:o,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"===(p=i.sent()).name)throw p;return[2,d(function(){return u(t-1,2*h)},t-1,2*h)];case 3:return[2]}})})},g.label=1;case 1:return g.trys.push([1,,3,4]),[4,u(y,m)];case 2:return[2,g.sent()];case 3:return[7];case 4:return[2]}})})},[e,o,s,f,v,y,m,w,K]),k=t.useCallback(function(){var e;null===(e=S.current)||void 0===e||e.abort()},[]);return{mutate:T,cancel:k}},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 u=this,o=t.useState(!1),s=o[0],f=o[1],v=t.useState(null),p=v[0],y=v[1],b=t.useRef(null),m=t.useCallback(function(t,o){return void 0===o&&(o="POST"),r(u,void 0,void 0,function(){var u,s,v,p,m,w=this;return i(this,function(g){switch(g.label){case 0:f(!0),y(null),null===(p=b.current)||void 0===p||p.abort(),u=new AbortController,b.current=u,s=h(null!==(m=null==a?void 0:a.timeout)&&void 0!==m?m:1e4),v=function(u,f){return void 0===u&&(u=null!==(p=null==a?void 0:a.retry)&&void 0!==p?p: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,h,p;return i(this,function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,c(e,{method:o,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===(p=null==a?void 0:a.onSuccess)||void 0===p||p.call(a,r),[2,r];case 2:if("AbortError"===(h=i.sent()).name)throw h;return[2,d(function(){return v(u-1,2*f)},u-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:p,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})});
@@ -0,0 +1 @@
1
+ export declare function prefetchData<T>(fetchData: () => Promise<T>, cacheKey: string, cacheRegistry: any): void;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "reactive-query-z",
3
- "version": "1.0.0",
4
- "description": "Intent-first orchestration engine for React applications with support for REST and GraphQL",
3
+ "version": "2.1.0",
4
+ "description": "Query is a lightweight, reactive library for data fetching and state orchestration in React.",
5
5
  "license": "MIT",
6
6
  "author": "Delpi.Kye",
7
7