From d905f08993beb96a439d498d1d8f181b381086ce Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Wed, 15 May 2024 16:42:21 -0500 Subject: [PATCH] i think cron works --- src/core/_internal.ts | 2 -- src/core/interfaces.ts | 1 - src/core/ioc/container.ts | 2 -- src/core/operators.ts | 2 +- src/core/structures/default-services.ts | 41 +++++++++++++++++++++++++ src/handlers/event-utils.ts | 5 +-- src/handlers/user-defined-events.ts | 2 +- src/types/core-modules.ts | 1 + src/types/core-plugin.ts | 3 +- src/types/utility.ts | 4 ++- 10 files changed, 49 insertions(+), 14 deletions(-) delete mode 100644 src/core/_internal.ts diff --git a/src/core/_internal.ts b/src/core/_internal.ts deleted file mode 100644 index da0a304f..00000000 --- a/src/core/_internal.ts +++ /dev/null @@ -1,2 +0,0 @@ -import type { Result } from 'ts-results-es' -export type VoidResult = Result; diff --git a/src/core/interfaces.ts b/src/core/interfaces.ts index 6e02d301..cf682da4 100644 --- a/src/core/interfaces.ts +++ b/src/core/interfaces.ts @@ -22,7 +22,6 @@ export interface Emitter { addListener(eventName: string | symbol, listener: AnyFunction): this; removeListener(eventName: string | symbol, listener: AnyFunction): this; emit(eventName: string | symbol, ...payload: any[]): boolean; - on(eventName: string | symbol, listener: AnyFunction): this } diff --git a/src/core/ioc/container.ts b/src/core/ioc/container.ts index 92aa4f53..8a41e705 100644 --- a/src/core/ioc/container.ts +++ b/src/core/ioc/container.ts @@ -1,5 +1,3 @@ -import type { UnpackedDependencies } from '../../types/utility'; - /** * A semi-generic container that provides error handling, emitter, and module store. * For the handler to operate correctly, The only user provided dependency needs to be @sern/client diff --git a/src/core/operators.ts b/src/core/operators.ts index a02fb249..6fdbaf93 100644 --- a/src/core/operators.ts +++ b/src/core/operators.ts @@ -18,8 +18,8 @@ import { import type { Emitter, ErrorHandling, Logging } from './interfaces'; import util from 'node:util'; import type { PluginResult } from '../types/core-plugin'; -import type { VoidResult } from './_internal'; import { Result } from 'ts-results-es'; +import { VoidResult } from '../types/utility'; /** * if {src} is true, mapTo V, else ignore * @param item diff --git a/src/core/structures/default-services.ts b/src/core/structures/default-services.ts index 2bcd41d2..75001ee2 100644 --- a/src/core/structures/default-services.ts +++ b/src/core/structures/default-services.ts @@ -1,4 +1,9 @@ import type { LogPayload, Logging, ErrorHandling } from '../interfaces'; +import { AnyFunction } from '../../types/utility'; +import cron from 'node-cron' +import { EventEmitter } from 'events'; +import type { CronEventCommand, Module } from '../../types/core-modules' +import { EventType } from './enums'; /** * @internal * @since 2.0.0 @@ -41,3 +46,39 @@ export class DefaultLogging implements Logging { console.warn(`WARN: ${this.date().toISOString()} -> ${payload.message}`); } } + +export class Cron extends EventEmitter { + tasks: string[] = []; + modules: Map = new Map(); + private sanityCheck(eventName: string | symbol) : asserts eventName is string { + if(typeof eventName === 'symbol') throw Error("Cron cannot add symbol based listener") + if(!cron.validate(eventName)) { + throw Error("Invalid cron expression while adding") + } + } + addCronModule(module: Module) { + if(module.type !== EventType.Cron) { + throw Error("Can only add cron modules"); + } + this.modules.set(module.name!, module as CronEventCommand); + } + addListener(eventName: string | symbol, listener: AnyFunction): this { + this.sanityCheck(eventName); + const retrievedModule = this.modules.get(eventName); + if(!retrievedModule) throw Error("Adding task: module " +eventName +"was not found"); + cron.schedule(retrievedModule.pattern, listener, { + name: retrievedModule?.name! + }); + return this; + } + removeListener(eventName: string | symbol, listener: AnyFunction) { + this.sanityCheck(eventName); + const retrievedModule = this.modules.get(eventName); + if(!retrievedModule) throw Error("Removing cron: module " +eventName +"was not found"); + const task= cron.getTasks().get(retrievedModule.name!) + if(!task) throw Error("Finding cron task with"+ retrievedModule.name + " not found"); + task.stop(); + super.removeListener(eventName, listener); + return this; + } +} diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index ba50a581..dc52eb90 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -13,14 +13,11 @@ import { finalize, pipe } from 'rxjs'; -import { - type VoidResult, -} from '../core/_internal'; import * as Id from '../core/id' import type { Emitter, ErrorHandling, Logging } from '../core/interfaces'; import { PayloadType, SernError } from '../core/structures/enums' import { Err, Ok, Result } from 'ts-results-es'; -import type { Awaitable, UnpackedDependencies } from '../types/utility'; +import type { Awaitable, UnpackedDependencies, VoidResult } from '../types/utility'; import type { ControlPlugin } from '../types/core-plugin'; import type { CommandModule, Module, Processed } from '../types/core-modules'; import { EventEmitter } from 'node:events'; diff --git a/src/handlers/user-defined-events.ts b/src/handlers/user-defined-events.ts index 627c2ab9..572b0d17 100644 --- a/src/handlers/user-defined-events.ts +++ b/src/handlers/user-defined-events.ts @@ -1,6 +1,6 @@ import { EventType, PayloadType, SernError } from '../core/structures/enums'; import { eventDispatcher, handleCrash } from './event-utils' -import { EventModule, Module, Processed } from '../types/core-modules'; +import { EventModule, Module } from '../types/core-modules'; import * as Files from '../core/module-loading' import type { UnpackedDependencies } from '../types/utility'; import { resultPayload } from '../core/functions'; diff --git a/src/types/core-modules.ts b/src/types/core-modules.ts index 98905736..bbb82e1e 100644 --- a/src/types/core-modules.ts +++ b/src/types/core-modules.ts @@ -51,6 +51,7 @@ export interface ExternalEventCommand extends Module { } export interface CronEventCommand extends Module { name?: string; + pattern: string; type: EventType.Cron; execute(...args: unknown[]): Awaitable; } diff --git a/src/types/core-plugin.ts b/src/types/core-plugin.ts index 331c9be2..abf36515 100644 --- a/src/types/core-plugin.ts +++ b/src/types/core-plugin.ts @@ -33,7 +33,7 @@ import type { TextCommand, UserSelectCommand, } from './core-modules'; -import type { Args, Awaitable, Payload, SlashOptions } from './utility'; +import type { Args, Awaitable, Payload, SlashOptions, VoidResult } from './utility'; import type { CommandType, EventType, PluginType } from '../core/structures/enums' import type { Context } from '../core/structures/context' import type { @@ -48,7 +48,6 @@ import type { UserContextMenuCommandInteraction, UserSelectMenuInteraction, } from 'discord.js'; -import { VoidResult } from '../core/_internal'; export type PluginResult = Awaitable; diff --git a/src/types/utility.ts b/src/types/utility.ts index d215ea16..ac959db7 100644 --- a/src/types/utility.ts +++ b/src/types/utility.ts @@ -1,10 +1,12 @@ import type { CommandInteractionOptionResolver, InteractionReplyOptions, MessageReplyOptions } from 'discord.js'; import type { PayloadType } from '../core/structures/enums'; import type { Module } from './core-modules'; +import type { Result } from 'ts-results-es'; export type Awaitable = PromiseLike | T; -export type AnyFunction = (...args: never[]) => unknown; +export type VoidResult = Result; +export type AnyFunction = (...args: any[]) => unknown; // Thanks to @kelsny type ParseType = {