73 lines
No EOL
9.7 KiB
JavaScript
Executable file
73 lines
No EOL
9.7 KiB
JavaScript
Executable file
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
import { switchMap } from 'rxjs/operators';
|
|
/** Manages event on a Google Maps object, ensuring that events are added only when necessary. */
|
|
export class MapEventManager {
|
|
/** Clears all currently-registered event listeners. */
|
|
_clearListeners() {
|
|
for (const listener of this._listeners) {
|
|
listener.remove();
|
|
}
|
|
this._listeners = [];
|
|
}
|
|
constructor(_ngZone) {
|
|
this._ngZone = _ngZone;
|
|
/** Pending listeners that were added before the target was set. */
|
|
this._pending = [];
|
|
this._listeners = [];
|
|
this._targetStream = new BehaviorSubject(undefined);
|
|
}
|
|
/** Gets an observable that adds an event listener to the map when a consumer subscribes to it. */
|
|
getLazyEmitter(name) {
|
|
return this._targetStream.pipe(switchMap(target => {
|
|
const observable = new Observable(observer => {
|
|
// If the target hasn't been initialized yet, cache the observer so it can be added later.
|
|
if (!target) {
|
|
this._pending.push({ observable, observer });
|
|
return undefined;
|
|
}
|
|
const listener = target.addListener(name, (event) => {
|
|
this._ngZone.run(() => observer.next(event));
|
|
});
|
|
// If there's an error when initializing the Maps API (e.g. a wrong API key), it will
|
|
// return a dummy object that returns `undefined` from `addListener` (see #26514).
|
|
if (!listener) {
|
|
observer.complete();
|
|
return undefined;
|
|
}
|
|
this._listeners.push(listener);
|
|
return () => listener.remove();
|
|
});
|
|
return observable;
|
|
}));
|
|
}
|
|
/** Sets the current target that the manager should bind events to. */
|
|
setTarget(target) {
|
|
const currentTarget = this._targetStream.value;
|
|
if (target === currentTarget) {
|
|
return;
|
|
}
|
|
// Clear the listeners from the pre-existing target.
|
|
if (currentTarget) {
|
|
this._clearListeners();
|
|
this._pending = [];
|
|
}
|
|
this._targetStream.next(target);
|
|
// Add the listeners that were bound before the map was initialized.
|
|
this._pending.forEach(subscriber => subscriber.observable.subscribe(subscriber.observer));
|
|
this._pending = [];
|
|
}
|
|
/** Destroys the manager and clears the event listeners. */
|
|
destroy() {
|
|
this._clearListeners();
|
|
this._pending = [];
|
|
this._targetStream.complete();
|
|
}
|
|
}
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFwLWV2ZW50LW1hbmFnZXIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi9zcmMvZ29vZ2xlLW1hcHMvbWFwLWV2ZW50LW1hbmFnZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7Ozs7OztHQU1HO0FBR0gsT0FBTyxFQUFDLGVBQWUsRUFBRSxVQUFVLEVBQWEsTUFBTSxNQUFNLENBQUM7QUFDN0QsT0FBTyxFQUFDLFNBQVMsRUFBQyxNQUFNLGdCQUFnQixDQUFDO0FBV3pDLGlHQUFpRztBQUNqRyxNQUFNLE9BQU8sZUFBZTtJQU0xQix1REFBdUQ7SUFDL0MsZUFBZTtRQUNyQixLQUFLLE1BQU0sUUFBUSxJQUFJLElBQUksQ0FBQyxVQUFVLEVBQUUsQ0FBQztZQUN2QyxRQUFRLENBQUMsTUFBTSxFQUFFLENBQUM7UUFDcEIsQ0FBQztRQUVELElBQUksQ0FBQyxVQUFVLEdBQUcsRUFBRSxDQUFDO0lBQ3ZCLENBQUM7SUFFRCxZQUFvQixPQUFlO1FBQWYsWUFBTyxHQUFQLE9BQU8sQ0FBUTtRQWRuQyxtRUFBbUU7UUFDM0QsYUFBUSxHQUErRCxFQUFFLENBQUM7UUFDMUUsZUFBVSxHQUFvQyxFQUFFLENBQUM7UUFDakQsa0JBQWEsR0FBRyxJQUFJLGVBQWUsQ0FBd0IsU0FBUyxDQUFDLENBQUM7SUFXeEMsQ0FBQztJQUV2QyxrR0FBa0c7SUFDbEcsY0FBYyxD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