Browse Source

pub-query begins

asmer 3 tháng trước cách đây
mục cha
commit
a0f28faf3e
1 tập tin đã thay đổi với 11 bổ sung5 xóa
  1. 11 5
      src/lib/pub/query/index.ts

+ 11 - 5
src/lib/pub/query/index.ts

@@ -29,6 +29,7 @@ const defaultOptions:CreateQueryPubOptions = {
 
 export default (options:CreateQueryPubOptions) => {
     const queryPub = createPub({queries: {}, mutations: {}})
+    const currentPromises = {queries: {}, mutations: {}}
 
     options = {...defaultOptions, ...options}
 
@@ -41,14 +42,19 @@ export default (options:CreateQueryPubOptions) => {
             }
 
             const query = (...args) => {
-                if (isMutation && (!options.avoidRepeatInProgress || queryPub.mutations?.[promiseName]?.status !== 'PENDING')){
-                    queryPub.mutations[promiseName] = {status: 'PENDING'}
-                    return forceQuery(...args).then(payload => queryPub.mutations[promiseName] = {status: 'FULFILLED', payload},
-                                                    error   => queryPub.mutations[promiseName] = {status: 'REJECTED', error})
+                if (isMutation){
+                    if (options.avoidRepeatInProgress && queryPub.mutations?.[promiseName]?.status === 'PENDING'){
+                        return currentPromises.mutations[promiseName]
+                    }
+                    else {
+                        queryPub.mutations[promiseName] = {status: 'PENDING'}
+                        return currentPromises.mutations[promiseName] = forceQuery(...args).then(payload => queryPub.mutations[promiseName] = {status: 'FULFILLED', payload},
+                                                                                                 error   => queryPub.mutations[promiseName] = {status: 'REJECTED', error})
+                    }
                 }
                 if (!isMutation){
                     const stringifiedArgs = JSON.stringify(args)
-                    queryPub[promiseName][stringifiedArgs]
+                    queryPub.queries[promiseName][stringifiedArgs]
                 }
             }
             return {forceQuery, query}