ngneat / hotkeys

🤖 A declarative library for handling hotkeys in Angular applications

Home Page:https://netbasal.com/diy-keyboard-shortcuts-in-your-angular-application-4704734547a2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pressing hotkeys removed by removeShortcuts() may cause TypeError

ebichan38 opened this issue · comments

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Performance issue
[ ] Feature request
[x] Documentation issue or request
[ ] Support request
[ ] Other... Please describe:

Current behavior

Pressing the hotkey removed by removeShortcuts() may cause TypeError.

Assume that a user presses the hotkey which is already removed by removeShortcuts().

example 1:

    ngOnInit(): void {
        const subscription = this.hotkeys.addShortcut({keys: 'shift.a'}).subscribe((e) => {console.log('e;',e)});
        subscription.unsubscribe();
        this.hotkeys.removeShortcuts('shift.a');
    }

This works fine. Pressing shift.a does nothing as expected.

example 2:

  ngOnInit(): void {
    const subscription = this.hotkeys.addShortcut({keys: 'shift.a'}).subscribe((e) => {console.log('e;',e)});
    this.hotkeys.removeShortcuts('shift.a');
  }

In this case, pressing shift.a causes ERROR TypeError: Cannot read property 'allowIn' of undefined
on

const excludedTargets = this.getExcludedTargets(hotkey.allowIn || []);
.

It seems that unsubscribing addShortcut()'s subscription disposes Angular EventManager's eventListener, but removeShortcuts() does not.

Expected behavior

#6
#9

Reading the original issue & PR, I could not find whether the current behavior is by design or not.

If it's not by design, removeShortcuts() should dispose EventManager's eventListener.
If it's by design, adding the explanation of this behavior to the docs might be helpful.

Minimal reproduction of the problem with instructions

https://github.com/ebichan38/hotkeys-unsubscribe

unsubscribe.component for example 1, and no-unsubscribe.component is for example 2.

What is the motivation / use case for changing the behavior?

removeShortcuts() might not work properly.

Environment


Angular CLI: 12.1.1


Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

You're welcome to create a PR that unsubscribes when calling remove

Okay, I'll investigate and create a PR.