Skip to content

Commit

Permalink
fix: trigger api support multi listeners jaywcjlove#403
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukiniro committed Nov 23, 2022
1 parent 9d45f35 commit f0dc168
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,12 @@ function hotkeys(key, option, method) {

function trigger(shortcut, scope = 'all') {
Object.keys(_handlers).forEach((key) => {
const data = _handlers[key].find((item) => item.scope === scope && item.shortcut === shortcut);
if (data && data.method) {
data.method();
}
const dataList = _handlers[key].filter((item) => item.scope === scope && item.shortcut === shortcut);
dataList.forEach((data) => {
if (data && data.method) {
data.method();
}
});
});
}

Expand Down
22 changes: 22 additions & 0 deletions test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,28 @@ describe('\n Hotkeys.js Test Case222.\n', () => {
await hotkeys.unbind('a');
});

test('Hotkey trigger whith shortcut.', async () => {
let count = 0;
hotkeys('a', () => {
count++;
});
hotkeys.trigger('a');
expect(count).toBe(1);
hotkeys.unbind('a');
});

test('Hotkey trigger whith multi shortcut.', async () => {
let count = 0;
for (let i = 0; i < 3; i++) {
hotkeys('a', () => {
count++;
});
}
hotkeys.trigger('a');
expect(count).toBe(3);
hotkeys.unbind('a');
});

afterAll(async () => {
await browser.close();
});
Expand Down

0 comments on commit f0dc168

Please sign in to comment.