mirror of
https://github.com/sern-handler/handler
synced 2026-06-26 09:42:15 +00:00
revert: readd module store and add contract
This commit is contained in:
@@ -18,5 +18,6 @@ export interface ErrorHandling {
|
||||
* @param error
|
||||
*/
|
||||
updateAlive(error: Error): void;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export { type ErrorHandling } from './error-handling';
|
||||
export type { Logging, LogPayload } from './logging';
|
||||
export { type ModuleManager } from './module-manager';
|
||||
export * from './error-handling';
|
||||
export * from './logging';
|
||||
export * from './module-manager';
|
||||
export * from './module-store'
|
||||
|
||||
4
src/core/contracts/module-store.ts
Normal file
4
src/core/contracts/module-store.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
export interface CoreModuleStore {
|
||||
commands : Map<string, string>;
|
||||
}
|
||||
@@ -2,44 +2,59 @@ import { Container } from "iti";
|
||||
import { DefaultErrorHandling, DefaultModuleManager, SernEmitter } from "../";
|
||||
import { isAsyncFunction} from "node:util/types";
|
||||
import * as assert from 'node:assert'
|
||||
import { Dependencies } from "../ioc/types";
|
||||
import { Subject } from "rxjs";
|
||||
import { ModuleStore } from "./module-store";
|
||||
|
||||
/**
|
||||
* Provides all the defaults for sern to function properly.
|
||||
* The only user provided dependency needs to be @sern/client
|
||||
*/
|
||||
export class CoreContainer<T extends Partial<Dependencies>> extends Container<T, {}> {
|
||||
private _ready = false;
|
||||
private ready$ = new Subject<never>();
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.listenForInsertions();
|
||||
|
||||
(this as Container<{}, {}>)
|
||||
.add({
|
||||
'@sern/errors': () => new DefaultErrorHandling(),
|
||||
'@sern/emitter': () => new SernEmitter(),
|
||||
'@sern/modules': () => new DefaultModuleManager(new Map())
|
||||
'@sern/store': () => new ModuleStore(),
|
||||
}).add(ctx => {
|
||||
return { '@sern/modules': () => new DefaultModuleManager(ctx["@sern/store"]) };
|
||||
})
|
||||
}
|
||||
|
||||
private listenForInsertions() {
|
||||
assert.ok(this.isReady(), "listening for init functions should only occur prior to sern being ready.")
|
||||
|
||||
async withInit<const Keys extends keyof Dependencies>(...keys: Keys[]) {
|
||||
if(this.isReady()) {
|
||||
throw Error("You cannot call this method after sern has started");
|
||||
const unsubscriber = this.on('containerUpserted', this.callInitHooks);
|
||||
this.ready$.subscribe({
|
||||
complete: unsubscriber
|
||||
});
|
||||
}
|
||||
|
||||
private async callInitHooks(e: { key: keyof T, newContainer: T[keyof T]|null }) {
|
||||
|
||||
const dep = e.newContainer;
|
||||
assert.ok(dep);
|
||||
|
||||
//Ignore any dependencies that are not objects or array
|
||||
if(typeof(dep) !== 'object' || Array.isArray(dep)) {
|
||||
return;
|
||||
}
|
||||
for await (const k of keys) {
|
||||
const dep = this.get(k);
|
||||
assert.ok(dep !== undefined);
|
||||
if('init' in dep && typeof dep.init === 'function') {
|
||||
isAsyncFunction(dep.init)
|
||||
if('init' in dep && typeof dep.init === 'function') {
|
||||
isAsyncFunction(dep.init)
|
||||
? await dep.init()
|
||||
: dep.init()
|
||||
} else {
|
||||
throw Error(`called withInit with key ${k} but found nothing to init`)
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
isReady() {
|
||||
return this._ready;
|
||||
return this.ready$.closed;
|
||||
}
|
||||
ready() {
|
||||
this._ready = true;
|
||||
this.ready$.unsubscribe();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ export abstract class CoreContext<M, I> {
|
||||
}
|
||||
|
||||
public isMessage(): this is CoreContext<M, never> {
|
||||
return this.ctx.map(() => true).unwrapOr(false);
|
||||
return this.ctx.ok;
|
||||
}
|
||||
|
||||
public isSlash(): this is CoreContext<never, I> {
|
||||
|
||||
@@ -2,3 +2,4 @@ export * from './enums';
|
||||
export * from './context';
|
||||
export * from './sern-emitter';
|
||||
export * from './services'
|
||||
export * from './module-store'
|
||||
|
||||
9
src/core/structures/module-store.ts
Normal file
9
src/core/structures/module-store.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { CoreModuleStore } from "../contracts";
|
||||
|
||||
/*
|
||||
* @internal
|
||||
* Version 4.0.0 will internalize this api. Please refrain from using ModuleStore!
|
||||
*/
|
||||
export class ModuleStore implements CoreModuleStore {
|
||||
commands = new Map<string, string>();
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { ErrorHandling } from "../../contracts";
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @since 2.0.0
|
||||
*/
|
||||
export class DefaultErrorHandling implements ErrorHandling {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { LogPayload, Logging } from "../../contracts";
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @since 2.0.0
|
||||
*/
|
||||
export class DefaultLogging implements Logging {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { ModuleStore } from "../../../shared";
|
||||
import { ModuleManager } from "../../contracts";
|
||||
import { importModule } from "../../module-loading";
|
||||
import { CommandModule } from "../../types/modules";
|
||||
import { ModuleStore } from "../module-store";
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @since 2.0.0
|
||||
*/
|
||||
export class DefaultModuleManager implements ModuleManager {
|
||||
@@ -14,14 +15,14 @@ export class DefaultModuleManager implements ModuleManager {
|
||||
}
|
||||
|
||||
get(id: string) {
|
||||
return this.moduleStore.get(id);
|
||||
return this.moduleStore.commands.get(id);
|
||||
}
|
||||
set(id: string, path: string): void {
|
||||
this.moduleStore.set(id, path);
|
||||
this.moduleStore.commands.set(id, path);
|
||||
}
|
||||
//not tested
|
||||
getPublishableCommands(): Promise<CommandModule[]> {
|
||||
const entries = this.moduleStore.entries();
|
||||
const entries = this.moduleStore.commands.entries();
|
||||
const publishable = 0b000000110;
|
||||
return Promise.all(
|
||||
Array.from(entries)
|
||||
|
||||
Reference in New Issue
Block a user