Remove module store, manager, and Intializable type

This commit is contained in:
Jacob Nguyen
2024-04-28 19:34:08 -05:00
parent 3d10ee1c59
commit 76ee9c6edf
13 changed files with 27 additions and 158 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,5 +1,4 @@
export { CommandType, PluginType, PayloadType, EventType } from './enums';
export * from './context';
export * from './services';
export * from './module-store';

View File

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

View File

@@ -1,3 +1,2 @@
export * from './error-handling';
export * from './logger';
export * from './module-manager';

View File

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