Skip to content

Commit

Permalink
Polish context.execute result mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangoree committed Dec 22, 2023
1 parent 1758ed4 commit 9adef31
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions packages/clide-js/src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,17 @@ export class Context<TOptions extends OptionsConfig = OptionsConfig> {

let _initialData = initialData;
let skip = false;
let newResult = initialData;

await this.hooks.call('preExecute', {
initialData,
state,
setInitialData: (data) => {
_initialData = data;
newResult = data;
},
setResultAndSkip: (result) => {
this._result = result;
newResult = result;
skip = true;
},
skip: () => {
Expand All @@ -330,25 +332,21 @@ export class Context<TOptions extends OptionsConfig = OptionsConfig> {
if (!skip) {
try {
await state.start(_initialData);
newResult = state.data;
} catch (error) {
await this.throw(error);
}
}

let didOverrideResult = false;

await this.hooks.call('postExecute', {
result: this._result,
result: newResult,
state,
setResult: (result) => {
this._result = result;
didOverrideResult = true;
newResult = result;
},
});

// Only set the result to the state's data if it wasn't overridden by the
// postExecute hook and the command wasn't skipped
if (!didOverrideResult && !skip) this._result = state.data;
this._result = newResult;
};

/**
Expand Down

0 comments on commit 9adef31

Please sign in to comment.