How to add one item from observable to an array of items in a different observable?












0














I am struggling with using observables and pipes where I want to add one item from one observable to another observable containing a list of items of the same type.



I have type X. And of the type X I have an observable array:



readonly arrayOfx$: Observable<X>;


I also have an observable of only the the type X:



private readonly _x$: Observable<UpdateOfX>;

interface UpdateOfX {
x: X,
updateState: "Add" | "Modified" | "Removed"
}


All this code is in a Service Class where the service should only expose the array of X. The data in the array I want to show in my html with async piping and this part of the functionality works. The host and the client are connected with the signalR technique and onConnected, an array of items of type X is retrieved. But as the application runs, in the backend new items of type X can be created, existing items can be changed or can be removed and when this occures, only this item will be send via the signalR connection and the modification state.



In the front end, this item must be added to the already retrieved array of items of type X. In the service, the pipe technique is used and my question is, how do I add the single item I get in a later moment to the list of items I retrieved earlier?



constructor() {
this.arrayOfx$ = this._someSignalRHelperService.retrieveMultipleItems$.pipe(
tap((xArray: X) => console.log(xArray)),
//can I somehow get the a later created x from the server here...
);

this._x$ = this._someSignalRHelperService.retrieveOneItem$.pipe(
tap((updateOfX: UpdateOfX) => console.log(updateOfX)),
map((updateOfX: UpdateOfX) => {
//process the updateState
//... or must I do something here to get x into x?
})
);
}


Since SignalR is used, the backend is in control when the client receives a new item of type X when there is one created.










share|improve this question





























    0














    I am struggling with using observables and pipes where I want to add one item from one observable to another observable containing a list of items of the same type.



    I have type X. And of the type X I have an observable array:



    readonly arrayOfx$: Observable<X>;


    I also have an observable of only the the type X:



    private readonly _x$: Observable<UpdateOfX>;

    interface UpdateOfX {
    x: X,
    updateState: "Add" | "Modified" | "Removed"
    }


    All this code is in a Service Class where the service should only expose the array of X. The data in the array I want to show in my html with async piping and this part of the functionality works. The host and the client are connected with the signalR technique and onConnected, an array of items of type X is retrieved. But as the application runs, in the backend new items of type X can be created, existing items can be changed or can be removed and when this occures, only this item will be send via the signalR connection and the modification state.



    In the front end, this item must be added to the already retrieved array of items of type X. In the service, the pipe technique is used and my question is, how do I add the single item I get in a later moment to the list of items I retrieved earlier?



    constructor() {
    this.arrayOfx$ = this._someSignalRHelperService.retrieveMultipleItems$.pipe(
    tap((xArray: X) => console.log(xArray)),
    //can I somehow get the a later created x from the server here...
    );

    this._x$ = this._someSignalRHelperService.retrieveOneItem$.pipe(
    tap((updateOfX: UpdateOfX) => console.log(updateOfX)),
    map((updateOfX: UpdateOfX) => {
    //process the updateState
    //... or must I do something here to get x into x?
    })
    );
    }


    Since SignalR is used, the backend is in control when the client receives a new item of type X when there is one created.










    share|improve this question



























      0












      0








      0







      I am struggling with using observables and pipes where I want to add one item from one observable to another observable containing a list of items of the same type.



      I have type X. And of the type X I have an observable array:



      readonly arrayOfx$: Observable<X>;


      I also have an observable of only the the type X:



      private readonly _x$: Observable<UpdateOfX>;

      interface UpdateOfX {
      x: X,
      updateState: "Add" | "Modified" | "Removed"
      }


      All this code is in a Service Class where the service should only expose the array of X. The data in the array I want to show in my html with async piping and this part of the functionality works. The host and the client are connected with the signalR technique and onConnected, an array of items of type X is retrieved. But as the application runs, in the backend new items of type X can be created, existing items can be changed or can be removed and when this occures, only this item will be send via the signalR connection and the modification state.



      In the front end, this item must be added to the already retrieved array of items of type X. In the service, the pipe technique is used and my question is, how do I add the single item I get in a later moment to the list of items I retrieved earlier?



      constructor() {
      this.arrayOfx$ = this._someSignalRHelperService.retrieveMultipleItems$.pipe(
      tap((xArray: X) => console.log(xArray)),
      //can I somehow get the a later created x from the server here...
      );

      this._x$ = this._someSignalRHelperService.retrieveOneItem$.pipe(
      tap((updateOfX: UpdateOfX) => console.log(updateOfX)),
      map((updateOfX: UpdateOfX) => {
      //process the updateState
      //... or must I do something here to get x into x?
      })
      );
      }


      Since SignalR is used, the backend is in control when the client receives a new item of type X when there is one created.










      share|improve this question















      I am struggling with using observables and pipes where I want to add one item from one observable to another observable containing a list of items of the same type.



      I have type X. And of the type X I have an observable array:



      readonly arrayOfx$: Observable<X>;


      I also have an observable of only the the type X:



      private readonly _x$: Observable<UpdateOfX>;

      interface UpdateOfX {
      x: X,
      updateState: "Add" | "Modified" | "Removed"
      }


      All this code is in a Service Class where the service should only expose the array of X. The data in the array I want to show in my html with async piping and this part of the functionality works. The host and the client are connected with the signalR technique and onConnected, an array of items of type X is retrieved. But as the application runs, in the backend new items of type X can be created, existing items can be changed or can be removed and when this occures, only this item will be send via the signalR connection and the modification state.



      In the front end, this item must be added to the already retrieved array of items of type X. In the service, the pipe technique is used and my question is, how do I add the single item I get in a later moment to the list of items I retrieved earlier?



      constructor() {
      this.arrayOfx$ = this._someSignalRHelperService.retrieveMultipleItems$.pipe(
      tap((xArray: X) => console.log(xArray)),
      //can I somehow get the a later created x from the server here...
      );

      this._x$ = this._someSignalRHelperService.retrieveOneItem$.pipe(
      tap((updateOfX: UpdateOfX) => console.log(updateOfX)),
      map((updateOfX: UpdateOfX) => {
      //process the updateState
      //... or must I do something here to get x into x?
      })
      );
      }


      Since SignalR is used, the backend is in control when the client receives a new item of type X when there is one created.







      typescript2.0 rxjs6






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 11:28

























      asked Nov 12 at 10:59









      Cornelis

      4852929




      4852929
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can use combineLatest(), and do whatever manipulation you want as soon as your receive the two emissions:



          constructor() {
          this.combinedOfX$ = combineLatest(
          this._someSignalRHelperService.retrieveMultipleItems$,
          this._someSignalRHelperService.retrieveOneItem$
          ).pipe(
          map(([singleOfX, multipleOfX]) => {
          // do your adding or mapping and whatever here.
          })
          )
          }


          Not sure if that is by design but the problem with having the frontend (client) to deal with the data is not so ideal. Your single source of truth is now based on your client, which different machines have different processing speed and may cause nuances and inconsistent displays. Also, the code will be messy in a sense that you will now need to check through the entire arrayOfX every time a singleOfX gets updated - you need to check if it exists in the current list, if yes, edit/delete; else, append to the list. What if the user refreshes his browser accidentally? You lost all of your processing.



          Since you are already using SignalR, it would be more advisable that you let the server handle all the data, and let the server be single source of truth. Then you will just need to subscribe to one hub and listen to the changes of the arrayOfX; and pretty much don't care of the single updates.






          share|improve this answer





















          • Thank you for your reply, I will try it and come back with feedback. On your comments, below the code: I couldn't agree more. Unfortunately, I am not in control in what happens on the server side I also would expect a fully updated list of items every time from the backend instead of all the updates. Too bad this is not the case and that's why I have to do messy code instead.
            – Cornelis
            Nov 13 at 12:15











          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%2f53260723%2fhow-to-add-one-item-from-observable-to-an-array-of-items-in-a-different-observab%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














          You can use combineLatest(), and do whatever manipulation you want as soon as your receive the two emissions:



          constructor() {
          this.combinedOfX$ = combineLatest(
          this._someSignalRHelperService.retrieveMultipleItems$,
          this._someSignalRHelperService.retrieveOneItem$
          ).pipe(
          map(([singleOfX, multipleOfX]) => {
          // do your adding or mapping and whatever here.
          })
          )
          }


          Not sure if that is by design but the problem with having the frontend (client) to deal with the data is not so ideal. Your single source of truth is now based on your client, which different machines have different processing speed and may cause nuances and inconsistent displays. Also, the code will be messy in a sense that you will now need to check through the entire arrayOfX every time a singleOfX gets updated - you need to check if it exists in the current list, if yes, edit/delete; else, append to the list. What if the user refreshes his browser accidentally? You lost all of your processing.



          Since you are already using SignalR, it would be more advisable that you let the server handle all the data, and let the server be single source of truth. Then you will just need to subscribe to one hub and listen to the changes of the arrayOfX; and pretty much don't care of the single updates.






          share|improve this answer





















          • Thank you for your reply, I will try it and come back with feedback. On your comments, below the code: I couldn't agree more. Unfortunately, I am not in control in what happens on the server side I also would expect a fully updated list of items every time from the backend instead of all the updates. Too bad this is not the case and that's why I have to do messy code instead.
            – Cornelis
            Nov 13 at 12:15
















          0














          You can use combineLatest(), and do whatever manipulation you want as soon as your receive the two emissions:



          constructor() {
          this.combinedOfX$ = combineLatest(
          this._someSignalRHelperService.retrieveMultipleItems$,
          this._someSignalRHelperService.retrieveOneItem$
          ).pipe(
          map(([singleOfX, multipleOfX]) => {
          // do your adding or mapping and whatever here.
          })
          )
          }


          Not sure if that is by design but the problem with having the frontend (client) to deal with the data is not so ideal. Your single source of truth is now based on your client, which different machines have different processing speed and may cause nuances and inconsistent displays. Also, the code will be messy in a sense that you will now need to check through the entire arrayOfX every time a singleOfX gets updated - you need to check if it exists in the current list, if yes, edit/delete; else, append to the list. What if the user refreshes his browser accidentally? You lost all of your processing.



          Since you are already using SignalR, it would be more advisable that you let the server handle all the data, and let the server be single source of truth. Then you will just need to subscribe to one hub and listen to the changes of the arrayOfX; and pretty much don't care of the single updates.






          share|improve this answer





















          • Thank you for your reply, I will try it and come back with feedback. On your comments, below the code: I couldn't agree more. Unfortunately, I am not in control in what happens on the server side I also would expect a fully updated list of items every time from the backend instead of all the updates. Too bad this is not the case and that's why I have to do messy code instead.
            – Cornelis
            Nov 13 at 12:15














          0












          0








          0






          You can use combineLatest(), and do whatever manipulation you want as soon as your receive the two emissions:



          constructor() {
          this.combinedOfX$ = combineLatest(
          this._someSignalRHelperService.retrieveMultipleItems$,
          this._someSignalRHelperService.retrieveOneItem$
          ).pipe(
          map(([singleOfX, multipleOfX]) => {
          // do your adding or mapping and whatever here.
          })
          )
          }


          Not sure if that is by design but the problem with having the frontend (client) to deal with the data is not so ideal. Your single source of truth is now based on your client, which different machines have different processing speed and may cause nuances and inconsistent displays. Also, the code will be messy in a sense that you will now need to check through the entire arrayOfX every time a singleOfX gets updated - you need to check if it exists in the current list, if yes, edit/delete; else, append to the list. What if the user refreshes his browser accidentally? You lost all of your processing.



          Since you are already using SignalR, it would be more advisable that you let the server handle all the data, and let the server be single source of truth. Then you will just need to subscribe to one hub and listen to the changes of the arrayOfX; and pretty much don't care of the single updates.






          share|improve this answer












          You can use combineLatest(), and do whatever manipulation you want as soon as your receive the two emissions:



          constructor() {
          this.combinedOfX$ = combineLatest(
          this._someSignalRHelperService.retrieveMultipleItems$,
          this._someSignalRHelperService.retrieveOneItem$
          ).pipe(
          map(([singleOfX, multipleOfX]) => {
          // do your adding or mapping and whatever here.
          })
          )
          }


          Not sure if that is by design but the problem with having the frontend (client) to deal with the data is not so ideal. Your single source of truth is now based on your client, which different machines have different processing speed and may cause nuances and inconsistent displays. Also, the code will be messy in a sense that you will now need to check through the entire arrayOfX every time a singleOfX gets updated - you need to check if it exists in the current list, if yes, edit/delete; else, append to the list. What if the user refreshes his browser accidentally? You lost all of your processing.



          Since you are already using SignalR, it would be more advisable that you let the server handle all the data, and let the server be single source of truth. Then you will just need to subscribe to one hub and listen to the changes of the arrayOfX; and pretty much don't care of the single updates.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 at 9:10









          CozyAzure

          5,33041836




          5,33041836












          • Thank you for your reply, I will try it and come back with feedback. On your comments, below the code: I couldn't agree more. Unfortunately, I am not in control in what happens on the server side I also would expect a fully updated list of items every time from the backend instead of all the updates. Too bad this is not the case and that's why I have to do messy code instead.
            – Cornelis
            Nov 13 at 12:15


















          • Thank you for your reply, I will try it and come back with feedback. On your comments, below the code: I couldn't agree more. Unfortunately, I am not in control in what happens on the server side I also would expect a fully updated list of items every time from the backend instead of all the updates. Too bad this is not the case and that's why I have to do messy code instead.
            – Cornelis
            Nov 13 at 12:15
















          Thank you for your reply, I will try it and come back with feedback. On your comments, below the code: I couldn't agree more. Unfortunately, I am not in control in what happens on the server side I also would expect a fully updated list of items every time from the backend instead of all the updates. Too bad this is not the case and that's why I have to do messy code instead.
          – Cornelis
          Nov 13 at 12:15




          Thank you for your reply, I will try it and come back with feedback. On your comments, below the code: I couldn't agree more. Unfortunately, I am not in control in what happens on the server side I also would expect a fully updated list of items every time from the backend instead of all the updates. Too bad this is not the case and that's why I have to do messy code instead.
          – Cornelis
          Nov 13 at 12:15


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53260723%2fhow-to-add-one-item-from-observable-to-an-array-of-items-in-a-different-observab%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

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues