Browse Source

some comments to pubQuery

asmer 1 month ago
parent
commit
f343609ae8
2 changed files with 47 additions and 3 deletions
  1. 6 2
      src/App.tsx
  2. 41 1
      src/lib/pub/query/index.ts

+ 6 - 2
src/App.tsx

@@ -108,7 +108,7 @@ const Good = ({good, children}) => {
             <div className='Good'>
                 <h2>{good.name}</h2>
                 <p>{good.description}</p>
-                <money>{good.price}</money>
+                <span>{good.price}</span>
                 {children}
             </div>
         : <h3>null</h3>
@@ -135,7 +135,11 @@ const Goods = ({children}) =>
 </div>
 
 const PageCategory = ({data}) => {
-    usePub(queryPub)
+    //usePub(queryPub) //IS IT REQUIRED?, or route mechanics enough (looks like this required for auto-refresh queries in PUB)
+    //if its required, is useCategoryQuery gives same result?
+    //yes, exactly. it required due to auto update of queryPub.
+    //
+    //useCategoryQuery({_id: useParams()._id}) //here we need id from url...
     console.log('page category update')
     return (
     <>

+ 41 - 1
src/lib/pub/query/index.ts

@@ -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)