|
@@ -36,24 +36,43 @@ export default (options:CreateQueryPubOptions) => {
|
|
|
|
|
|
options = {...defaultOptions, ...options}
|
|
|
|
|
|
+ /*
|
|
|
+ * function to remember basic transport function (like fetch or gql) in closure for further usage
|
|
|
+ *
|
|
|
+ * @param {function} queryFunc? - basic transport function. If omitted, used function from options of createQueryPub
|
|
|
+ * @returns {function} "createQuery", which receives promise name for pub, isMutation flag, cacheTagsFunc and query function arguments
|
|
|
+ */
|
|
|
const withQueryFunc = (queryFunc?: QueryFuncType):Function => {
|
|
|
return ({promiseName, isMutation, cacheTagsFunc, args=[]}) => {
|
|
|
const qF = queryFunc || options.queryFunc
|
|
|
const defaultArgs = args
|
|
|
|
|
|
+ /*
|
|
|
+ * Concats default arguments from withQueryFunc resulting function with passed arguments
|
|
|
+ *
|
|
|
+ */
|
|
|
const mixArgs = (args:any[]) =>
|
|
|
Array.isArray(defaultArgs) ?
|
|
|
[...defaultArgs, ...args] :
|
|
|
(typeof defaultArgs === 'function' ?
|
|
|
defaultArgs(...args) : args)
|
|
|
|
|
|
+ /*
|
|
|
+ * used to create key for pub
|
|
|
+ */
|
|
|
const stringifyArgs = args =>
|
|
|
JSON.stringify(mixArgs(args))
|
|
|
|
|
|
+ /*
|
|
|
+ * invokes query, using basic transport function and mixArgs
|
|
|
+ */
|
|
|
const makeQuery = (...args:any[]) => {
|
|
|
return qF(...mixArgs(args))
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ * Checks tags intersection for invalidation of some cached query result
|
|
|
+ */
|
|
|
const intersectTags = (first:Array<any>, second:Array<any>) =>
|
|
|
first.find(firstItem => second.find(secondItem => firstItem.type === secondItem.type && firstItem.id === secondItem.id))
|
|
|
|
|
@@ -70,6 +89,11 @@ export default (options:CreateQueryPubOptions) => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ * Performs query, if needed, using makeQuery or forceQuery
|
|
|
+ * Checks is query mutation or pure query, and options like avoidRepeatInProgress or
|
|
|
+ * cache timeouts. So, in some circumstances, query skipped.
|
|
|
+ */
|
|
|
const query = (...args:any[]) => {
|
|
|
if (isMutation){
|
|
|
if (options.avoidRepeatInProgress && queryPub.mutations[promiseName]?.refreshing){
|
|
@@ -97,6 +121,12 @@ export default (options:CreateQueryPubOptions) => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ * Forces query using makeQuery, but maintain pub structure. (cache timeout, status and so)
|
|
|
+ * Always runs query for Mutation
|
|
|
+ * The only reason to skip query for Query is avoidRepeatInProgress (deduplicate queries). Maybe this logic should be moved to query function
|
|
|
+ *
|
|
|
+ */
|
|
|
const forceQuery = (...args:any[]) => {
|
|
|
if (isMutation) return query(...args)
|
|
|
|
|
@@ -112,7 +142,6 @@ export default (options:CreateQueryPubOptions) => {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
if (options.avoidRepeatInProgress && queryPub.queries[promiseName]?.[stringifiedArgs]?.refreshing){
|
|
|
return currentPromises.queries[promiseName][stringifiedArgs]
|
|
|
}
|
|
@@ -147,14 +176,25 @@ export default (options:CreateQueryPubOptions) => {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ * Creates hook name from promise name like rtk query
|
|
|
+ */
|
|
|
const hookName = `use${promiseName[0].toUpperCase() + promiseName.slice(1) + (isMutation ? 'Mutation' : 'Query')}`
|
|
|
|
|
|
+ /*
|
|
|
+ * Mutation hook. Returns query function and current pub data
|
|
|
+ */
|
|
|
const useMutation = () => {
|
|
|
usePub(queryPub.mutations)
|
|
|
|
|
|
return [query, queryPub.mutations[promiseName] || {status: 'INIT'}]
|
|
|
}
|
|
|
|
|
|
+ /*
|
|
|
+ * Query hook. Calls query function with arguments passed.
|
|
|
+ * Listening for pub.
|
|
|
+ * @returns pub data
|
|
|
+ */
|
|
|
const useQuery = (...args) => {
|
|
|
query(...args)
|
|
|
|