Skip to content

Commit

Permalink
chore(module_manager): don't check remaining handlers if a specific s…
Browse files Browse the repository at this point in the history
…ymbol was returned (#230)

Closes #66.
  • Loading branch information
rojvv committed Jul 27, 2022
1 parent 0773eb3 commit 6ec43f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion handlers/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NewMessageEvent, TelegramClient } from "$grm";

export const End = Symbol();

export interface HandlerFuncParams {
client: TelegramClient;
event: NewMessageEvent;
Expand All @@ -10,5 +12,7 @@ export abstract class Handler {
{ client, event }: HandlerFuncParams,
): Promise<boolean> | boolean;

abstract handle({ client, event }: HandlerFuncParams): Promise<void>;
abstract handle(
{ client, event }: HandlerFuncParams,
): Promise<void | typeof End>;
}
7 changes: 5 additions & 2 deletions module_manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Api, NewMessageEvent, TelegramClient } from "$grm";
import { join, resolve, toFileUrl } from "./deps.ts";
import { CommandHandler } from "./handlers/mod.ts";
import { CommandHandler, End } from "./handlers/mod.ts";
import { updateMessage } from "./helpers.ts";
import { getHelp, isModule, Module } from "./module.ts";

Expand Down Expand Up @@ -200,7 +200,10 @@ export class ModuleManager {
for (const handler of handlers) {
if (await handler.check({ client: this.client, event })) {
try {
await handler.handle({ client: this.client, event });
const result = await handler.handle({ client: this.client, event });
if (typeof result === "symbol" && result == End) {
break;
}
} catch (err) {
console.error(err);
try {
Expand Down

0 comments on commit 6ec43f5

Please sign in to comment.