Basically I want to know is there a way to get last value from EventEmmiter right after I have subscribed to it?
Why I need this. Imagine a list component that uses two more components: filter and grid. Filter provides filter event and grid provides sorting event.
In my list component I want to write code like this:
Observable
.combineLatest(filtering$, sorting$)
.switchMap(([filter, sorting]) => {
return this.api.list(filter, sorting);
})
...
Very short elegant code. But there are two problems:
EventEmitter is not observable. It is not much of a problem because I can easily wrap it with observable.Currently I am solving it with BehaviorSubject from RxJs:
Represents a value that changes over time. Observers can subscribe to the subject to receive the last (or initial) value and all subsequent notifications.
In my filter component I have:
class UsersListFilter {
private filteringSource = new BehaviorSubject<UserFilter>(new UserFilter());
filtering$ = this.filteringSource.asObservable();
...
}
And in my list component:
class UsersList {
@ViewChild(UsersListFilter) private filter: UsersListFilter;
...
setupDataReloading() {
Observable
.combineLatest(this.filter.filtering$, this.grid.sorting$)
...
}
}
As you can see I do not use EventEmitter at all. But this solution feels kinda unnatural in Angular2 because I am ignoring standard way of interacting with child component (@Output).
Any thoughts?
Use this custom class instead
import { EventEmitter } from '@angular/core';
import { ObjectUnsubscribedError } from 'rxjs';
export class CustomEventEmitter<T> extends EventEmitter<T> {
private _value: T;
constructor() {
super();
}
get value(): T {
return this.getValue();
}
getValue(): T {
if (this.hasError) {
throw this.thrownError;
} else if (this.closed) {
throw new ObjectUnsubscribedError();
} else {
return this._value;
}
}
emit(value?: T) {
super.emit(this._value = value);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With