diff --git a/src/keyboard_macro.js b/src/keyboard_macro.js index 7719716c..0534f265 100644 --- a/src/keyboard_macro.js +++ b/src/keyboard_macro.js @@ -25,7 +25,7 @@ const KeyboardMacro = function({ awaitController }) { const sequence = CommandSequence(); const internalCommands = new Map(); - const makeGuardedCommand = function(body, teardown) { + const makeGuardedCommand = function(body) { return async function(args) { if (locked) { return; @@ -37,9 +37,6 @@ const KeyboardMacro = function({ awaitController }) { console.error(error); console.info('kb-macro: Exception in guarded command'); } - if (teardown) { - teardown(); - } locked = false; }; }; @@ -140,7 +137,10 @@ const KeyboardMacro = function({ awaitController }) { }; const playback = makeGuardedCommand(async function(args) { - if (!recording) { + if (recording) { + return; + } + try { changePlaybackState(true, PlaybackStateReason.Start); shouldAbortPlayback = false; args = (args && typeof(args) === 'object') ? args : {}; @@ -155,13 +155,13 @@ const KeyboardMacro = function({ awaitController }) { } } } - } - }, function teardown() { - if (shouldAbortPlayback) { - changePlaybackState(false, PlaybackStateReason.Abort); - shouldAbortPlayback = false; - } else { - changePlaybackState(false, PlaybackStateReason.Finish); + } finally { + if (shouldAbortPlayback) { + changePlaybackState(false, PlaybackStateReason.Abort); + shouldAbortPlayback = false; + } else { + changePlaybackState(false, PlaybackStateReason.Finish); + } } }); diff --git a/test/suite/keyboard_macro.test.js b/test/suite/keyboard_macro.test.js index 53c9dfd9..90a4bff3 100644 --- a/test/suite/keyboard_macro.test.js +++ b/test/suite/keyboard_macro.test.js @@ -319,6 +319,17 @@ describe('KeybaordMacro', () => { await keyboardMacro.playback({ repeat: 5 }); assert.deepStrictEqual(logs, [ 'begin', 'end' ]); }); + it('should do nothing when recording is ongoing', async () => { + keyboardMacro.startRecording(); + keyboardMacro.push({ command: 'internal:log' }); + keyboardMacro.finishRecording(); + + keyboardMacro.startRecording(); + keyboardMacro.push({ command: 'internal:log' }); + await keyboardMacro.playback(); + keyboardMacro.finishRecording(); + assert.deepStrictEqual(logs, []); + }); it('should prevent reentry', async () => { keyboardMacro.startRecording(); keyboardMacro.push({ command: 'internal:log' });