소스 검색

pub-query begins

asmer 3 달 전
부모
커밋
a0f28faf3e
1개의 변경된 파일11개의 추가작업 그리고 5개의 파일을 삭제
  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}