Skip to content

Commit

Permalink
fix(release): update to angular 17 and bump all dependencies to latest
Browse files Browse the repository at this point in the history
Put back ngOnDestroy
  • Loading branch information
Pilpin committed Mar 16, 2024
1 parent 5323d0c commit 26a6048
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions projects/ngneat/hotkeys/src/lib/hotkeys.directive.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
computed,
DestroyRef,
Directive,
ElementRef,
EventEmitter,
inject,
input,
OnChanges,
OnDestroy,
Output,
SimpleChanges,
} from '@angular/core';
Expand All @@ -15,16 +15,14 @@ import { mergeAll } from 'rxjs/operators';

import { Hotkey, HotkeysService, Options as ServiceOptions } from './hotkeys.service';
import { coerceArray } from './utils/array';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';

type Options = Omit<ServiceOptions, 'group' | 'element' | 'description'>;

@Directive({
standalone: true,
selector: '[hotkeys]',
})
export class HotkeysDirective implements OnChanges {
private destroyRef = inject(DestroyRef);
export class HotkeysDirective implements OnChanges, OnDestroy {
private hotkeysService = inject(HotkeysService);
private elementRef = inject(ElementRef);
private subscription: Subscription;
Expand All @@ -44,18 +42,25 @@ export class HotkeysDirective implements OnChanges {
}));

ngOnChanges(changes: SimpleChanges): void {
if (this.subscription) {
this.subscription.unsubscribe();
}
this.subscription = null;

this.deleteHotkeys();
if (!this.hotkeys) {
return;
}

this.setHotkeys(this._hotkey());
}

ngOnDestroy() {
this.deleteHotkeys();
}

private deleteHotkeys() {
if (this.subscription) {
this.subscription.unsubscribe();
}
this.subscription = null;
}

private setHotkeys(hotkeys: Hotkey | Hotkey[]) {
const coercedHotkeys = coerceArray(hotkeys);
this.subscription = merge(
Expand All @@ -65,7 +70,7 @@ export class HotkeysDirective implements OnChanges {
: this.hotkeysService.addShortcut({ ...hotkey, element: this.elementRef.nativeElement });
}),
)
.pipe(takeUntilDestroyed(this.destroyRef), mergeAll())
.pipe(mergeAll())
.subscribe((e) => this.hotkey.next(e));
}
}

0 comments on commit 26a6048

Please sign in to comment.