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

Pressing hotkeys removed by removeShortcuts() may cause TypeError #30

Closed
ebichan38 opened this issue Jul 14, 2021 · 2 comments
Closed

Comments

@ebichan38
Copy link

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

@NetanelBasal
Copy link
Member

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

@ebichan38
Copy link
Author

Okay, I'll investigate and create a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants