mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 09:26:54 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d429f3adbf | ||
|
|
5e011b471e | ||
|
|
41344608c6 |
@@ -1,5 +1,12 @@
|
||||
# Changelog
|
||||
|
||||
## [3.0.1](https://github.com/sern-handler/handler/compare/v3.0.0...v3.0.1) (2023-08-05)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* collectors ([4134460](https://github.com/sern-handler/handler/commit/41344608c677b6069c46412f5f16e4337182ca7d))
|
||||
|
||||
## [3.0.0](https://github.com/sern-handler/handler/compare/v2.6.3...v3.0.0) (2023-07-29)
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@sern/handler",
|
||||
"packageManager": "yarn@3.5.0",
|
||||
"version": "3.0.0",
|
||||
"version": "3.0.1",
|
||||
"description": "A complete, customizable, typesafe, & reactive framework for discord bots.",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/cjs/index.cjs",
|
||||
@@ -40,7 +40,7 @@
|
||||
"dependencies": {
|
||||
"iti": "^0.6.0",
|
||||
"rxjs": "^7.8.0",
|
||||
"ts-results-es": "^3.6.0"
|
||||
"ts-results-es": "latest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@faker-js/faker": "^8.0.1",
|
||||
|
||||
@@ -80,11 +80,6 @@ export async function composeRoot(
|
||||
}
|
||||
|
||||
export function useContainer<const T extends Dependencies>() {
|
||||
console.warn(`
|
||||
Warning: using a container hook (useContainer) is not recommended.
|
||||
Could lead to many unwanted side effects.
|
||||
Use the new Service(s) api function instead.
|
||||
`);
|
||||
return <V extends (keyof T)[]>(...keys: [...V]) =>
|
||||
keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as IntoDependencies<V>;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
import { Emitter, ErrorHandling, Logging } from './contracts';
|
||||
import util from 'node:util';
|
||||
import type { PluginResult, VoidResult } from '../types/core-plugin';
|
||||
import type { Result } from 'ts-results-es'
|
||||
/**
|
||||
* if {src} is true, mapTo V, else ignore
|
||||
* @param item
|
||||
@@ -69,3 +70,17 @@ export function handleError<C>(crashHandler: ErrorHandling, logging?: Logging) {
|
||||
return caught;
|
||||
};
|
||||
}
|
||||
// Temporary until i get rxjs operators working on ts-results-es
|
||||
export const filterTap = <K, R>(onErr: (e: R) => void): OperatorFunction<Result<K, R>, K> =>
|
||||
pipe(
|
||||
concatMap(result => {
|
||||
if(result.ok) {
|
||||
return of(result.val)
|
||||
}
|
||||
onErr(result.val);
|
||||
return EMPTY
|
||||
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -26,9 +26,8 @@ import { Emitter, ErrorHandling, Logging, ModuleManager, useContainerRaw } from
|
||||
import { contextArgs, createDispatcher, dispatchMessage } from './dispatchers';
|
||||
import { ObservableInput, pipe } from 'rxjs';
|
||||
import { SernEmitter } from '../core';
|
||||
import { Result } from 'ts-results-es';
|
||||
import { Err, Ok, Result } from 'ts-results-es';
|
||||
import type { Awaitable } from '../types/utility';
|
||||
import assert from 'node:assert';
|
||||
import type { ControlPlugin } from '../types/core-plugin';
|
||||
import type { AnyModule, CommandModule, Module, Processed } from '../types/core-modules';
|
||||
import type { ImportPayload } from '../types/core';
|
||||
@@ -37,7 +36,10 @@ function createGenericHandler<Source, Narrowed extends Source, Output>(
|
||||
source: Observable<Source>,
|
||||
makeModule: (event: Narrowed) => Promise<Output>,
|
||||
) {
|
||||
return (pred: (i: Source) => i is Narrowed) => source.pipe(filter(pred), concatMap(makeModule));
|
||||
return (pred: (i: Source) => i is Narrowed) =>
|
||||
source.pipe(
|
||||
filter(pred),
|
||||
concatMap(makeModule));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,14 +67,18 @@ export function createInteractionHandler<T extends Interaction>(
|
||||
source: Observable<Interaction>,
|
||||
mg: ModuleManager,
|
||||
) {
|
||||
return createGenericHandler<Interaction, T, ReturnType<typeof createDispatcher>>(
|
||||
return createGenericHandler<Interaction, T, Result<ReturnType<typeof createDispatcher>, void>>(
|
||||
source,
|
||||
async event => {
|
||||
const fullPath = mg.get(Id.reconstruct(event));
|
||||
assert(fullPath, SernError.UndefinedModule + ' No full path found in module store');
|
||||
return Files.defaultModuleLoader<Processed<CommandModule>>(fullPath).then(payload =>
|
||||
createDispatcher({ module: payload.module, event }),
|
||||
);
|
||||
if(!fullPath) {
|
||||
return Err.EMPTY
|
||||
}
|
||||
return Files
|
||||
.defaultModuleLoader<Processed<CommandModule>>(fullPath)
|
||||
.then(payload =>
|
||||
Ok(createDispatcher({ module: payload.module, event }))
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -86,11 +92,15 @@ export function createMessageHandler(
|
||||
const [prefix, ...rest] = fmt(event.content, defaultPrefix);
|
||||
const fullPath = mg.get(`${prefix}_A1`);
|
||||
|
||||
assert(fullPath, SernError.UndefinedModule + ' No full path found in module store');
|
||||
return Files.defaultModuleLoader<Processed<CommandModule>>(fullPath).then(payload => {
|
||||
const args = contextArgs(event, rest);
|
||||
return dispatchMessage(payload.module, args);
|
||||
});
|
||||
if(!fullPath) {
|
||||
return Err('Possibly undefined behavior: could not find a static id to resolve ')
|
||||
}
|
||||
return Files
|
||||
.defaultModuleLoader<Processed<CommandModule>>(fullPath)
|
||||
.then(payload => {
|
||||
const args = contextArgs(event, rest);
|
||||
return Ok(dispatchMessage(payload.module, args));
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
isModal,
|
||||
sharedEventStream,
|
||||
SernError,
|
||||
filterTap,
|
||||
} from '../core/_internal';
|
||||
import { createInteractionHandler, executeModule, makeModuleExecutor } from './_internal';
|
||||
import type { DependencyList } from '../types/ioc';
|
||||
@@ -22,10 +23,11 @@ export function interactionHandler([emitter, , , modules, client]: DependencyLis
|
||||
handle(isCommand),
|
||||
handle(isModal),
|
||||
);
|
||||
return interactionHandler$.pipe(
|
||||
makeModuleExecutor(module => {
|
||||
emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure));
|
||||
}),
|
||||
concatMap(payload => executeModule(emitter, payload)),
|
||||
return interactionHandler$
|
||||
.pipe(
|
||||
filterTap(e => emitter.emit('warning', SernEmitter.warning(e))),
|
||||
makeModuleExecutor(module =>
|
||||
emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure))),
|
||||
concatMap(payload => executeModule(emitter, payload)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { concatMap, EMPTY } from 'rxjs';
|
||||
import type { Message } from 'discord.js';
|
||||
import { SernEmitter } from '../core';
|
||||
import { sharedEventStream, SernError } from '../core/_internal';
|
||||
import { sharedEventStream, SernError, filterTap } from '../core/_internal';
|
||||
import { createMessageHandler, executeModule, makeModuleExecutor } from './_internal';
|
||||
import type { DependencyList } from '../types/ioc';
|
||||
|
||||
@@ -38,6 +38,7 @@ export function messageHandler(
|
||||
const msgCommands$ = handle(isNonBot(defaultPrefix));
|
||||
|
||||
return msgCommands$.pipe(
|
||||
filterTap((e) => emitter.emit('warning', SernEmitter.warning(e))),
|
||||
makeModuleExecutor(module => {
|
||||
emitter.emit('module.activate', SernEmitter.failure(module, SernError.PluginFailure));
|
||||
}),
|
||||
|
||||
10
yarn.lock
10
yarn.lock
@@ -619,7 +619,7 @@ __metadata:
|
||||
iti: ^0.6.0
|
||||
prettier: 2.8.8
|
||||
rxjs: ^7.8.0
|
||||
ts-results-es: ^3.6.0
|
||||
ts-results-es: latest
|
||||
tsup: ^6.7.0
|
||||
typescript: 5.0.2
|
||||
vitest: latest
|
||||
@@ -3844,10 +3844,10 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"ts-results-es@npm:^3.6.0":
|
||||
version: 3.6.0
|
||||
resolution: "ts-results-es@npm:3.6.0"
|
||||
checksum: 28593545cad764efce2de64b09037f855c3bbd499e3f5e2683863c86abc9b4453e5aa7d651edff93d42340a2c24f8bbabcf92467fcec767e09bbeeac01e97396
|
||||
"ts-results-es@npm:latest":
|
||||
version: 3.6.1
|
||||
resolution: "ts-results-es@npm:3.6.1"
|
||||
checksum: af0d93ee4d3bd9e99a5fd4ac4b0ad090aef0a61e1f38ee596cfebe8d47090b34a2557d3778e00b4aae7c74962133805275ffffe56716e4d747fa559a926d9ced
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
|
||||
Reference in New Issue
Block a user