mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
Remove module store, manager, and Intializable type
This commit is contained in:
@@ -5,7 +5,6 @@ export * from './operators';
|
||||
export * as Files from './module-loading';
|
||||
export * from './functions';
|
||||
export { SernError } from './structures/enums';
|
||||
export { ModuleStore } from './structures/module-store';
|
||||
export * as __Services from './structures/services';
|
||||
export { useContainerRaw } from './ioc/base';
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
export * from './error-handling';
|
||||
export * from './logging';
|
||||
export * from './module-manager';
|
||||
export * from './module-store';
|
||||
export * from './hooks';
|
||||
export * from './emitter';
|
||||
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
import type {
|
||||
CommandMeta,
|
||||
CommandModule,
|
||||
CommandModuleDefs,
|
||||
Module,
|
||||
} from '../../types/core-modules';
|
||||
import { CommandType } from '../structures';
|
||||
|
||||
interface MetadataAccess {
|
||||
getMetadata(m: Module): CommandMeta | undefined;
|
||||
setMetadata(m: Module, c: CommandMeta): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.0.0
|
||||
* @internal - direct access to the module manager will be removed in version 4
|
||||
*/
|
||||
export interface ModuleManager extends MetadataAccess {
|
||||
get(id: string): Module | undefined;
|
||||
|
||||
set(id: string, path: Module): void;
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
getPublishableCommands(): CommandModule[];
|
||||
|
||||
/*
|
||||
* @deprecated
|
||||
*/
|
||||
getByNameCommandType<T extends CommandType>(
|
||||
name: string,
|
||||
commandType: T,
|
||||
): CommandModuleDefs[T] | undefined;
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
import type { CommandMeta, Module } from '../../types/core-modules';
|
||||
|
||||
/**
|
||||
* Represents a core module store that stores IDs mapped to file paths.
|
||||
*/
|
||||
export interface CoreModuleStore {
|
||||
commands: Map<string, Module>;
|
||||
metadata: WeakMap<Module, CommandMeta>;
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { Container } from 'iti';
|
||||
import { Disposable } from '../';
|
||||
import * as assert from 'node:assert';
|
||||
import { Subject } from 'rxjs';
|
||||
import { __Services, ModuleStore } from '../_internal';
|
||||
import { __Services } from '../_internal';
|
||||
import * as Hooks from './hooks';
|
||||
import { EventEmitter } from 'node:events';
|
||||
|
||||
@@ -24,11 +24,7 @@ export class CoreContainer<T extends Partial<Dependencies>> extends Container<T,
|
||||
|
||||
(this as Container<{}, {}>)
|
||||
.add({ '@sern/errors': () => new __Services.DefaultErrorHandling,
|
||||
'@sern/emitter': () => new EventEmitter({ captureRejections: true }),
|
||||
'@sern/store': () => new ModuleStore })
|
||||
.add(ctx => {
|
||||
return { '@sern/modules': new __Services.DefaultModuleManager(ctx['@sern/store'])};
|
||||
});
|
||||
'@sern/emitter': () => new EventEmitter({ captureRejections: true }) })
|
||||
}
|
||||
|
||||
isReady() {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
export { CommandType, PluginType, PayloadType, EventType } from './enums';
|
||||
export * from './context';
|
||||
export * from './services';
|
||||
export * from './module-store';
|
||||
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { CommandMeta, Module } from '../../types/core-modules';
|
||||
|
||||
/*
|
||||
* @deprecated
|
||||
* Version 4.0.0 will internalize this api. Please refrain from using ModuleStore!
|
||||
* For interacting with modules, use the ModuleManager instead.
|
||||
*/
|
||||
export class ModuleStore {
|
||||
metadata = new WeakMap<Module, CommandMeta>();
|
||||
commands = new Map<string, Module>();
|
||||
}
|
||||
@@ -1,3 +1,2 @@
|
||||
export * from './error-handling';
|
||||
export * from './logger';
|
||||
export * from './module-manager';
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
import * as Id from '../../../core/id';
|
||||
import { CoreModuleStore, ModuleManager } from '../../contracts';
|
||||
import { CommandMeta, CommandModule, CommandModuleDefs, Module } from '../../../types/core-modules';
|
||||
import { CommandType } from '../enums';
|
||||
/**
|
||||
* @internal
|
||||
* @since 2.0.0
|
||||
* Version 4.0.0 will internalize this api. Please refrain from using DefaultModuleManager!
|
||||
*/
|
||||
export class DefaultModuleManager implements ModuleManager {
|
||||
constructor(private moduleStore: CoreModuleStore) {}
|
||||
|
||||
|
||||
getByNameCommandType<T extends CommandType>(name: string, commandType: T) {
|
||||
const module = this.get(Id.create(name, commandType));
|
||||
if (!module) {
|
||||
return undefined;
|
||||
}
|
||||
return module as CommandModuleDefs[T];
|
||||
}
|
||||
|
||||
setMetadata(m: Module, c: CommandMeta): void {
|
||||
this.moduleStore.metadata.set(m, c);
|
||||
}
|
||||
|
||||
getMetadata(m: Module): CommandMeta {
|
||||
const maybeModule = this.moduleStore.metadata.get(m);
|
||||
if (!maybeModule) {
|
||||
throw Error('Could not find metadata in store for ' + m);
|
||||
}
|
||||
return maybeModule;
|
||||
}
|
||||
|
||||
get(id: string) {
|
||||
return this.moduleStore.commands.get(id);
|
||||
}
|
||||
set(id: string, path: CommandModule): void {
|
||||
this.moduleStore.commands.set(id, path);
|
||||
}
|
||||
//not tested
|
||||
getPublishableCommands(): CommandModule[] {
|
||||
const entries = this.moduleStore.commands.entries();
|
||||
const publishable = 0b000000110;
|
||||
return Array.from(entries)
|
||||
.filter(([id]) => {
|
||||
const last_entry = id.at(-1);
|
||||
return last_entry == 'B' || !(publishable & Number.parseInt(last_entry!));
|
||||
})
|
||||
.map(([, path]) => path as CommandModule);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import {
|
||||
tap,
|
||||
catchError,
|
||||
finalize,
|
||||
map,
|
||||
} from 'rxjs';
|
||||
import {
|
||||
Files,
|
||||
@@ -23,7 +22,7 @@ import {
|
||||
VoidResult,
|
||||
resultPayload,
|
||||
} from '../core/_internal';
|
||||
import { Emitter, ErrorHandling, Logging, ModuleManager, PayloadType } from '../core';
|
||||
import { Emitter, ErrorHandling, Logging, PayloadType } from '../core';
|
||||
import { contextArgs, createDispatcher } from './dispatchers';
|
||||
import { ObservableInput, pipe } from 'rxjs';
|
||||
import { Err, Ok, Result } from 'ts-results-es';
|
||||
@@ -65,7 +64,7 @@ export function fmt(msg: string, prefix: string): string[] {
|
||||
*/
|
||||
export function createInteractionHandler<T extends Interaction>(
|
||||
source: Observable<Interaction>,
|
||||
mg: ModuleManager,
|
||||
mg: any, //TODO
|
||||
) {
|
||||
return createGenericHandler<Interaction, T, Result<ReturnType<typeof createDispatcher>, void>>(
|
||||
source,
|
||||
@@ -86,7 +85,7 @@ export function createInteractionHandler<T extends Interaction>(
|
||||
export function createMessageHandler(
|
||||
source: Observable<Message>,
|
||||
defaultPrefix: string,
|
||||
mg: ModuleManager,
|
||||
mg: any, //TODO
|
||||
) {
|
||||
return createGenericHandler(source, async event => {
|
||||
const [prefix, ...rest] = fmt(event.content, defaultPrefix);
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
import { ObservableInput, concat, first, fromEvent, ignoreElements, pipe, tap } from 'rxjs';
|
||||
import { SernError, _Module } from '../core/_internal';
|
||||
import { Result } from 'ts-results-es';
|
||||
import { Logging, ModuleManager } from '../core/contracts';
|
||||
import { buildModules, callInitPlugins } from './_internal';
|
||||
import * as assert from 'node:assert';
|
||||
import * as util from 'node:util';
|
||||
import { _Module } from '../core/_internal';
|
||||
import { Logging, } from '../core/contracts';
|
||||
import type { DependencyList } from '../types/ioc';
|
||||
import type { AnyModule, CommandMeta, Processed } from '../types/core-modules';
|
||||
|
||||
export function readyHandler(
|
||||
[sEmitter, , log , moduleManager, client]: DependencyList,
|
||||
[sEmitter, , log ,, client]: DependencyList,
|
||||
allPaths: ObservableInput<string>,
|
||||
) {
|
||||
//Todo: add module manager on on ready
|
||||
|
||||
35
src/sern.ts
35
src/sern.ts
@@ -29,36 +29,35 @@ export function init(maybeWrapper: Wrapper | 'file') {
|
||||
const dependencies = Services('@sern/emitter',
|
||||
'@sern/errors',
|
||||
'@sern/logger',
|
||||
'@sern/modules',
|
||||
'@sern/client');
|
||||
const logger = dependencies[2],
|
||||
errorHandler = dependencies[1];
|
||||
|
||||
const wrapper = Files.loadConfig(maybeWrapper, logger);
|
||||
if (wrapper.events !== undefined) {
|
||||
eventsHandler(dependencies, Files.getFullPathTree(wrapper.events));
|
||||
//eventsHandler(dependencies, Files.getFullPathTree(wrapper.events));
|
||||
}
|
||||
|
||||
const initCallsite = callsites()[1].getFileName();
|
||||
const presencePath = Files.shouldHandle(initCallsite!, "presence");
|
||||
|
||||
//Ready event: load all modules and when finished, time should be taken and logged
|
||||
readyHandler(dependencies, Files.getFullPathTree(wrapper.commands))
|
||||
.add(() => {
|
||||
logger?.info({ message: "Client signaled ready, registering modules" });
|
||||
const time = ((performance.now() - startTime) / 1000).toFixed(2);
|
||||
dependencies[0].emit('modulesLoaded');
|
||||
logger?.info({ message: `sern: registered in ${time} s`, });
|
||||
if(presencePath.exists) {
|
||||
const setPresence = async (p: any) => {
|
||||
return (dependencies[4] as Client).user?.setPresence(p);
|
||||
}
|
||||
presenceHandler(presencePath.path, setPresence).subscribe();
|
||||
}
|
||||
});
|
||||
// readyHandler(dependencies, Files.getFullPathTree(wrapper.commands))
|
||||
// .add(() => {
|
||||
// logger?.info({ message: "Client signaled ready, registering modules" });
|
||||
// const time = ((performance.now() - startTime) / 1000).toFixed(2);
|
||||
// dependencies[0].emit('modulesLoaded');
|
||||
// logger?.info({ message: `sern: registered in ${time} s`, });
|
||||
// if(presencePath.exists) {
|
||||
// const setPresence = async (p: any) => {
|
||||
// return (dependencies[4] as Client).user?.setPresence(p);
|
||||
// }
|
||||
// presenceHandler(presencePath.path, setPresence).subscribe();
|
||||
// }
|
||||
// });
|
||||
|
||||
const messages$ = messageHandler(dependencies, wrapper.defaultPrefix);
|
||||
const interactions$ = interactionHandler(dependencies);
|
||||
//const messages$ = messageHandler(dependencies, wrapper.defaultPrefix);
|
||||
//const interactions$ = interactionHandler(dependencies);
|
||||
// listening to the message stream and interaction stream
|
||||
merge(messages$, interactions$).pipe(handleCrash(errorHandler, dependencies[0], logger)).subscribe();
|
||||
//merge(messages$, interactions$).pipe(handleCrash(errorHandler, dependencies[0], logger)).subscribe();
|
||||
}
|
||||
|
||||
@@ -10,29 +10,18 @@ export type Singleton<T> = () => T;
|
||||
* Every time this is called, a new object is created
|
||||
*/
|
||||
export type Transient<T> = () => () => T;
|
||||
/**
|
||||
* Type to annotate that something is initializable.
|
||||
* If T has an init method, this will be called.
|
||||
*/
|
||||
export type Initializable<T extends Contracts.Init> = T
|
||||
|
||||
export type DependencyList = [
|
||||
Contracts.Emitter,
|
||||
Contracts.ErrorHandling,
|
||||
Contracts.Logging | undefined,
|
||||
Contracts.ModuleManager,
|
||||
null,
|
||||
Contracts.Emitter,
|
||||
];
|
||||
|
||||
export interface CoreDependencies {
|
||||
'@sern/client': () => Contracts.Emitter;
|
||||
'@sern/emitter': () => Contracts.Emitter;
|
||||
/**
|
||||
* @deprecated
|
||||
* Will be removed and turned internal
|
||||
*/
|
||||
'@sern/store': () => Contracts.CoreModuleStore;
|
||||
'@sern/modules': () => Contracts.ModuleManager;
|
||||
'@sern/errors': () => Contracts.ErrorHandling;
|
||||
'@sern/logger'?: () => Contracts.Logging;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user