revert: readd module store and add contract

This commit is contained in:
Jacob Nguyen
2023-05-10 21:40:40 -05:00
parent e6f3a55d73
commit 0256a38187
10 changed files with 60 additions and 26 deletions

View File

@@ -18,5 +18,6 @@ export interface ErrorHandling {
* @param error
*/
updateAlive(error: Error): void;
}

View File

@@ -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'

View File

@@ -0,0 +1,4 @@
export interface CoreModuleStore {
commands : Map<string, string>;
}

View File

@@ -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();
}
}

View File

@@ -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> {

View File

@@ -2,3 +2,4 @@ export * from './enums';
export * from './context';
export * from './sern-emitter';
export * from './services'
export * from './module-store'

View 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>();
}

View File

@@ -1,6 +1,7 @@
import { ErrorHandling } from "../../contracts";
/**
* @internal
* @since 2.0.0
*/
export class DefaultErrorHandling implements ErrorHandling {

View File

@@ -1,6 +1,7 @@
import { LogPayload, Logging } from "../../contracts";
/**
* @internal
* @since 2.0.0
*/
export class DefaultLogging implements Logging {

View File

@@ -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)