Skip to content

Commit 59d08ef

Browse files
authored
fix: remove rxjs (#376)
* firstcommit * removerxjs * document-task * documentation+clean * fixregres * fix+regress * fix+regres+errorhandling
1 parent 7deb79e commit 59d08ef

18 files changed

+968
-904
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
"@sern/ioc": "^1.1.0",
3939
"callsites": "^3.1.0",
4040
"cron": "^3.1.7",
41-
"deepmerge": "^4.3.1",
42-
"rxjs": "^7.8.0"
41+
"deepmerge": "^4.3.1"
4342
},
4443
"devDependencies": {
4544
"@faker-js/faker": "^8.0.1",

src/core/functions.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,22 @@ import type {
1111
import { ApplicationCommandOptionType, InteractionType } from 'discord.js';
1212
import { PluginType } from './structures/enums';
1313
import assert from 'assert';
14-
import type { Payload } from '../types/utility';
14+
import type { Payload, UnpackedDependencies } from '../types/utility';
15+
16+
export const createSDT = (module: Module, deps: UnpackedDependencies, params: string|undefined) => {
17+
return {
18+
state: {},
19+
deps,
20+
params,
21+
type: module.type,
22+
module: {
23+
name: module.name,
24+
description: module.description,
25+
locals: module.locals,
26+
meta: module.meta
27+
}
28+
}
29+
}
1530

1631
/**
1732
* Removes the first character(s) _[depending on prefix length]_ of the message

src/core/ioc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function makeDependencies (conf: ValidDependencyConfig) {
7070
}
7171
container.addSingleton('@sern/errors', new __Services.DefaultErrorHandling);
7272
container.addSingleton('@sern/modules', new Map);
73-
container.addSingleton('@sern/emitter', new EventEmitter)
73+
container.addSingleton('@sern/emitter', new EventEmitter({ captureRejections: true }))
7474
container.addSingleton('@sern/scheduler', new __Services.TaskScheduler)
7575
conf(dependencyBuilder(container));
7676
await container.ready();

src/core/modules.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,34 @@ export function discordEvent<T extends keyof ClientEvents>(mod: {
102102
return eventModule({ type: EventType.Discord, ...mod, });
103103
}
104104

105-
export function scheduledTask(ism: ScheduledTask) { return ism }
105+
/**
106+
* Creates a scheduled task that can be executed at specified intervals using cron patterns
107+
*
108+
* @param {ScheduledTask} ism - The scheduled task configuration object
109+
* @param {string} ism.trigger - A cron pattern that determines when the task should execute
110+
* Format: "* * * * *" (minute hour day month day-of-week)
111+
* @param {Function} ism.execute - The function to execute when the task is triggered
112+
* @param {Object} ism.execute.context - The execution context passed to the task
113+
*
114+
* @returns {ScheduledTask} The configured scheduled task
115+
*
116+
* @example
117+
* // Create a task that runs every minute
118+
* export default scheduledTask({
119+
* trigger: "* * * * *",
120+
* execute: (context) => {
121+
* console.log("Task executed!");
122+
* }
123+
* });
124+
*
125+
* @remarks
126+
* - Tasks must be placed in the 'tasks' directory specified in your config
127+
* - The file name serves as a unique identifier for the task
128+
* - Tasks can be cancelled using deps['@sern/scheduler'].kill(uuid)
129+
*
130+
* @see {@link https://crontab.guru/} for testing and creating cron patterns
131+
*/
132+
export function scheduledTask(ism: ScheduledTask): ScheduledTask {
133+
return ism
134+
}
106135

src/core/plugin.ts

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,107 @@ export function makePlugin<V extends unknown[]>(
1515
export function EventInitPlugin(execute: (args: InitArgs) => PluginResult) {
1616
return makePlugin(PluginType.Init, execute);
1717
}
18+
1819
/**
20+
* Creates an initialization plugin for command preprocessing and modification
21+
*
1922
* @since 2.5.0
23+
* @template I - Extends CommandType to enforce type safety for command modules
24+
*
25+
* @param {function} execute - Function to execute during command initialization
26+
* @param {InitArgs<T>} execute.args - The initialization arguments
27+
* @param {T} execute.args.module - The command module being initialized
28+
* @param {string} execute.args.absPath - The absolute path to the module file
29+
* @param {Dependencies} execute.args.deps - Dependency injection container
30+
*
31+
* @returns {Plugin} A plugin that runs during command initialization
32+
*
33+
* @example
34+
* // Plugin to update command description
35+
* export const updateDescription = (description: string) => {
36+
* return CommandInitPlugin(({ deps }) => {
37+
* if(description.length > 100) {
38+
* deps.logger?.info({ message: "Invalid description" })
39+
* return controller.stop("From updateDescription: description is invalid");
40+
* }
41+
* module.description = description;
42+
* return controller.next();
43+
* });
44+
* };
45+
*
46+
* @example
47+
* // Plugin to store registration date in module locals
48+
* export const dateRegistered = () => {
49+
* return CommandInitPlugin(({ module }) => {
50+
* module.locals.registered = Date.now()
51+
* return controller.next();
52+
* });
53+
* };
54+
*
55+
* @remarks
56+
* - Init plugins can modify how commands are loaded and perform preprocessing
57+
* - The module.locals object can be used to store custom plugin-specific data
58+
* - Be careful when modifying module fields as multiple plugins may interact with them
59+
* - Use controller.next() to continue to the next plugin
60+
* - Use controller.stop(reason) to halt plugin execution
2061
*/
2162
export function CommandInitPlugin<I extends CommandType>(
2263
execute: (args: InitArgs) => PluginResult
23-
) {
64+
): Plugin {
2465
return makePlugin(PluginType.Init, execute);
2566
}
67+
2668
/**
69+
* Creates a control plugin for command preprocessing, filtering, and state management
70+
*
2771
* @since 2.5.0
72+
* @template I - Extends CommandType to enforce type safety for command modules
73+
*
74+
* @param {function} execute - Function to execute during command control flow
75+
* @param {CommandArgs<I>} execute.args - The command arguments array
76+
* @param {Context} execute.args[0] - The discord context (e.g., guild, channel, user info, interaction)
77+
* @param {SDT} execute.args[1] - The State, Dependencies, Params, Module, and Type object
78+
*
79+
* @returns {Plugin} A plugin that runs during command execution flow
80+
*
81+
* @example
82+
* // Plugin to restrict command to specific guild
83+
* export const inGuild = (guildId: string) => {
84+
* return CommandControlPlugin((ctx, sdt) => {
85+
* if(ctx.guild.id !== guildId) {
86+
* return controller.stop();
87+
* }
88+
* return controller.next();
89+
* });
90+
* };
91+
*
92+
* @example
93+
* // Plugins passing state through the chain
94+
* const plugin1 = CommandControlPlugin((ctx, sdt) => {
95+
* return controller.next({ 'plugin1/data': 'from plugin1' });
96+
* });
97+
*
98+
* const plugin2 = CommandControlPlugin((ctx, sdt) => {
99+
* return controller.next({ 'plugin2/data': ctx.user.id });
100+
* });
101+
*
102+
* export default commandModule({
103+
* type: CommandType.Slash,
104+
* plugins: [plugin1, plugin2],
105+
* execute: (ctx, sdt) => {
106+
* console.log(sdt.state); // Access accumulated state
107+
* }
108+
* });
109+
*
110+
* @remarks
111+
* - Control plugins are executed in order when a discord.js event is emitted
112+
* - Use controller.next() to continue to next plugin or controller.stop() to halt execution
113+
* - State can be passed between plugins using controller.next({ key: value })
114+
* - State keys should be namespaced to avoid collisions (e.g., 'plugin-name/key')
115+
* - Final accumulated state is passed to the command's execute function
116+
* - All plugins must succeed for the command to execute
117+
* - Plugins have access to dependencies through the sdt.deps object
118+
* - Useful for implementing preconditions, filters, and command preprocessing
28119
*/
29120
export function CommandControlPlugin<I extends CommandType>(
30121
execute: (...args: CommandArgs<I>) => PluginResult,

0 commit comments

Comments
 (0)