Hi I was reading trought code from angular material google map library. It is all quite clear, but one thing. The code below is what I haven't understood (from map-event-manager.ts).
/** Gets an observable that adds an event listener to the map when a consumer subscribes to it. */
getLazyEmitter<T>(name: string): Observable<T> {
const observable = new Observable<T>(observer => {
// If the target hasn't been initialized yet, cache the observer so it can be added later.
if (!this._target) {
this._pending.push({observable, observer});
return undefined;
}
const listener = this._target.addListener(name, (event: T) => {
this._ngZone.run(() => observer.next(event));
});
this._listeners.push(listener);
return () => listener.remove();
});
return observable;
}
/** Sets the current target that the manager should bind events to. */
setTarget(target: MapEventManagerTarget) {
if (target === this._target) {
return;
}
// Clear the listeners from the pre-existing target.
if (this._target) {
this._clearListeners();
this._pending = [];
}
this._target = target;
// Add the listeners that were bound before the map was initialized.
this._pending.forEach(subscriber => subscriber.observable.subscribe(subscriber.observer));
this._pending = [];
}
This line, in particular, is not clear to me. Why and how this wires up. And where is the part that let it run back inside of ngZone?
this._pending.forEach(subscriber => subscriber.observable.subscribe(subscriber.observer));
Zone comeback: this._ngZone.run(() => observer.next(event));