Skip to content

Commit

Permalink
i think cron works
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed May 15, 2024
1 parent 0d82658 commit d905f08
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 14 deletions.
2 changes: 0 additions & 2 deletions src/core/_internal.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/core/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
2 changes: 0 additions & 2 deletions src/core/ioc/container.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions src/core/structures/default-services.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<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;
}
}
5 changes: 1 addition & 4 deletions src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/user-defined-events.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
1 change: 1 addition & 0 deletions src/types/core-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>;
}
Expand Down
3 changes: 1 addition & 2 deletions src/types/core-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -48,7 +48,6 @@ import type {
UserContextMenuCommandInteraction,
UserSelectMenuInteraction,
} from 'discord.js';
import { VoidResult } from '../core/_internal';

export type PluginResult = Awaitable<VoidResult>;

Expand Down
4 changes: 3 additions & 1 deletion src/types/utility.ts
Original file line number Diff line number Diff line change
@@ -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<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
type ParseType<T> = {
Expand Down

0 comments on commit d905f08

Please sign in to comment.