Skip to content

Commit

Permalink
cron works now, poc
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed May 15, 2024
1 parent d905f08 commit 203e8c8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/core/ioc/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async function composeRoot(
__add_container('@sern/logger', new __Services.DefaultLogging());
}
__add_container('@sern/errors', new __Services.DefaultErrorHandling());
__add_container('@sern/cron', {})
__add_container('@sern/cron', new __Services.Cron())
__add_container('@sern/modules', new Map())
__add_container('@sern/emitter', new EventEmitter())
//Build the container based on the callback provided by the user
Expand All @@ -98,7 +98,7 @@ export async function makeDependencies (conf: ValidDependencyConfig) {
__add_container('@sern/logger', new __Services.DefaultLogging);
}
__add_container('@sern/errors', new __Services.DefaultErrorHandling());
__add_container('@sern/cron', {})
__add_container('@sern/cron', new __Services.Cron())
__add_container('@sern/modules', new Map())
__add_container('@sern/emitter', new EventEmitter())
await useContainerRaw().ready();
Expand Down
9 changes: 6 additions & 3 deletions src/core/structures/default-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ export class Cron extends EventEmitter {
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");
}

//@ts-ignore
if(!cron.validate(module.pattern)) {
throw Error("Invalid cron expression while adding")
}
this.modules.set(module.name!, module as CronEventCommand);
}
addListener(eventName: string | symbol, listener: AnyFunction): this {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const createResult = createResultResolver<
* @param module
* @param source
*/
export function eventDispatcher(module: Module, source: unknown) {
export function eventDispatcher(module: Module, source: unknown) {
assert.ok(source instanceof EventEmitter, `${source} is not an EventEmitter`);

const execute: OperatorFunction<unknown[], unknown> =
Expand Down
9 changes: 6 additions & 3 deletions src/handlers/user-defined-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ const intoDispatcher = (deps: UnpackedDependencies) =>
return eventDispatcher(module, deps['@sern/client']);
case EventType.External:
return eventDispatcher(module, deps[module.emitter]);
case EventType.Cron:
//@ts-ignore TODO
return eventDispatcher(module, deps['@sern/cron'])
case EventType.Cron: {
//@ts-ignore
const cron = deps['@sern/cron'];
cron.addCronModule(module);
return eventDispatcher(module, cron)
}
default:
throw Error(SernError.InvalidModuleType + ' while creating event handler');
}
Expand Down
5 changes: 2 additions & 3 deletions src/types/core-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ export interface ExternalEventCommand extends Module {
execute(...args: unknown[]): Awaitable<unknown>;
}
export interface CronEventCommand extends Module {
type: EventType.Cron;
name?: string;
pattern: string;
type: EventType.Cron;
execute(...args: unknown[]): Awaitable<unknown>;
}

Expand Down Expand Up @@ -168,8 +168,7 @@ export interface EventModuleDefs {
[EventType.Sern]: SernEventCommand;
[EventType.Discord]: DiscordEventCommand;
[EventType.External]: ExternalEventCommand;
//TODO
[EventType.Cron]: ExternalEventCommand;
[EventType.Cron]: CronEventCommand;
}

export interface SernAutocompleteData
Expand Down

0 comments on commit 203e8c8

Please sign in to comment.