Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel background process when leaving a component to another?

I am using this libray:

https://github.com/VadimDez/ng2-pdf-viewer

I have created a repository with an example of my problem.

https://github.com/YeisonVelez11/pdf

I am generating a pdf, and this works well. but if the pdf has not finished loading and I navigate to another component, I get an error.

Basically when I go back to a preview, I get a problem because the pdf that is being loaded can not be canceled. I do not know what trick I can do to get out of the document view at any time.

enter image description here

  <pdf-viewer [src]="archivo_adjunto"
              *ngIf="archivo_adjunto"
              [render-text]="false"
              [original-size]="true"
              [page]="1"
              [autoresize]="true"
              [show-all]="true"
              style="display: block;"
              (page-rendered)="pageRendered($event)"
  ></pdf-viewer>

ionViewDidLoad() {
this.archivo_adjunto="./assets/documents/Resumen Ejecutivo Autoevaluacion.pdf"

}

This is done in Ionic, but the functionality is in Angular.

like image 504
yavg Avatar asked Nov 01 '18 05:11

yavg


People also ask

Which command will stop a background process?

pkill. pkill is one of the commands that can directly kill a background process, using the process name.

How do I stop all background processes in Ubuntu?

If a program has multiple processes, you can use the killall command to terminate them all at once. Like pkill, this uses the package name—use top to find this under the Command column. To use killall, type killall process or sudo killall process, replacing process with the package name.


1 Answers

In large-scale apps or in components that deal with bunch of observables or subjects or BehaviorSubjects, it is good practice to unsubscribe the observables when you are leaving the component in order to prevent problems like memory leaks or slowing down system.


 ngOnDestroy(){
   this.ObservableOrSubsject1$.unsubscribe();
   this.ObservableOrSubsject2$.unsubscribe();
   ....
 }

Also do not forget to implement OnDestroy to the class.

export MyComponent implements OnInit, OnDestroy

There is another good way that I actually like and you can use while dealing with subscriptions; in which you can provide a subject Like:

private onDestroy$: Subject = new Subject();

and when using any observable (e.g: mapping or subscribing) pipe with takeUntil:

ngOnInit(){
   this.ObservableOrSubsject1
     .pipe(takeUntil(this.onDestroy$))
     .subscribe((result: IResult) => {
       this.result= Object.assign({}, result);
   });

and in ngOnDestroy just pass "this.onDestroy$.next()":

ngOnDestroy(){
   this.onDestroy$.next();
 }

Again, Do not forget to implement OnDestroy to the class.

At the end, I recommend using Ngrx/Store and Ngrx/Effects to manage your states and unify the events in your application; which will save you from lots of headaches and keeps your application in an standard architecture.

like image 89
Hossein Hassanzadeh Avatar answered Dec 04 '22 22:12

Hossein Hassanzadeh