Angular-Async-Data icon indicating copy to clipboard operation
Angular-Async-Data copied to clipboard

Avoid subscribe() while interacting with outputs

Open alfredoperez opened this issue 5 years ago • 3 comments

Is it possible to avoid subscriptions while reacting to an event and emitting an event from it?

combineLatest([this.selectionChangedAction$,this.activeResults$])
	.pipe(
	    tap(([selectedRows,  activeResults]) => {

                const getSelected = this.getSelected(activeResults, selectedRows);
		
                ** NOTE: This is an @Output to parent component**
		this.onAccessInclusionChange.emit(accessInclusionList);

				})
	).subscribe(); --> Can this be avoided?

I have not seen this case in your talks and I am trying to avoid subscriptions

alfredoperez avatar Feb 12 '20 16:02 alfredoperez

If what is to the left of combineLatest() is bound with an async pipe, you can avoid the subscribe. If you can do a short stackblitz example of what you are trying to achieve, I can take a look.

DeborahK avatar Feb 12 '20 16:02 DeborahK

https://stackblitz.com/github/alfredoperez/angular-async-data?file=src%2Fapp%2Fproducts%2Fproduct-list%2Fproduct-list-asyncPipe.component.ts

Added two examples of things I don't know how to avoid the subscription. Try clicking the checkbox and resizing the windows

  • Emitting an event as a consequence of an action
  • Executing a method from an observable created by a HostListener.
 combineLatest([this.selectionChangedAction$, this.products$ ])
      .pipe(
        tap(([selected, allProducts]) => {
            if (selected !== null) {
              const target =  allProducts.find(item => selected.id === item.id);
              this.productsSelectedChange.emit(target);
            }
          }
        )
      )
      .subscribe(); 

    this.resize$.pipe(debounceTime(300))
      .subscribe(() => {
      this.resizingLabel = `resizing action... E.g. resize grid base on size. width: ${window.outerWidth}  height: ${window.outerHeight}`;
    });

alfredoperez avatar Feb 12 '20 18:02 alfredoperez

Thanks =)

Also, is there a way to avoid subscription on:

  • Events from HosListener, like subscribing to a window resize event
  • valueChanges from a form. E.g.
     this.customerForm.valueChanges.subscirbe( ()=>{
             this.store.dispatch(markSectionAsDirty)
      });
    

alfredoperez avatar Feb 14 '20 15:02 alfredoperez