Skip to content

Commit

Permalink
add post-command invocation hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Dec 14, 2024
1 parent 0539ab0 commit 867bae9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import revxrsal.commands.node.ExecutionContext;

/**
* A hook that gets called when a command is executed
* A hook that gets called when a command is about to be executed
*/
@FunctionalInterface
public interface CommandExecutedHook<A extends CommandActor> extends Hook {
Expand Down
27 changes: 27 additions & 0 deletions common/src/main/java/revxrsal/commands/hook/Hooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ public boolean onCommandExecuted(@NotNull ExecutableCommand<A> command, @NotNull
return !cancelHandle.wasCancelled();
}

/**
* Calls all {@link PostCommandExecutedHook post-execution hooks}.
*
* @param command The command that was executed
* @param context The execution context
*/
@ApiStatus.Internal
@SuppressWarnings({"rawtypes", "unchecked"})
public void onPostCommandExecuted(@NotNull ExecutableCommand<A> command, @NotNull ExecutionContext<A> context) {
for (Hook hook : hooks) {
if (hook instanceof PostCommandExecutedHook) {
PostCommandExecutedHook<A> executedHook = (PostCommandExecutedHook) hook;
executedHook.onPostExecuted(command, context);
}
}
}

/**
* A builder for {@link Hooks}
*
Expand All @@ -156,6 +173,16 @@ public static class Builder<A extends CommandActor> {
return hook(hook);
}

/**
* Adds a hook that runs after a command is executed
*
* @param hook Hook to register
* @return this builder
*/
public @NotNull Builder<A> onPostCommandExecuted(@NotNull PostCommandExecutedHook<? super A> hook) {
return hook(hook);
}

/**
* Adds a hook that runs after a command is registered
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of sweeper, licensed under the MIT License.
*
* Copyright (c) Revxrsal <reflxction.github@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package revxrsal.commands.hook;

import org.jetbrains.annotations.NotNull;
import revxrsal.commands.command.CommandActor;
import revxrsal.commands.command.ExecutableCommand;
import revxrsal.commands.node.ExecutionContext;

/**
* A hook that gets called when a command is executed.
* <p>
* This hook is only fired for commands that have been successfully executed.
* Any command that errors in the process will not be invoked here.
*/
@FunctionalInterface
public interface PostCommandExecutedHook<A extends CommandActor> extends Hook {

/**
* Invokes the hook for a command whose action has been successfully executed
*
* @param command The command that will be executed
* @param context The execution context
*/
void onPostExecuted(@NotNull ExecutableCommand<A> command, @NotNull ExecutionContext<A> context);

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void execute(ExecutionContext<A> context) {
//noinspection rawtypes
function.responseHandler().handleResponse(result, (ExecutionContext) context);
}
context.lamp().hooks().onPostCommandExecuted(context.command(), context);
} catch (Throwable t) {
context.lamp().handleException(t, ErrorContext.executingFunction(context));
}
Expand Down

0 comments on commit 867bae9

Please sign in to comment.