Skip to content

Commit

Permalink
Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tshino committed Jan 3, 2022
1 parent c0b2af6 commit c7d5b29
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/keyboard_macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,9 +37,6 @@ const KeyboardMacro = function({ awaitController }) {
console.error(error);
console.info('kb-macro: Exception in guarded command');
}
if (teardown) {
teardown();
}
locked = false;
};
};
Expand Down Expand Up @@ -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 : {};
Expand All @@ -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);
}
}
});

Expand Down
11 changes: 11 additions & 0 deletions test/suite/keyboard_macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down

0 comments on commit c7d5b29

Please sign in to comment.