Skip to content

Commit

Permalink
feat: add #subscribe() methods to every manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Amgelo563 committed Jul 9, 2024
1 parent 467a63a commit fa55bb3
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/features/command/CommandManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export interface CommandManager extends BotAware, BotLifecycleObserver {
editCommands(...commands: TopLevelCommand[]): Awaitable<this>;

/**
* Subscribes an event subscriber to the manager's bus.
* Subscribes a list of event subscribers to the manager's bus.
*
* Alias of:
* ```
Expand All @@ -98,8 +98,8 @@ export interface CommandManager extends BotAware, BotLifecycleObserver {
* ```
*/
subscribe(
subscriber: EventSubscriber<CommandEventArgs, keyof CommandEventArgs>,
): Awaitable<void>;
...subscribers: EventSubscriber<CommandEventArgs, keyof CommandEventArgs>[]
): Awaitable<this>;

/**
* Deploys the commands on Discord, if that wasn't already done on
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/features/plugin/PluginManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Amgelo563
* Copyright (c) 2024 Amgelo563
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -30,6 +30,7 @@ import type { Identifier } from '../../identity/Identifier.js';
import type { BotLifecycleObserver } from '../../types/BotLifecycleObserver';
import type { ClassImplements } from '../../types/ClassImplements.js';
import type { EventBus } from '../event/bus/EventBus.js';
import type { EventSubscriber } from '../event/subscriber/EventSubscriber';
import type { PluginEventArgs } from './events/PluginEvent.js';
import type { NyxPlugin } from './plugin/NyxPlugin.js';

Expand All @@ -50,6 +51,19 @@ export interface PluginManager extends BotAware, BotLifecycleObserver {
*/
unregister(pluginOrId: NyxPlugin | Identifier): Awaitable<this>;

/**
* Subscribes a list of event subscribers to the manager's bus.
*
* Alias of:
* ```
* const managerBus = pluginManager.getEventBus();
* await managerBus.subscribe(subscriber);
* ```
*/
subscribe(
...subscribers: EventSubscriber<PluginEventArgs, keyof PluginEventArgs>[]
): Awaitable<this>;

/** Returns a plugin by its ID. */
getPluginById(id: Identifier): NyxPlugin | null;

Expand Down
19 changes: 18 additions & 1 deletion packages/core/src/features/schedule/ScheduleManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Amgelo563
* Copyright (c) 2024 Amgelo563
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -29,6 +29,7 @@ import type { Identifier } from '../../identity/Identifier.js';
import type { BotLifecycleObserver } from '../../types/BotLifecycleObserver';
import type { ClassImplements } from '../../types/ClassImplements.js';
import type { EventBus } from '../event/bus/EventBus.js';
import type { EventSubscriber } from '../event/subscriber/EventSubscriber';
import type { ScheduleEventArgs } from './events/ScheduleEvent.js';
import type { ScheduleExecutor } from './execution/executor/ScheduleExecutor.js';
import type { ScheduleTickMeta } from './execution/meta/ScheduleTickMeta.js';
Expand Down Expand Up @@ -64,6 +65,22 @@ export interface ScheduleManager extends BotLifecycleObserver, BotAware {
meta?: ScheduleTickMeta,
): Awaitable<this>;

/**
* Subscribes a list of event subscribers to the manager's bus.
*
* Alias of:
* ```
* const managerBus = scheduleManager.getEventBus();
* await managerBus.subscribe(subscriber);
* ```
*/
subscribe(
...subscribers: EventSubscriber<
ScheduleEventArgs,
keyof ScheduleEventArgs
>[]
): Awaitable<this>;

/** Returns the job that belongs to the passed schedule or schedule ID. */
getJobForSchedule(
scheduleOrId: Schedule | Identifier,
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/features/session/SessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ export interface SessionManager extends BotAware, BotLifecycleObserver {
subscriber: EventSubscriber<ClientEvents, Events.InteractionCreate>,
): Awaitable<this>;

/**
* Subscribes a list of event subscribers to the manager's bus.
*
* Alias of:
* ```
* const managerBus = sessionManager.getEventBus();
* await managerBus.subscribe(subscriber);
* ```
*/
subscribe(
...subscribers: EventSubscriber<SessionEventArgs, keyof SessionEventArgs>[]
): Awaitable<this>;

/** Returns the {@link SessionPromiseRepository} for this manager. */
getPromiseRepository(): SessionPromiseRepository;

Expand Down
20 changes: 19 additions & 1 deletion packages/core/src/service/BotService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Amgelo563
* Copyright (c) 2024 Amgelo563
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,6 +26,7 @@ import type { Awaitable } from 'discord.js';
import type { BotAware } from '../bot/BotAware.js';
import type { NyxBot } from '../bot/NyxBot.js';
import type { EventBus } from '../features/event/bus/EventBus.js';
import type { EventSubscriber } from '../features/event/subscriber/EventSubscriber';
import type { BotStatus } from './BotStatus.js';
import type { BotServiceEventArgs } from './events/BotServiceEvent.js';

Expand Down Expand Up @@ -69,6 +70,23 @@ export interface BotService extends BotAware {
*/
isRunning(): boolean;

/**
* Subscribes a list of event subscribers to the service's bus.
*
* Alias of:
* ```
* const managerBus = botService.getEventBus();
* await managerBus.subscribe(subscriber);
* ```
*/
subscribe(
...subscribers: EventSubscriber<
BotServiceEventArgs,
keyof BotServiceEventArgs
>[]
): Awaitable<this>;

/** Returns the {@link EventBus} for the bot's service events. */
getEventBus(): EventBus<BotServiceEventArgs>;

/** Returns the current status of the bot. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,10 @@ export class DefaultCommandManager implements CommandManager {
}

public async subscribe(
subscriber: EventSubscriber<CommandEventArgs, keyof CommandEventArgs>,
): Promise<void> {
await this.eventBus.subscribe(subscriber);
...subscribers: EventSubscriber<CommandEventArgs, keyof CommandEventArgs>[]
): Promise<this> {
await this.eventBus.subscribe(...subscribers);
return this;
}

public async deploy(): Promise<void> {
Expand Down
10 changes: 9 additions & 1 deletion packages/framework/src/features/plugin/DefaultPluginManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Amgelo563
* Copyright (c) 2024 Amgelo563
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,6 +27,7 @@ import { Collection } from '@discordjs/collection';
import type {
ClassImplements,
EventBus,
EventSubscriber,
Identifier,
NyxBot,
NyxPlugin,
Expand Down Expand Up @@ -131,6 +132,13 @@ export class DefaultPluginManager implements PluginManager {
return this;
}

public async subscribe(
...subscribers: EventSubscriber<PluginEventArgs, keyof PluginEventArgs>[]
): Promise<this> {
await this.bus.subscribe(...subscribers);
return this;
}

public getPluginById(id: Identifier): NyxPlugin | null {
return this.plugins.get(id) ?? null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Amgelo563
* Copyright (c) 2024 Amgelo563
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,6 +25,7 @@
import type {
ClassImplements,
EventBus,
EventSubscriber,
Identifier,
NyxBot,
Schedule,
Expand Down Expand Up @@ -225,6 +226,16 @@ export class DefaultScheduleManager implements ScheduleManager {
return this;
}

public async subscribe(
...subscribers: EventSubscriber<
ScheduleEventArgs,
keyof ScheduleEventArgs
>[]
): Promise<this> {
await this.bus.subscribe(...subscribers);
return this;
}

public getScheduleByID(id: Identifier): Schedule | null {
return this.repository.getScheduleByID(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ export class DefaultSessionManager implements SessionManager {
return (await this.repository.get(sessionCustomId)) ?? null;
}

public async subscribe(
...subscribers: EventSubscriber<SessionEventArgs, keyof SessionEventArgs>[]
): Promise<this> {
await this.bus.subscribe(...subscribers);
return this;
}

public async setUpdateSubscriber(
subscriber: EventSubscriber<ClientEvents, Events.InteractionCreate>,
): Promise<this> {
Expand Down
27 changes: 19 additions & 8 deletions packages/framework/src/service/DefaultBotService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Amgelo563
* Copyright (c) 2024 Amgelo563
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,6 +27,7 @@ import type {
BotServiceEventArgs,
BotStatus,
EventBus,
EventSubscriber,
NyxBot,
} from '@nyx-discord/core';
import {
Expand All @@ -52,6 +53,13 @@ export class DefaultBotService implements BotService {

protected status: BotStatus = BotStatusEnum.Unprepared;

constructor(bot: NyxBot, bus: EventBus<BotServiceEventArgs>) {
this.bot = bot;
this.bus = bus;

this.startPromise = DefaultBotService.createStartPromiseData();
}

public static createStartPromiseData(): StartPromiseData {
const startPromise: Partial<StartPromiseData> = {};
startPromise.promise = new Promise<NyxBot>((resolve, reject) => {
Expand All @@ -62,13 +70,6 @@ export class DefaultBotService implements BotService {
return startPromise as StartPromiseData;
}

constructor(bot: NyxBot, bus: EventBus<BotServiceEventArgs>) {
this.bot = bot;
this.bus = bus;

this.startPromise = DefaultBotService.createStartPromiseData();
}

public static create(bot: NyxBot): BotService {
const busId = Symbol('BotServiceEventBus');

Expand Down Expand Up @@ -194,6 +195,16 @@ export class DefaultBotService implements BotService {
return this.status === BotStatusEnum.Running;
}

public async subscribe(
...subscribers: EventSubscriber<
BotServiceEventArgs,
keyof BotServiceEventArgs
>[]
): Promise<this> {
await this.bus.subscribe(...subscribers);
return this;
}

public getStartPromise(): Promise<NyxBot> {
return this.startPromise.promise;
}
Expand Down

0 comments on commit fa55bb3

Please sign in to comment.