Skip to content

Commit

Permalink
feat: cmdBus.on support async
Browse files Browse the repository at this point in the history
  • Loading branch information
dockfries committed Sep 25, 2022
1 parent 7472930 commit 24f86f3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "omp-node-lib",
"version": "0.2.1",
"version": "0.2.2",
"description": "Better with omp-node-ts",
"main": "dist/bundle.js",
"types": "dist/bundle.d.ts",
Expand Down
8 changes: 5 additions & 3 deletions src/controllers/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ export class CmdBus {
CmdBus.eventList.splice(idx, 1);
}

static emit<T extends BasePlayer>(
static async emit<T extends BasePlayer>(
player: T,
userEventName: TEventName,
userEventArgs: Array<string>
): boolean {
): Promise<boolean> {
const idx: number = CmdBus.findEventIdxByName(userEventName);
if (idx > -1) {
const result = CmdBus.eventList[idx].fn.apply(player, userEventArgs);
let result = CmdBus.eventList[idx].fn.apply(player, userEventArgs);
if (result instanceof Promise) result = await result;
if (result === undefined || result === null) return false;
if (typeof result === "number") return Boolean(result);
return result;
}
Expand Down
9 changes: 4 additions & 5 deletions src/controllers/player/playerEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,10 @@ export abstract class BasePlayerEvent<
Use eventBus to observe and subscribe to level 1 instructions,
support string and array pass, array used for alias.
*/
const exist: boolean = CmdBus.emit(
p,
regCmdtext[0],
regCmdtext.splice(1)
);
let exist = false;
(async () => {
exist = await CmdBus.emit(p, regCmdtext[0], regCmdtext.splice(1));
})();
if (exist) return 1;
// The command %s you entered does not exist
return this.onCommandError(p, regCmdtext.join(" "), ICmdErrInfo.notExist);
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type TEventName = string | string[];
export type TEventFunc = <T extends BasePlayer>(
this: T,
...args: string[]
) => boolean | number;
) => boolean | number | void | Promise<boolean | number | void>;

export type TLocales = Record<string | number, ILocale>;

Expand Down

0 comments on commit 24f86f3

Please sign in to comment.