0

UPDATE more details about observable usage

I've need service because checkboxcell component can't use @Output() property. This happened becaues of wrong usage with ag-grid api - renderer component was used as editor. It can't be corrected at this moment.

@Component({
    ...
    template: `<mat-checkbox (change)="onChanged($event)"></mat-checkbox>`
})
export class CheckboxCellComponent implements ICellRendererAngularComp {
    ...
    //each component that use checkboxcell provide own instance of PermissionChange
    constructor(private permissionChange: PermissionChange) {

    }
    public onChanged({ checked }): void {
         //use observable
         this.permissionChange.changeEvent.next();
    }
}

ORIGINAL question

I have a simple service

export class PermissionChange {
    public changeEvent: Subject<void>;
    constructor() {
        this.changeEvent = new Subject<void>();
    }
}

And component, that use the service:

@Component({
    ...
    providers: [PermissionChange]
})
export class ...Component implements OnInit {
    constructor(
        private permissionChange: PermissionChange,
    ) { }

    public ngOnInit(): void {
        //subscription
        this.permissionChange.changeEvent.subscribe(() => {...});
    }
}

PermisionChange is personal provider for this component => instance of PermisionChange will be destroyed in the moment when component will be destroyed => changeEvent observable will be destroyed too. So maybe there is no need to unsubscribe, maybe subscribtion will be destroyed too? But I don't know how to check it.

Nikita
  • 1,019
  • 2
  • 15
  • 39
  • Possible duplicate of [How to unsubscribe from EventEmitter in Angular 2?](https://stackoverflow.com/questions/36494509/how-to-unsubscribe-from-eventemitter-in-angular-2) – Pierre Mallet Jan 30 '18 at 11:42
  • 1
    possible duplicate of https://stackoverflow.com/questions/36494509/how-to-unsubscribe-from-eventemitter-in-angular-2 . Look espacially on the second answer. EventEmitter should be used in component only. In a Injectable you should use plain Observables – Pierre Mallet Jan 30 '18 at 11:44
  • @PierreMallet I've used the observable and edited the question. – Nikita Jan 30 '18 at 11:48
  • What is the actual code of the service? It matters. How is the observable really created, and when does it emit events? – JB Nizet Jan 30 '18 at 11:51
  • But you added an observable that never emit a data.. its hard to say of you need to unsubscribe. If your changeEvent in not a Subject ( if it complete after first emition you dont need to unsubscribe ). But as JB NIzet says, we cant help you without much details on implementation – Pierre Mallet Jan 30 '18 at 11:53
  • @PierreMallet this is the actual code of the service, also I've provided more details – Nikita Jan 30 '18 at 12:05
  • @JBNizet this is the actual code of the service, also I've provided more details – Nikita Jan 30 '18 at 12:06
  • Observable doesn't have a next() method. This code wouldn't compile. – JB Nizet Jan 30 '18 at 12:12
  • @JBNizet you are right, in real code it still EventEmitter :) So subject is what I need according to this https://stackoverflow.com/questions/36494509/how-to-unsubscribe-from-eventemitter-in-angular-2 , question is the same – Nikita Jan 30 '18 at 12:34

1 Answers1

1

You don't need to unsubscribe in this case: the parent component, the child components and the service instance will all be destroyed and be eligible to garbage collection when the parent component is destroyed.

Nothing live will keep a reference to the Subject in the service, and it will thus be eligible to GC, too.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I've just read more about subject, so subscription - is disposable resource. When I've written changeEvent.subscribe() - resources were alloced. I not sure that destroying the changeEvent object lead to subscription's dispose. Maybe you know how to check it? – Nikita Feb 01 '18 at 07:24
  • Yes, because resources were allocated not somewhere in space but in the subject instance, so when subject was destroyed all resources were disposed too. – Nikita Aug 07 '18 at 05:35