i think cron works

This commit is contained in:
Jacob Nguyen
2024-05-15 16:42:21 -05:00
parent 0d82658fc5
commit d905f08993
10 changed files with 49 additions and 14 deletions

View File

@@ -1,2 +0,0 @@
import type { Result } from 'ts-results-es'
export type VoidResult = Result<void, void>;

View File

@@ -22,7 +22,6 @@ export interface Emitter {
addListener(eventName: string | symbol, listener: AnyFunction): this; addListener(eventName: string | symbol, listener: AnyFunction): this;
removeListener(eventName: string | symbol, listener: AnyFunction): this; removeListener(eventName: string | symbol, listener: AnyFunction): this;
emit(eventName: string | symbol, ...payload: any[]): boolean; emit(eventName: string | symbol, ...payload: any[]): boolean;
on(eventName: string | symbol, listener: AnyFunction): this
} }

View File

@@ -1,5 +1,3 @@
import type { UnpackedDependencies } from '../../types/utility';
/** /**
* A semi-generic container that provides error handling, emitter, and module store. * 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 * For the handler to operate correctly, The only user provided dependency needs to be @sern/client

View File

@@ -18,8 +18,8 @@ import {
import type { Emitter, ErrorHandling, Logging } from './interfaces'; import type { Emitter, ErrorHandling, Logging } from './interfaces';
import util from 'node:util'; import util from 'node:util';
import type { PluginResult } from '../types/core-plugin'; import type { PluginResult } from '../types/core-plugin';
import type { VoidResult } from './_internal';
import { Result } from 'ts-results-es'; import { Result } from 'ts-results-es';
import { VoidResult } from '../types/utility';
/** /**
* if {src} is true, mapTo V, else ignore * if {src} is true, mapTo V, else ignore
* @param item * @param item

View File

@@ -1,4 +1,9 @@
import type { LogPayload, Logging, ErrorHandling } from '../interfaces'; 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 * @internal
* @since 2.0.0 * @since 2.0.0
@@ -41,3 +46,39 @@ export class DefaultLogging implements Logging {
console.warn(`WARN: ${this.date().toISOString()} -> ${payload.message}`); console.warn(`WARN: ${this.date().toISOString()} -> ${payload.message}`);
} }
} }
export class Cron extends EventEmitter {
tasks: string[] = [];
modules: Map<string, CronEventCommand> = 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;
}
}

View File

@@ -13,14 +13,11 @@ import {
finalize, finalize,
pipe pipe
} from 'rxjs'; } from 'rxjs';
import {
type VoidResult,
} from '../core/_internal';
import * as Id from '../core/id' import * as Id from '../core/id'
import type { Emitter, ErrorHandling, Logging } from '../core/interfaces'; import type { Emitter, ErrorHandling, Logging } from '../core/interfaces';
import { PayloadType, SernError } from '../core/structures/enums' import { PayloadType, SernError } from '../core/structures/enums'
import { Err, Ok, Result } from 'ts-results-es'; 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 { ControlPlugin } from '../types/core-plugin';
import type { CommandModule, Module, Processed } from '../types/core-modules'; import type { CommandModule, Module, Processed } from '../types/core-modules';
import { EventEmitter } from 'node:events'; import { EventEmitter } from 'node:events';

View File

@@ -1,6 +1,6 @@
import { EventType, PayloadType, SernError } from '../core/structures/enums'; import { EventType, PayloadType, SernError } from '../core/structures/enums';
import { eventDispatcher, handleCrash } from './event-utils' 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 * as Files from '../core/module-loading'
import type { UnpackedDependencies } from '../types/utility'; import type { UnpackedDependencies } from '../types/utility';
import { resultPayload } from '../core/functions'; import { resultPayload } from '../core/functions';

View File

@@ -51,6 +51,7 @@ export interface ExternalEventCommand extends Module {
} }
export interface CronEventCommand extends Module { export interface CronEventCommand extends Module {
name?: string; name?: string;
pattern: string;
type: EventType.Cron; type: EventType.Cron;
execute(...args: unknown[]): Awaitable<unknown>; execute(...args: unknown[]): Awaitable<unknown>;
} }

View File

@@ -33,7 +33,7 @@ import type {
TextCommand, TextCommand,
UserSelectCommand, UserSelectCommand,
} from './core-modules'; } 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 { CommandType, EventType, PluginType } from '../core/structures/enums'
import type { Context } from '../core/structures/context' import type { Context } from '../core/structures/context'
import type { import type {
@@ -48,7 +48,6 @@ import type {
UserContextMenuCommandInteraction, UserContextMenuCommandInteraction,
UserSelectMenuInteraction, UserSelectMenuInteraction,
} from 'discord.js'; } from 'discord.js';
import { VoidResult } from '../core/_internal';
export type PluginResult = Awaitable<VoidResult>; export type PluginResult = Awaitable<VoidResult>;

View File

@@ -1,10 +1,12 @@
import type { CommandInteractionOptionResolver, InteractionReplyOptions, MessageReplyOptions } from 'discord.js'; import type { CommandInteractionOptionResolver, InteractionReplyOptions, MessageReplyOptions } from 'discord.js';
import type { PayloadType } from '../core/structures/enums'; import type { PayloadType } from '../core/structures/enums';
import type { Module } from './core-modules'; import type { Module } from './core-modules';
import type { Result } from 'ts-results-es';
export type Awaitable<T> = PromiseLike<T> | T; export type Awaitable<T> = PromiseLike<T> | T;
export type AnyFunction = (...args: never[]) => unknown; export type VoidResult = Result<void, void>;
export type AnyFunction = (...args: any[]) => unknown;
// Thanks to @kelsny // Thanks to @kelsny
type ParseType<T> = { type ParseType<T> = {