Skip to content

Commit

Permalink
chore(SebmGoogleMapMarker): cleanup event subscriptions on marker des…
Browse files Browse the repository at this point in the history
…troy

refs #451
  • Loading branch information
Alex Weber committed Jul 6, 2016
1 parent 1e8145c commit 152ee2e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/core/directives/google-map-marker.ts
@@ -1,4 +1,5 @@
import {AfterContentInit, ContentChild, Directive, EventEmitter, OnChanges, OnDestroy, SimpleChange} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';

import {MouseEvent} from '../events';
import * as mapTypes from '../services/google-maps-types';
Expand Down Expand Up @@ -91,6 +92,7 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges, AfterContentIn

private _markerAddedToManger: boolean = false;
private _id: string;
private _observableSubscriptions: Subscription[] = [];

constructor(private _markerManager: MarkerManager) { this._id = (markerId++).toString(); }

Expand Down Expand Up @@ -130,16 +132,19 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges, AfterContentIn
}

private _addEventListeners() {
this._markerManager.createEventObservable('click', this).subscribe(() => {
const cs = this._markerManager.createEventObservable('click', this).subscribe(() => {
if (this.openInfoWindow && this._infoWindow != null) {
this._infoWindow.open();
}
this.markerClick.emit(null);
});
this._markerManager.createEventObservable<mapTypes.MouseEvent>('dragend', this)
this._observableSubscriptions.push(cs);

const ds = this._markerManager.createEventObservable<mapTypes.MouseEvent>('dragend', this)
.subscribe((e: mapTypes.MouseEvent) => {
this.dragEnd.emit({coords: {lat: e.latLng.lat(), lng: e.latLng.lng()}});
});
this._observableSubscriptions.push(ds);
}

/** @internal */
Expand All @@ -149,5 +154,9 @@ export class SebmGoogleMapMarker implements OnDestroy, OnChanges, AfterContentIn
toString(): string { return 'SebmGoogleMapMarker-' + this._id.toString(); }

/** @internal */
ngOnDestroy() { this._markerManager.deleteMarker(this); }
ngOnDestroy() {
this._markerManager.deleteMarker(this);
// unsubscribe all registered observable subscriptions
this._observableSubscriptions.forEach((s) => s.unsubscribe());
}
}

1 comment on commit 152ee2e

@brian-singer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

Please sign in to comment.