angular assign different observable to template with async based on condition (any memory leak ?)









up vote
3
down vote

favorite












I need to render data from different ngrx stores based on some flag. Both stores gives same type of data.



Approach 1



<ng-contaier *ngIf="flag$ | async; else anotherStoreData">
<ng-container *ngIf="store1$ | async as store1">
<div> store1?.prop1 </div>
<div> store1?.prop2 </div>
</ng-container>
</ng-contaier>
<ng-template #anotherStoreData>
<ng-container *ngIf="store2$ | async as store2">
<div> store2?.prop1 </div>
<div> store2?.prop2 </div>
</ng-container>
</ng-template>

flag$: Observable<boolean>
store1$: Observable<Store>
store2$: Observable<Store>
ngInit()
flag$ = streamService.userNewStore();
store1$ = this.store.select(<someselector1>);
store2$ = this.store.select(<someselector2>);



Approach 2



<ng-container *ngIf="store$ | async as store">
<div> store?.prop1 </div>
<div> store?.prop2 </div>
</ng-container>


store$: Observable<Store>
ngInit()
streamService.userNewStore()
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe((flag) =>
store$ = flag ? this.store.select(<someselector1>) : this.store.select(<someselector2>);
);



In Approach1 I am duplicating template, which is fine for small template - but if it is big then i am thinking about Approach 2.



In Approach2 streamService can change the flag at any time, in that case what will happen to the previous subscription in the template with async pipe. Will it lead any memory leak?



Is there any other alternatives that I can use, please suggest.










share|improve this question



























    up vote
    3
    down vote

    favorite












    I need to render data from different ngrx stores based on some flag. Both stores gives same type of data.



    Approach 1



    <ng-contaier *ngIf="flag$ | async; else anotherStoreData">
    <ng-container *ngIf="store1$ | async as store1">
    <div> store1?.prop1 </div>
    <div> store1?.prop2 </div>
    </ng-container>
    </ng-contaier>
    <ng-template #anotherStoreData>
    <ng-container *ngIf="store2$ | async as store2">
    <div> store2?.prop1 </div>
    <div> store2?.prop2 </div>
    </ng-container>
    </ng-template>

    flag$: Observable<boolean>
    store1$: Observable<Store>
    store2$: Observable<Store>
    ngInit()
    flag$ = streamService.userNewStore();
    store1$ = this.store.select(<someselector1>);
    store2$ = this.store.select(<someselector2>);



    Approach 2



    <ng-container *ngIf="store$ | async as store">
    <div> store?.prop1 </div>
    <div> store?.prop2 </div>
    </ng-container>


    store$: Observable<Store>
    ngInit()
    streamService.userNewStore()
    .pipe(takeUntil(this.ngUnsubscribe))
    .subscribe((flag) =>
    store$ = flag ? this.store.select(<someselector1>) : this.store.select(<someselector2>);
    );



    In Approach1 I am duplicating template, which is fine for small template - but if it is big then i am thinking about Approach 2.



    In Approach2 streamService can change the flag at any time, in that case what will happen to the previous subscription in the template with async pipe. Will it lead any memory leak?



    Is there any other alternatives that I can use, please suggest.










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I need to render data from different ngrx stores based on some flag. Both stores gives same type of data.



      Approach 1



      <ng-contaier *ngIf="flag$ | async; else anotherStoreData">
      <ng-container *ngIf="store1$ | async as store1">
      <div> store1?.prop1 </div>
      <div> store1?.prop2 </div>
      </ng-container>
      </ng-contaier>
      <ng-template #anotherStoreData>
      <ng-container *ngIf="store2$ | async as store2">
      <div> store2?.prop1 </div>
      <div> store2?.prop2 </div>
      </ng-container>
      </ng-template>

      flag$: Observable<boolean>
      store1$: Observable<Store>
      store2$: Observable<Store>
      ngInit()
      flag$ = streamService.userNewStore();
      store1$ = this.store.select(<someselector1>);
      store2$ = this.store.select(<someselector2>);



      Approach 2



      <ng-container *ngIf="store$ | async as store">
      <div> store?.prop1 </div>
      <div> store?.prop2 </div>
      </ng-container>


      store$: Observable<Store>
      ngInit()
      streamService.userNewStore()
      .pipe(takeUntil(this.ngUnsubscribe))
      .subscribe((flag) =>
      store$ = flag ? this.store.select(<someselector1>) : this.store.select(<someselector2>);
      );



      In Approach1 I am duplicating template, which is fine for small template - but if it is big then i am thinking about Approach 2.



      In Approach2 streamService can change the flag at any time, in that case what will happen to the previous subscription in the template with async pipe. Will it lead any memory leak?



      Is there any other alternatives that I can use, please suggest.










      share|improve this question















      I need to render data from different ngrx stores based on some flag. Both stores gives same type of data.



      Approach 1



      <ng-contaier *ngIf="flag$ | async; else anotherStoreData">
      <ng-container *ngIf="store1$ | async as store1">
      <div> store1?.prop1 </div>
      <div> store1?.prop2 </div>
      </ng-container>
      </ng-contaier>
      <ng-template #anotherStoreData>
      <ng-container *ngIf="store2$ | async as store2">
      <div> store2?.prop1 </div>
      <div> store2?.prop2 </div>
      </ng-container>
      </ng-template>

      flag$: Observable<boolean>
      store1$: Observable<Store>
      store2$: Observable<Store>
      ngInit()
      flag$ = streamService.userNewStore();
      store1$ = this.store.select(<someselector1>);
      store2$ = this.store.select(<someselector2>);



      Approach 2



      <ng-container *ngIf="store$ | async as store">
      <div> store?.prop1 </div>
      <div> store?.prop2 </div>
      </ng-container>


      store$: Observable<Store>
      ngInit()
      streamService.userNewStore()
      .pipe(takeUntil(this.ngUnsubscribe))
      .subscribe((flag) =>
      store$ = flag ? this.store.select(<someselector1>) : this.store.select(<someselector2>);
      );



      In Approach1 I am duplicating template, which is fine for small template - but if it is big then i am thinking about Approach 2.



      In Approach2 streamService can change the flag at any time, in that case what will happen to the previous subscription in the template with async pipe. Will it lead any memory leak?



      Is there any other alternatives that I can use, please suggest.







      javascript angular ngrx angular-observable ngrx-store-4.0






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 0:42

























      asked Nov 10 at 22:35









      Dyapa Srikanth

      69111350




      69111350






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Having just checked the source code for the Async pipe, it appears that it will unsubscribe if the Observable changes.



          You can see this in line 100 of the file:



          if (obj !== this._obj) 
          this._dispose();
          return this.transform(obj as any);



          If the value being passed in is not the one currently held in memory, it calls this.dispose, which in turn unsubscribes.



          With that in mind, I would definitely prefer the 2nd approach






          share|improve this answer
















          • 1




            Is this why I like stackoverflow ?
            – Dyapa Srikanth
            Nov 10 at 23:38

















          up vote
          0
          down vote













          You can use the flag$ observable in a conditional operator to determine the data source:



          <ng-container *ngIf="((flag$ | async) ? store1$ : store2$) | async as store">
          <div> store?.prop1 </div>
          <div> store?.prop2 </div>
          </ng-container>


          See this stackblitz for a demo.






          share|improve this answer




















          • @ConnersFan when it is switching from store1$ to store2$ will it unsubscribe store1$ - If yes, is there any difference in going with ngIf and ngOnInit on selecting store based on flag?
            – Dyapa Srikanth
            Nov 10 at 23:31










          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',
          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%2f53244100%2fangular-assign-different-observable-to-template-with-async-based-on-condition-a%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          Having just checked the source code for the Async pipe, it appears that it will unsubscribe if the Observable changes.



          You can see this in line 100 of the file:



          if (obj !== this._obj) 
          this._dispose();
          return this.transform(obj as any);



          If the value being passed in is not the one currently held in memory, it calls this.dispose, which in turn unsubscribes.



          With that in mind, I would definitely prefer the 2nd approach






          share|improve this answer
















          • 1




            Is this why I like stackoverflow ?
            – Dyapa Srikanth
            Nov 10 at 23:38














          up vote
          4
          down vote



          accepted










          Having just checked the source code for the Async pipe, it appears that it will unsubscribe if the Observable changes.



          You can see this in line 100 of the file:



          if (obj !== this._obj) 
          this._dispose();
          return this.transform(obj as any);



          If the value being passed in is not the one currently held in memory, it calls this.dispose, which in turn unsubscribes.



          With that in mind, I would definitely prefer the 2nd approach






          share|improve this answer
















          • 1




            Is this why I like stackoverflow ?
            – Dyapa Srikanth
            Nov 10 at 23:38












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          Having just checked the source code for the Async pipe, it appears that it will unsubscribe if the Observable changes.



          You can see this in line 100 of the file:



          if (obj !== this._obj) 
          this._dispose();
          return this.transform(obj as any);



          If the value being passed in is not the one currently held in memory, it calls this.dispose, which in turn unsubscribes.



          With that in mind, I would definitely prefer the 2nd approach






          share|improve this answer












          Having just checked the source code for the Async pipe, it appears that it will unsubscribe if the Observable changes.



          You can see this in line 100 of the file:



          if (obj !== this._obj) 
          this._dispose();
          return this.transform(obj as any);



          If the value being passed in is not the one currently held in memory, it calls this.dispose, which in turn unsubscribes.



          With that in mind, I would definitely prefer the 2nd approach







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 23:03









          user184994

          10.7k11526




          10.7k11526







          • 1




            Is this why I like stackoverflow ?
            – Dyapa Srikanth
            Nov 10 at 23:38












          • 1




            Is this why I like stackoverflow ?
            – Dyapa Srikanth
            Nov 10 at 23:38







          1




          1




          Is this why I like stackoverflow ?
          – Dyapa Srikanth
          Nov 10 at 23:38




          Is this why I like stackoverflow ?
          – Dyapa Srikanth
          Nov 10 at 23:38












          up vote
          0
          down vote













          You can use the flag$ observable in a conditional operator to determine the data source:



          <ng-container *ngIf="((flag$ | async) ? store1$ : store2$) | async as store">
          <div> store?.prop1 </div>
          <div> store?.prop2 </div>
          </ng-container>


          See this stackblitz for a demo.






          share|improve this answer




















          • @ConnersFan when it is switching from store1$ to store2$ will it unsubscribe store1$ - If yes, is there any difference in going with ngIf and ngOnInit on selecting store based on flag?
            – Dyapa Srikanth
            Nov 10 at 23:31














          up vote
          0
          down vote













          You can use the flag$ observable in a conditional operator to determine the data source:



          <ng-container *ngIf="((flag$ | async) ? store1$ : store2$) | async as store">
          <div> store?.prop1 </div>
          <div> store?.prop2 </div>
          </ng-container>


          See this stackblitz for a demo.






          share|improve this answer




















          • @ConnersFan when it is switching from store1$ to store2$ will it unsubscribe store1$ - If yes, is there any difference in going with ngIf and ngOnInit on selecting store based on flag?
            – Dyapa Srikanth
            Nov 10 at 23:31












          up vote
          0
          down vote










          up vote
          0
          down vote









          You can use the flag$ observable in a conditional operator to determine the data source:



          <ng-container *ngIf="((flag$ | async) ? store1$ : store2$) | async as store">
          <div> store?.prop1 </div>
          <div> store?.prop2 </div>
          </ng-container>


          See this stackblitz for a demo.






          share|improve this answer












          You can use the flag$ observable in a conditional operator to determine the data source:



          <ng-container *ngIf="((flag$ | async) ? store1$ : store2$) | async as store">
          <div> store?.prop1 </div>
          <div> store?.prop2 </div>
          </ng-container>


          See this stackblitz for a demo.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 23:15









          ConnorsFan

          28.1k42457




          28.1k42457











          • @ConnersFan when it is switching from store1$ to store2$ will it unsubscribe store1$ - If yes, is there any difference in going with ngIf and ngOnInit on selecting store based on flag?
            – Dyapa Srikanth
            Nov 10 at 23:31
















          • @ConnersFan when it is switching from store1$ to store2$ will it unsubscribe store1$ - If yes, is there any difference in going with ngIf and ngOnInit on selecting store based on flag?
            – Dyapa Srikanth
            Nov 10 at 23:31















          @ConnersFan when it is switching from store1$ to store2$ will it unsubscribe store1$ - If yes, is there any difference in going with ngIf and ngOnInit on selecting store based on flag?
          – Dyapa Srikanth
          Nov 10 at 23:31




          @ConnersFan when it is switching from store1$ to store2$ will it unsubscribe store1$ - If yes, is there any difference in going with ngIf and ngOnInit on selecting store based on flag?
          – Dyapa Srikanth
          Nov 10 at 23:31

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244100%2fangular-assign-different-observable-to-template-with-async-based-on-condition-a%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線