@swan-io/graphql-client
Advanced tools
Comparing version 0.1.0-alpha14 to 0.1.0-alpha15
@@ -1084,3 +1084,3 @@ 'use strict'; | ||
exports.ClientContext = react.createContext(new exports.Client({ url: "/graphql" })); | ||
exports.useDeferredQuery = (query, { optimize = false } = {}) => { | ||
exports.useDeferredQuery = (query, { optimize = false, debounce } = {}) => { | ||
const client = react.useContext(exports.ClientContext); | ||
@@ -1091,2 +1091,3 @@ const [stableQuery] = react.useState(query); | ||
); | ||
const timeoutRef = react.useRef(void 0); | ||
const getSnapshot = react.useCallback(() => { | ||
@@ -1104,3 +1105,2 @@ return stableVariables.flatMap( | ||
}, [data]); | ||
const [isQuerying, setIsQuerying] = react.useState(false); | ||
const runQuery = react.useCallback( | ||
@@ -1114,9 +1114,31 @@ (variables) => { | ||
); | ||
setIsQuerying(true); | ||
return client.request(stableQuery, variables, { optimize }).tap(() => setIsQuerying(false)); | ||
}, | ||
[client, stableQuery, optimize] | ||
[client, optimize, stableQuery] | ||
); | ||
const [isQuerying, setIsQuerying] = react.useState(false); | ||
const exposedRunQuery = react.useCallback( | ||
(variables) => { | ||
if (timeoutRef.current !== void 0) { | ||
clearTimeout(timeoutRef.current); | ||
} | ||
setIsQuerying(true); | ||
if (debounce === void 0) { | ||
return runQuery(variables); | ||
} else { | ||
const [future, resolve] = boxed.Deferred.make(); | ||
timeoutRef.current = window.setTimeout( | ||
(variables2) => { | ||
runQuery(variables2).tap(resolve); | ||
}, | ||
debounce, | ||
variables | ||
); | ||
return future; | ||
} | ||
}, | ||
[runQuery, debounce] | ||
); | ||
const asyncDataToExpose = isQuerying ? boxed.AsyncData.Loading() : asyncData; | ||
return [asyncDataToExpose, runQuery]; | ||
return [asyncDataToExpose, exposedRunQuery]; | ||
}; | ||
@@ -1123,0 +1145,0 @@ exports.useMutation = (mutation) => { |
@@ -9,2 +9,3 @@ import { AsyncData, Result, Future } from '@swan-io/boxed'; | ||
optimize?: boolean; | ||
debounce?: number; | ||
}; | ||
@@ -15,4 +16,4 @@ type DeferredQuery<Data, Variables> = readonly [ | ||
]; | ||
declare const useDeferredQuery: <Data, Variables>(query: TypedDocumentNode<Data, Variables>, { optimize }?: DeferredQueryConfig) => DeferredQuery<Data, Variables>; | ||
declare const useDeferredQuery: <Data, Variables>(query: TypedDocumentNode<Data, Variables>, { optimize, debounce }?: DeferredQueryConfig) => DeferredQuery<Data, Variables>; | ||
export { type DeferredQuery, type DeferredQueryConfig, useDeferredQuery }; |
{ | ||
"name": "@swan-io/graphql-client", | ||
"version": "0.1.0-alpha14", | ||
"version": "0.1.0-alpha15", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A simple, typesafe GraphQL client for React", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
290562
2816