Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align destroy calls #211

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions projects/ngx-maplibre-gl/src/lib/control/control.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
ChangeDetectionStrategy,
Component,
DestroyRef,
ElementRef,
OnDestroy,
afterNextRender,
inject,
input,
Expand Down Expand Up @@ -61,10 +61,9 @@ export class CustomControl implements IControl {
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
})
export class ControlComponent<T extends IControl> {
export class ControlComponent<T extends IControl> implements OnDestroy {
/** Init injection */
private readonly mapService = inject(MapService);
private readonly destroyRef = inject(DestroyRef);

/** Init input */
readonly position = input<ControlPosition>();
Expand All @@ -83,11 +82,11 @@ export class ControlComponent<T extends IControl> {
});
}
});
}

this.destroyRef.onDestroy(() => {
if (this.mapService?.mapInstance?.hasControl(this.control)) {
this.mapService.removeControl(this.control);
}
});
ngOnDestroy(): void {
if (this.mapService?.mapInstance?.hasControl(this.control)) {
this.mapService.removeControl(this.control);
}
}
}
11 changes: 6 additions & 5 deletions projects/ngx-maplibre-gl/src/lib/image/image.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DestroyRef,
NgZone,
OnChanges,
OnDestroy,
OnInit,
SimpleChanges,
inject,
Expand Down Expand Up @@ -54,7 +55,7 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ImageComponent implements OnInit, OnChanges {
export class ImageComponent implements OnInit, OnChanges, OnDestroy {
/** Init injection */
private readonly mapService = inject(MapService);
private readonly destroyRef = inject(DestroyRef);
Expand All @@ -79,10 +80,6 @@ export class ImageComponent implements OnInit, OnChanges {
private isAdded = signal(false);
private isAdding = signal(false);

constructor() {
this.destroyRef.onDestroy(() => this.removeImage());
}

ngOnInit() {
this.mapService.mapLoaded$
.pipe(
Expand Down Expand Up @@ -112,6 +109,10 @@ export class ImageComponent implements OnInit, OnChanges {
}
}

ngOnDestroy(): void {
this.removeImage()
}

removeImage() {
if (this.isAdded()) {
this.mapService.removeImage(this.id());
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-maplibre-gl/src/lib/layer/layer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class LayerComponent
}
}

ngOnDestroy() {
ngOnDestroy(): void {
if (this.layerAdded()) {
const sourceIdAdded = this.sourceIdAdded();
this.mapService.removeLayer(this.id());
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-maplibre-gl/src/lib/map/map.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class MapComponent implements OnChanges, OnDestroy, MapEvent {
});
}

ngOnDestroy() {
ngOnDestroy(): void {
this.mapService.destroyMap();
}

Expand Down
7 changes: 5 additions & 2 deletions projects/ngx-maplibre-gl/src/lib/marker/marker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DestroyRef,
ElementRef,
OnChanges,
OnDestroy,
OnInit,
SimpleChanges,
ViewEncapsulation,
Expand Down Expand Up @@ -45,7 +46,7 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
})
export class MarkerComponent implements OnChanges, OnInit {
export class MarkerComponent implements OnChanges, OnInit, OnDestroy {
/** Init injection */
private readonly mapService = inject(MapService);
private readonly destroyRef = inject(DestroyRef);
Expand Down Expand Up @@ -109,8 +110,10 @@ export class MarkerComponent implements OnChanges, OnInit {
this.markerInstance.set(marker);
});
});
}

this.destroyRef.onDestroy(() => this.removeMarker());
ngOnDestroy(): void {
this.removeMarker();
}

ngOnInit() {
Expand Down
9 changes: 6 additions & 3 deletions projects/ngx-maplibre-gl/src/lib/popup/popup.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DestroyRef,
ElementRef,
OnChanges,
OnDestroy,
OnInit,
SimpleChanges,
afterNextRender,
Expand Down Expand Up @@ -43,7 +44,7 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
})
export class PopupComponent implements OnChanges, OnInit {
export class PopupComponent implements OnChanges, OnInit, OnDestroy {
/** Init injection */
private readonly destroyRef = inject(DestroyRef);
private readonly mapService = inject(MapService);
Expand Down Expand Up @@ -105,8 +106,6 @@ export class PopupComponent implements OnChanges, OnInit {
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe();
});

this.destroyRef.onDestroy(() => this.removePopupFromMarker());
}

ngOnInit() {
Expand Down Expand Up @@ -157,6 +156,10 @@ export class PopupComponent implements OnChanges, OnInit {
}
}

ngOnDestroy(): void {
this.removePopupFromMarker();
}

private createPopup() {
return this.mapService.createPopup(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class FeatureComponent implements OnInit, OnDestroy {
this.geoJSONSourceComponent._addFeature(this.feature);
}

ngOnDestroy() {
ngOnDestroy(): void {
this.geoJSONSourceComponent._removeFeature(this.feature);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ImageSourceComponent implements OnInit, OnDestroy, OnChanges {
});
}

ngOnDestroy() {
ngOnDestroy(): void {
const sourceId = this.sourceId();
if (sourceId !== null) {
this.mapService.removeSource(sourceId);
Expand Down
11 changes: 6 additions & 5 deletions projects/ngx-maplibre-gl/src/lib/source/source.directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
DestroyRef,
Directive,
OnDestroy,
OnInit,
inject,
input,
Expand All @@ -19,7 +20,7 @@ import { Source, SourceSpecification } from 'maplibre-gl';
@Directive({
standalone: true,
})
export class SourceDirective implements OnInit {
export class SourceDirective implements OnInit, OnDestroy {
/** Init injection */
readonly mapService = inject(MapService);
private readonly destroyRef = inject(DestroyRef);
Expand All @@ -35,10 +36,6 @@ export class SourceDirective implements OnInit {
private readonly loadSourceSubject = new Subject<void>();
readonly loadSource$ = this.loadSourceSubject.asObservable();

constructor() {
this.destroyRef.onDestroy(() => this.removeSource());
}

ngOnInit() {
this.mapService.mapLoaded$
.pipe(
Expand All @@ -54,6 +51,10 @@ export class SourceDirective implements OnInit {
.subscribe();
}

ngOnDestroy(): void {
this.removeSource();
}

refresh() {
this.removeSource();
this.loadSourceSubject.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class LiveUpdateFeatureComponent implements OnDestroy {
});
}

ngOnDestroy() {
ngOnDestroy(): void {
if (this.timer) {
clearInterval(this.timer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class LiveUpdateImageSourceComponent implements OnDestroy {
});
}

ngOnDestroy() {
ngOnDestroy(): void {
if (this.timer !== undefined) {
clearInterval(this.timer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class MarkerAlignmentComponent implements OnDestroy {
})
}

ngOnDestroy() {
ngOnDestroy(): void {
if (this.timer) {
clearInterval(this.timer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
viewChild,
input,
signal,
inject,
DestroyRef,
OnDestroy,
} from '@angular/core';
import {
MatPaginator,
Expand Down Expand Up @@ -139,8 +138,8 @@ export class ClusterPopupComponent implements OnChanges {
ClusterPopupComponent,
],
})
export class NgxClusterHtmlComponent {
private destroyRef = inject(DestroyRef);
export class NgxClusterHtmlComponent implements OnDestroy {
private timer: ReturnType<typeof setInterval>;
readonly earthquakes = signal<GeoJSON.FeatureCollection | null>(null);

readonly selectedCluster = signal<{
Expand All @@ -149,19 +148,20 @@ export class NgxClusterHtmlComponent {
} | null>(null);

constructor() {
let timer: ReturnType<typeof setInterval>;
afterNextRender(async () => {
const earthquakes: GeoJSON.FeatureCollection = (await import(
'./earthquakes.geo.json'
)) as any;
timer = setInterval(() => {
this.timer = setInterval(() => {
if (earthquakes.features.length) {
earthquakes.features.pop();
}
this.earthquakes.set({ ...earthquakes });
}, 500);
});
this.destroyRef.onDestroy(() => clearInterval(timer));
}
ngOnDestroy(): void {
clearInterval(this.timer)
}

selectCluster(event: MouseEvent, feature: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class StackblitzEditComponent implements AfterViewInit, OnDestroy {
private changeDetectorRef: ChangeDetectorRef
) {}

ngOnDestroy() {
ngOnDestroy(): void {
if (this.sub) {
this.sub.unsubscribe();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
ApplicationRef,
Component,
ComponentFactoryResolver,
DestroyRef,
Injector,
OnDestroy,
afterNextRender,
inject,
input,
Expand All @@ -21,10 +21,9 @@ import {
standalone: true,
imports: [PortalModule],
})
export class LayoutToolbarMenuComponent {
export class LayoutToolbarMenuComponent implements OnDestroy {
private readonly componentFactoryResolver = inject(ComponentFactoryResolver);
private readonly injector = inject(Injector);
private readonly destroyRef = inject(DestroyRef);
private readonly appRef = inject(ApplicationRef);

readonly position = input<'left' | 'right'>();
Expand All @@ -46,7 +45,9 @@ export class LayoutToolbarMenuComponent {
);
this.portalOutlet.attach(this.portal());
});
}

this.destroyRef.onDestroy(() => this.portalOutlet?.detach());
ngOnDestroy(): void {
this.portalOutlet?.detach()
}
}