Parcourir la source

pub-query begins

asmer il y a 3 mois
Parent
commit
a0f28faf3e
1 fichiers modifiés avec 11 ajouts et 5 suppressions
  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}