From 78ccabc1fe072457da27d19921e45f22467784cb Mon Sep 17 00:00:00 2001 From: tshino Date: Fri, 31 Dec 2021 12:09:53 +0900 Subject: [PATCH] Add PlaybackStateReason to distinguish finish and abort --- src/extension.js | 6 +++++- src/keyboard_macro.js | 20 +++++++++++++++----- test/suite/keyboard_macro.test.js | 14 ++++++++++++-- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/extension.js b/src/extension.js index e77feac6..e297a84d 100644 --- a/src/extension.js +++ b/src/extension.js @@ -69,9 +69,13 @@ function activate(context) { ); addEventListener( keyboardMacro.onChangePlaybackState, - function({ playing }) { + function({ playing, reason }) { const contextName = ContextPrefix + 'playing'; vscode.commands.executeCommand('setContext', contextName, playing); + + if (playing === false && reason === keyboardMacro.PlaybackStateReason.Abort) { + vscode.window.setStatusBarMessage('Playback aborted!', 3000); + } } ); addEventListener( diff --git a/src/keyboard_macro.js b/src/keyboard_macro.js index cb3584de..7719716c 100644 --- a/src/keyboard_macro.js +++ b/src/keyboard_macro.js @@ -8,6 +8,11 @@ const KeyboardMacro = function({ awaitController }) { Cancel: 1, Finish: 2 }; + const PlaybackStateReason = { + Start: 0, + Abort: 1, + Finish: 2 + }; let onChangeRecordingStateCallback = null; let onChangePlaybackStateCallback = null; @@ -66,10 +71,10 @@ const KeyboardMacro = function({ awaitController }) { const onChangePlaybackState = function(callback) { onChangePlaybackStateCallback = callback; }; - const changePlaybackState = function(newState) { + const changePlaybackState = function(newState, reason) { playing = newState; if (onChangePlaybackStateCallback) { - onChangePlaybackStateCallback({ playing }); + onChangePlaybackStateCallback({ playing, reason }); } }; const onBeginWrappedCommand = function(callback) { @@ -136,7 +141,7 @@ const KeyboardMacro = function({ awaitController }) { const playback = makeGuardedCommand(async function(args) { if (!recording) { - changePlaybackState(true); + changePlaybackState(true, PlaybackStateReason.Start); shouldAbortPlayback = false; args = (args && typeof(args) === 'object') ? args : {}; const repeat = typeof(args.repeat) === 'number' ? args.repeat : 1; @@ -152,8 +157,12 @@ const KeyboardMacro = function({ awaitController }) { } } }, function teardown() { - changePlaybackState(false); - shouldAbortPlayback = false; + if (shouldAbortPlayback) { + changePlaybackState(false, PlaybackStateReason.Abort); + shouldAbortPlayback = false; + } else { + changePlaybackState(false, PlaybackStateReason.Finish); + } }); const abortPlayback = async function() { @@ -199,6 +208,7 @@ const KeyboardMacro = function({ awaitController }) { return { RecordingStateReason, + PlaybackStateReason, onChangeRecordingState, onChangePlaybackState, onBeginWrappedCommand, diff --git a/test/suite/keyboard_macro.test.js b/test/suite/keyboard_macro.test.js index d21b233e..53c9dfd9 100644 --- a/test/suite/keyboard_macro.test.js +++ b/test/suite/keyboard_macro.test.js @@ -45,14 +45,24 @@ describe('KeybaordMacro', () => { keyboardMacro.onChangePlaybackState((event) => { logs.push(event); }); + keyboardMacro.registerInternalCommand('internal:delay', async () => { + await TestUtil.sleep(100); + }); keyboardMacro.startRecording(); + keyboardMacro.push({ command: 'internal:delay' }); keyboardMacro.finishRecording(); + await keyboardMacro.playback(); + const promise = keyboardMacro.playback(); + keyboardMacro.abortPlayback(); + await promise; assert.deepStrictEqual(logs, [ - { playing: true }, - { playing: false } + { playing: true, reason: keyboardMacro.PlaybackStateReason.Start }, + { playing: false, reason: keyboardMacro.PlaybackStateReason.Finish }, + { playing: true, reason: keyboardMacro.PlaybackStateReason.Start }, + { playing: false, reason: keyboardMacro.PlaybackStateReason.Abort } ]); }); });