Make TaskHandler.handle async

This commit is contained in:
Dominik Jain 2025-09-27 15:11:09 +02:00 committed by Dominik Jain
parent fca9298d20
commit 4350f18ab3
4 changed files with 8 additions and 6 deletions

View File

@ -73,5 +73,5 @@ export abstract class TaskHandler<TParams = any> {
*
* @param task - The task to be handled
*/
abstract handle(task: Task<TParams>): void;
abstract handle(task: Task<TParams>): Promise<void>;
}

View File

@ -28,7 +28,9 @@ penpot.ui.onMessage<string | { id: string; task: string; params: any }>((message
// New request-based message handling
if (typeof message === "object" && message.task && message.id) {
handlePluginTaskRequest(message);
handlePluginTaskRequest(message).catch((error) => {
console.error("Error in handlePluginTaskRequest:", error);
});
}
});
@ -37,7 +39,7 @@ penpot.ui.onMessage<string | { id: string; task: string; params: any }>((message
*
* @param request - The task request containing ID, task type and parameters
*/
function handlePluginTaskRequest(request: { id: string; task: string; params: any }): void {
async function handlePluginTaskRequest(request: { id: string; task: string; params: any }): Promise<void> {
console.log("Executing plugin task:", request.task, request.params);
const task = new Task(request.id, request.task, request.params);
@ -48,7 +50,7 @@ function handlePluginTaskRequest(request: { id: string; task: string; params: an
try {
// Cast the params to the expected type and handle the task
console.log("Processing task with handler:", handler);
handler.handle(task);
await handler.handle(task);
// check whether a response was sent and send a generic success if not
if (!task.isResponseSent) {

View File

@ -182,7 +182,7 @@ export class ExecuteCodeTaskHandler extends TaskHandler<ExecuteCodeTaskParams> {
};
}
handle(task: Task<ExecuteCodeTaskParams>): void {
async handle(task: Task<ExecuteCodeTaskParams>): Promise<void> {
if (!task.params.code) {
task.sendError("executeCode task requires 'code' parameter");
return;

View File

@ -7,7 +7,7 @@ import { PrintTextTaskParams } from "../../../common/src";
export class PrintTextTaskHandler extends TaskHandler<PrintTextTaskParams> {
readonly taskType = "printText";
handle(task: Task<PrintTextTaskParams>): void {
async handle(task: Task<PrintTextTaskParams>): Promise<void> {
if (!task.params.text) {
throw new Error("printText task requires 'text' parameter");
}