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
It seems that unsubscribing addShortcut()'s subscription disposes Angular EventManager's eventListener, but removeShortcuts() does not.
Expected behavior
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.