Skip to content

Commit

Permalink
Add PlaybackStateReason to distinguish finish and abort
Browse files Browse the repository at this point in the history
  • Loading branch information
tshino committed Dec 31, 2021
1 parent d30d20d commit 78ccabc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
20 changes: 15 additions & 5 deletions src/keyboard_macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -199,6 +208,7 @@ const KeyboardMacro = function({ awaitController }) {

return {
RecordingStateReason,
PlaybackStateReason,
onChangeRecordingState,
onChangePlaybackState,
onBeginWrappedCommand,
Expand Down
14 changes: 12 additions & 2 deletions test/suite/keyboard_macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
]);
});
});
Expand Down

0 comments on commit 78ccabc

Please sign in to comment.