Skip to content

Commit

Permalink
refactor: rm redudant fns and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Nov 4, 2023
1 parent 03439fe commit 132b625
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
31 changes: 8 additions & 23 deletions src/handlers/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,6 @@ import { CommandType, Context } from '../core';
import type { Args } from '../types/utility';
import type { BothCommand, CommandModule, Module, Processed } from '../types/core-modules';

function dispatchInteraction<T extends CommandModule, V extends BaseInteraction | Message>(
payload: { module: Processed<T>; event: V },
createArgs: (m: typeof payload.event) => unknown[],
) {
return {
module: payload.module,
args: createArgs(payload.event),
};
}
//TODO: refactor dispatchers so that it implements a strategy for each different type of payload?
export function dispatchMessage(module: Processed<CommandModule>, args: [Context, Args]) {
return {
module,
args,
};
}

function dispatchAutocomplete(payload: {
module: Processed<BothCommand>;
event: AutocompleteInteraction;
Expand All @@ -52,9 +35,6 @@ export function contextArgs(wrappable: Message | BaseInteraction, messageArgs?:
return [ctx, args] as [Context, Args];
}

function interactionArg<T extends BaseInteraction>(interaction: T) {
return [interaction] as [T];
}

function intoPayload(module: Processed<Module>) {
return pipe(
Expand Down Expand Up @@ -109,9 +89,14 @@ export function createDispatcher(payload: {
*/
return dispatchAutocomplete(payload as never);
}
return dispatchInteraction(payload, contextArgs);
return {
module: payload.module,
args: contextArgs(payload.event),
};
}
default:
return dispatchInteraction(payload, interactionArg);
default: return {
module: payload.module,
args: [payload.event],
};
}
}
13 changes: 7 additions & 6 deletions src/handlers/event-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
useContainerRaw,
} from '../core/_internal';
import { Emitter, ErrorHandling, Logging, ModuleManager } from '../core';
import { contextArgs, createDispatcher, dispatchMessage } from './dispatchers';
import { contextArgs, createDispatcher } from './dispatchers';
import { ObservableInput, pipe } from 'rxjs';
import { SernEmitter } from '../core';
import { Err, Ok, Result } from 'ts-results-es';
Expand Down Expand Up @@ -78,8 +78,7 @@ export function createInteractionHandler<T extends Interaction>(
return Files
.defaultModuleLoader<Processed<CommandModule>>(fullPath)
.then(payload =>
Ok(createDispatcher({ module: payload.module, event }))
);
Ok(createDispatcher({ module: payload.module, event })));
},
);
}
Expand All @@ -98,9 +97,9 @@ export function createMessageHandler(
}
return Files
.defaultModuleLoader<Processed<CommandModule>>(fullPath)
.then(payload => {
.then(({ module })=> {
const args = contextArgs(event, rest);
return Ok(dispatchMessage(payload.module, args));
return Ok({ module, args });
});
});
}
Expand All @@ -126,7 +125,9 @@ export function buildModules<T extends AnyModule>(
input: ObservableInput<string>,
moduleManager: ModuleManager,
) {
return Files.buildModuleStream<Processed<T>>(input).pipe(assignDefaults(moduleManager));
return Files
.buildModuleStream<Processed<T>>(input)
.pipe(assignDefaults(moduleManager));
}

/**
Expand Down
16 changes: 7 additions & 9 deletions src/handlers/user-defined-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ export function eventsHandler(
}
};
buildModules<EventModule>(allPaths, moduleManager)
.pipe(
callInitPlugins(emitter),
map(intoDispatcher),
/**
* Where all events are turned on
*/
mergeAll(),
handleCrash(err, log),
)
.pipe(callInitPlugins(emitter),
map(intoDispatcher),
/**
* Where all events are turned on
*/
mergeAll(),
handleCrash(err, log))
.subscribe();
}

0 comments on commit 132b625

Please sign in to comment.