Redux Saga - group similar (but not all) actions into takeLeading / takeLatest












1















I am currently using redux-saga on a project and I want to use it to also prevent requesting the same resource multiple times.



At the moment I have multiple components on the same page requesting a resource (i.e. dispatch {types: "REQUEST_POST_COMMENTS", postId: <postIdHere>})



And I have this saga:



export function* loadPostComments() {
yield takeLatest("REQUEST_POST_COMMENTS", loadPostComments);
}



My page dispatches the following actions at the same time:




  • {types: "REQUEST_POST_COMMENTS", postId: 123}

  • {types: "REQUEST_POST_COMMENTS", postId: 321}

  • {types: "REQUEST_POST_COMMENTS", postId: 123}


And my goal is to for loadPostComments to be called twice, once for post 123 and one for post 321.



I have switched to takeLeading in the beta version - but this will only call loadPostComments once.



Is there any way to define a saga using a pattern but takeLatest to group them by the action.type value? Or any way of achieving something similar?










share|improve this question



























    1















    I am currently using redux-saga on a project and I want to use it to also prevent requesting the same resource multiple times.



    At the moment I have multiple components on the same page requesting a resource (i.e. dispatch {types: "REQUEST_POST_COMMENTS", postId: <postIdHere>})



    And I have this saga:



    export function* loadPostComments() {
    yield takeLatest("REQUEST_POST_COMMENTS", loadPostComments);
    }



    My page dispatches the following actions at the same time:




    • {types: "REQUEST_POST_COMMENTS", postId: 123}

    • {types: "REQUEST_POST_COMMENTS", postId: 321}

    • {types: "REQUEST_POST_COMMENTS", postId: 123}


    And my goal is to for loadPostComments to be called twice, once for post 123 and one for post 321.



    I have switched to takeLeading in the beta version - but this will only call loadPostComments once.



    Is there any way to define a saga using a pattern but takeLatest to group them by the action.type value? Or any way of achieving something similar?










    share|improve this question

























      1












      1








      1


      1






      I am currently using redux-saga on a project and I want to use it to also prevent requesting the same resource multiple times.



      At the moment I have multiple components on the same page requesting a resource (i.e. dispatch {types: "REQUEST_POST_COMMENTS", postId: <postIdHere>})



      And I have this saga:



      export function* loadPostComments() {
      yield takeLatest("REQUEST_POST_COMMENTS", loadPostComments);
      }



      My page dispatches the following actions at the same time:




      • {types: "REQUEST_POST_COMMENTS", postId: 123}

      • {types: "REQUEST_POST_COMMENTS", postId: 321}

      • {types: "REQUEST_POST_COMMENTS", postId: 123}


      And my goal is to for loadPostComments to be called twice, once for post 123 and one for post 321.



      I have switched to takeLeading in the beta version - but this will only call loadPostComments once.



      Is there any way to define a saga using a pattern but takeLatest to group them by the action.type value? Or any way of achieving something similar?










      share|improve this question














      I am currently using redux-saga on a project and I want to use it to also prevent requesting the same resource multiple times.



      At the moment I have multiple components on the same page requesting a resource (i.e. dispatch {types: "REQUEST_POST_COMMENTS", postId: <postIdHere>})



      And I have this saga:



      export function* loadPostComments() {
      yield takeLatest("REQUEST_POST_COMMENTS", loadPostComments);
      }



      My page dispatches the following actions at the same time:




      • {types: "REQUEST_POST_COMMENTS", postId: 123}

      • {types: "REQUEST_POST_COMMENTS", postId: 321}

      • {types: "REQUEST_POST_COMMENTS", postId: 123}


      And my goal is to for loadPostComments to be called twice, once for post 123 and one for post 321.



      I have switched to takeLeading in the beta version - but this will only call loadPostComments once.



      Is there any way to define a saga using a pattern but takeLatest to group them by the action.type value? Or any way of achieving something similar?







      reactjs redux redux-saga






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 12:29









      cipacdacipacda

      655




      655
























          1 Answer
          1






          active

          oldest

          votes


















          0














          My solution



          You have to save encountered postIds and to them after:



          function* watchRequestPostComments() {
          const encounteredPostIds = new Set()

          while(true) {
          const { postId } = yield take('REQUEST_POST_COMMENTS')
          if (!encounteredPostIds.has(postId) {
          yield fork(loadPostComments, postId)
          }
          encounteredPostIds.add(postId)
          }
          }


          Explanation



          There doesn't seem to exist a particular redux-saga-esque way of doing this. In any case, you have to track these IDs.



          I have answered a similar question about throttling requests.



          P.S.: This looks like an endless recursion. Must be an unintentional name clash:



          export function* loadPostComments() {
          yield takeLatest("REQUEST_POST_COMMENTS", loadPostComments)
          }





          share|improve this answer


























          • Thank you - this seems to do the trick. The endless recursion is indeed due to an unintentional name clash while writing the question :)

            – cipacda
            Nov 21 '18 at 8:48











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393024%2fredux-saga-group-similar-but-not-all-actions-into-takeleading-takelatest%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          My solution



          You have to save encountered postIds and to them after:



          function* watchRequestPostComments() {
          const encounteredPostIds = new Set()

          while(true) {
          const { postId } = yield take('REQUEST_POST_COMMENTS')
          if (!encounteredPostIds.has(postId) {
          yield fork(loadPostComments, postId)
          }
          encounteredPostIds.add(postId)
          }
          }


          Explanation



          There doesn't seem to exist a particular redux-saga-esque way of doing this. In any case, you have to track these IDs.



          I have answered a similar question about throttling requests.



          P.S.: This looks like an endless recursion. Must be an unintentional name clash:



          export function* loadPostComments() {
          yield takeLatest("REQUEST_POST_COMMENTS", loadPostComments)
          }





          share|improve this answer


























          • Thank you - this seems to do the trick. The endless recursion is indeed due to an unintentional name clash while writing the question :)

            – cipacda
            Nov 21 '18 at 8:48
















          0














          My solution



          You have to save encountered postIds and to them after:



          function* watchRequestPostComments() {
          const encounteredPostIds = new Set()

          while(true) {
          const { postId } = yield take('REQUEST_POST_COMMENTS')
          if (!encounteredPostIds.has(postId) {
          yield fork(loadPostComments, postId)
          }
          encounteredPostIds.add(postId)
          }
          }


          Explanation



          There doesn't seem to exist a particular redux-saga-esque way of doing this. In any case, you have to track these IDs.



          I have answered a similar question about throttling requests.



          P.S.: This looks like an endless recursion. Must be an unintentional name clash:



          export function* loadPostComments() {
          yield takeLatest("REQUEST_POST_COMMENTS", loadPostComments)
          }





          share|improve this answer


























          • Thank you - this seems to do the trick. The endless recursion is indeed due to an unintentional name clash while writing the question :)

            – cipacda
            Nov 21 '18 at 8:48














          0












          0








          0







          My solution



          You have to save encountered postIds and to them after:



          function* watchRequestPostComments() {
          const encounteredPostIds = new Set()

          while(true) {
          const { postId } = yield take('REQUEST_POST_COMMENTS')
          if (!encounteredPostIds.has(postId) {
          yield fork(loadPostComments, postId)
          }
          encounteredPostIds.add(postId)
          }
          }


          Explanation



          There doesn't seem to exist a particular redux-saga-esque way of doing this. In any case, you have to track these IDs.



          I have answered a similar question about throttling requests.



          P.S.: This looks like an endless recursion. Must be an unintentional name clash:



          export function* loadPostComments() {
          yield takeLatest("REQUEST_POST_COMMENTS", loadPostComments)
          }





          share|improve this answer















          My solution



          You have to save encountered postIds and to them after:



          function* watchRequestPostComments() {
          const encounteredPostIds = new Set()

          while(true) {
          const { postId } = yield take('REQUEST_POST_COMMENTS')
          if (!encounteredPostIds.has(postId) {
          yield fork(loadPostComments, postId)
          }
          encounteredPostIds.add(postId)
          }
          }


          Explanation



          There doesn't seem to exist a particular redux-saga-esque way of doing this. In any case, you have to track these IDs.



          I have answered a similar question about throttling requests.



          P.S.: This looks like an endless recursion. Must be an unintentional name clash:



          export function* loadPostComments() {
          yield takeLatest("REQUEST_POST_COMMENTS", loadPostComments)
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 22:09

























          answered Nov 20 '18 at 21:49









          Andrey MoiseevAndrey Moiseev

          1,55052945




          1,55052945













          • Thank you - this seems to do the trick. The endless recursion is indeed due to an unintentional name clash while writing the question :)

            – cipacda
            Nov 21 '18 at 8:48



















          • Thank you - this seems to do the trick. The endless recursion is indeed due to an unintentional name clash while writing the question :)

            – cipacda
            Nov 21 '18 at 8:48

















          Thank you - this seems to do the trick. The endless recursion is indeed due to an unintentional name clash while writing the question :)

          – cipacda
          Nov 21 '18 at 8:48





          Thank you - this seems to do the trick. The endless recursion is indeed due to an unintentional name clash while writing the question :)

          – cipacda
          Nov 21 '18 at 8:48


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393024%2fredux-saga-group-similar-but-not-all-actions-into-takeleading-takelatest%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules