-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
71 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,47 @@ | ||
import { EventType, SernError } from '../core/structures/enums'; | ||
import { eventDispatcher } from './event-utils' | ||
import type { EventModule, Processed } from '../types/core-modules'; | ||
import { EventType, PayloadType, SernError } from '../core/structures/enums'; | ||
import { eventDispatcher, handleCrash } from './event-utils' | ||
import { EventModule, Module, Processed } from '../types/core-modules'; | ||
import * as Files from '../core/module-loading' | ||
import type { UnpackedDependencies } from '../types/utility'; | ||
import { resultPayload } from '../core/functions'; | ||
import { from, map, mergeAll } from 'rxjs'; | ||
|
||
export default function(deps: UnpackedDependencies, eventDir: string) { | ||
//code smell | ||
const intoDispatcher = (e: { module: Processed<EventModule> }) => { | ||
switch (e.module.type) { | ||
const intoDispatcher = (deps: UnpackedDependencies) => | ||
(module : EventModule) => { | ||
switch (module.type) { | ||
case EventType.Sern: | ||
return eventDispatcher(e.module, deps['@sern/emitter']); | ||
return eventDispatcher(module, deps['@sern/emitter']); | ||
case EventType.Discord: | ||
return eventDispatcher(e.module, deps['@sern/client']); | ||
return eventDispatcher(module, deps['@sern/client']); | ||
case EventType.External: | ||
return eventDispatcher(e.module, deps[e.module.emitter]); | ||
return eventDispatcher(module, deps[module.emitter]); | ||
case EventType.Cron: | ||
//@ts-ignore TODO | ||
return eventDispatcher(e.module, deps['@sern/cron']) | ||
return eventDispatcher(module, deps['@sern/cron']) | ||
default: | ||
throw Error(SernError.InvalidModuleType + ' while creating event handler'); | ||
} | ||
}; | ||
Files.readRecursive(eventDir) | ||
//buildModules<EventModule>(allPaths) | ||
// pipe( | ||
// callInitPlugins(emitter), | ||
// map(intoDispatcher), | ||
// /** | ||
// * Where all events are turned on | ||
// */ | ||
// mergeAll(), | ||
// handleCrash(err, emitter, log)) | ||
// .subscribe(); | ||
}; | ||
|
||
export default async function(deps: UnpackedDependencies, eventDir: string) { | ||
const eventModules: EventModule[] = []; | ||
for await (const path of Files.readRecursive(eventDir)) { | ||
const { module } = await Files.importModule<Module>(path); | ||
for(const plugin of module.plugins) { | ||
const res = await plugin.execute({ module, absPath: module.meta.absPath }); | ||
if(res.isErr()) { | ||
deps['@sern/emitter'].emit('module.register', resultPayload(PayloadType.Failure, module, SernError.PluginFailure)); | ||
throw Error("Plugin failed with controller.stop()"); | ||
} | ||
} | ||
eventModules.push(module as EventModule); | ||
} | ||
from(eventModules) | ||
.pipe(map(intoDispatcher(deps)), | ||
/** | ||
* Where all events are turned on | ||
*/ | ||
mergeAll(), | ||
handleCrash(deps)) | ||
.subscribe(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters