the end of sern init??

This commit is contained in:
Jacob Nguyen
2024-04-28 22:16:54 -05:00
parent 68c5f09b46
commit 071d5eac49
8 changed files with 22 additions and 47 deletions

View File

@@ -7,10 +7,7 @@ export function makePlugin<V extends unknown[]>(
type: PluginType,
execute: (...args: any[]) => any,
): Plugin<V> {
return {
type,
execute,
} as Plugin<V>;
return { type, execute, } as Plugin<V>;
}
/**
* @since 2.5.0

View File

@@ -1,5 +1,4 @@
import * as assert from 'assert';
import { useContainer } from './dependency-injection';
import type { CoreDependencies, DependencyConfiguration } from '../../types/ioc';
import { CoreContainer } from './container';
import { Result } from 'ts-results-es';
@@ -141,7 +140,7 @@ function composeRoot(
container.ready();
}
export async function makeDependencies<const T extends Dependencies>
export async function makeDependencies
(conf: ValidDependencyConfig) {
containerSubject = new CoreContainer();
if(typeof conf === 'function') {
@@ -160,7 +159,5 @@ export async function makeDependencies<const T extends Dependencies>
} else {
composeRoot(containerSubject, conf);
}
return useContainer<T>();
}

View File

@@ -48,10 +48,3 @@ export function Services<const T extends (keyof Dependencies)[]>(...keys: [...T]
const container = useContainerRaw();
return keys.map(k => container.get(k)!) as IntoDependencies<T>;
}
export function useContainer<const T extends Dependencies>() {
return <V extends (keyof T)[]>(...keys: [...V]) =>
keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as IntoDependencies<V>;
}

View File

@@ -1,7 +1,7 @@
import path from 'node:path';
import assert from 'assert';
import { createRequire } from 'node:module';
import type { ImportPayload, Wrapper } from '../types/core';
import type { Wrapper } from '../types/core';
import { existsSync } from 'fs';
import type { Logging } from './interfaces';
@@ -17,7 +17,7 @@ export const parseCallsite = (fpath: string) => {
export const shouldHandle = (pth: string, filenam: string) => {
const file_name = filenam+path.extname(pth);
let newPath = path.join(path.dirname(pth), file_name)
.replace(/file:\\?/, "");
.replace(/file:\\?/, "");
return { exists: existsSync(newPath),
path: 'file:///'+newPath };
}
@@ -42,7 +42,7 @@ export async function importModule<T>(absPath: string) {
let commandModule = fileModule.default;
assert(commandModule , `Found no export @ ${absPath}. Forgot to ignore with "!"? (!${path.basename(absPath)})?`);
assert(commandModule , `No export @ ${absPath}. Forgot to ignore with "!"? (!${path.basename(absPath)})?`);
if ('default' in commandModule) {
commandModule = commandModule.default;
}

View File

@@ -36,10 +36,12 @@ export type {
AnyCommandPlugin,
} from './types/core-plugin';
export type { Wrapper } from './types/core';
export interface Wrapper {
commands: string;
defaultPrefix?: string;
events?: string;
}
export type { Args, SlashOptions, Payload, SernEventsMapping } from './types/utility';
export type { Singleton, Transient, CoreDependencies } from './types/ioc';
export {

View File

@@ -1,15 +1,16 @@
import { handleCrash } from './handlers/event-utils';
import callsites from 'callsites';
import { Files } from './core/_internal';
import path from 'node:path'
import { merge } from 'rxjs';
import { Services } from './core/ioc';
import { Wrapper } from './types/core';
import { eventsHandler } from './handlers/user-defined-events';
import { readyHandler } from './handlers/ready-event';
import { messageHandler } from './handlers/message-event';
import { interactionHandler } from './handlers/interaction-event';
import { presenceHandler } from './handlers/presence';
import type { Client } from 'discord.js';
import { type Wrapper } from './';
/**
@@ -24,7 +25,7 @@ import type { Client } from 'discord.js';
* })
* ```
*/
export function init(maybeWrapper: Wrapper | 'file') {
export function init() {
const startTime = performance.now();
const dependencies = Services('@sern/emitter',
'@sern/errors',
@@ -33,14 +34,15 @@ export function init(maybeWrapper: Wrapper | 'file') {
const logger = dependencies[2],
errorHandler = dependencies[1];
const wrapper = Files.loadConfig(maybeWrapper, logger);
if (wrapper.events !== undefined) {
//const wrapper = Files.loadConfig(maybeWrapper, logger);
//if (wrapper.events !== undefined) {
//eventsHandler(dependencies, Files.getFullPathTree(wrapper.events));
}
// }
//import(path.resolve("commands.js"))
//.then(({ commands }) => { })
//.catch(message => logger?.error({ message }))
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(() => {
@@ -55,7 +57,6 @@ export function init(maybeWrapper: Wrapper | 'file') {
// presenceHandler(presencePath.path, setPresence).subscribe();
// }
// });
//const messages$ = messageHandler(dependencies, wrapper.defaultPrefix);
//const interactions$ = interactionHandler(dependencies);
// listening to the message stream and interaction stream

View File

@@ -1,12 +0,0 @@
export interface ImportPayload<T> {
module: T;
absPath: string;
[key: string]: unknown;
}
export interface Wrapper {
commands: string;
defaultPrefix?: string;
events?: string;
}

View File

@@ -1,7 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { CoreContainer } from '../../src/core/ioc/container';
import { EventEmitter } from 'events';
import { DefaultLogging, Disposable, Emitter, Init, Logging } from '../../src/core';
import { Disposable, Emitter, Init, Logging, __Services } from '../../src/core';
import { CoreDependencies } from '../../src/types/ioc';
describe('ioc container', () => {
@@ -43,13 +43,12 @@ describe('ioc container', () => {
});
it('should container all core dependencies', async () => {
const keys = [
'@sern/modules',
'@sern/emitter',
'@sern/logger',
'@sern/errors',
] satisfies (keyof CoreDependencies)[];
container.add({
'@sern/logger': () => new DefaultLogging(),
'@sern/logger': () => new __Services.DefaultLogging(),
'@sern/client': () => new EventEmitter(),
});
for (const k of keys) {
@@ -92,9 +91,7 @@ describe('ioc container', () => {
it('should init dependency depending on something else', () => {
container.add({ '@sern/client': dependency2 });
container.upsert((cntr) => ({
'@sern/logger': dependency
}));
container.upsert((cntr) => ({ '@sern/logger': dependency }));
container.ready();
expect(dependency.init).toHaveBeenCalledTimes(1);
})