mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
nvm, now i did
This commit is contained in:
@@ -35,7 +35,7 @@
|
||||
"author": "SernDevs",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@sern/ioc": "^1.0.2",
|
||||
"@sern/ioc": "^1.0.3",
|
||||
"callsites": "^3.1.0",
|
||||
"node-cron": "^3.0.3",
|
||||
"rxjs": "^7.8.0",
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
import { Container } from '@sern/ioc';
|
||||
import * as __Services from '../structures/default-services';
|
||||
import type { Logging } from '../interfaces';
|
||||
import { __add_container, __add_wiredcontainer, __init_container, __swap_container, useContainerRaw } from './global';
|
||||
import { EventEmitter } from 'node:events';
|
||||
|
||||
export function disposeAll(logger: Logging|undefined) {
|
||||
useContainerRaw()
|
||||
?.disposeAll()
|
||||
.then(() => logger?.info({ message: 'Cleaning container and crashing' }));
|
||||
}
|
||||
|
||||
type Insertable =
|
||||
| ((container: Dependencies) => object)
|
||||
| object
|
||||
|
||||
const dependencyBuilder = (container: Container) => {
|
||||
return {
|
||||
/**
|
||||
* Insert a dependency into your container.
|
||||
* Supply the correct key and dependency
|
||||
*/
|
||||
add(key: keyof Dependencies, v: Insertable) {
|
||||
if(typeof v !== 'function') {
|
||||
container.addSingleton(key, v)
|
||||
} else {
|
||||
//@ts-ignore
|
||||
container.addWiredSingleton(key, (cntr) => v(cntr))
|
||||
}
|
||||
},
|
||||
/**
|
||||
* @param key the key of the dependency
|
||||
* @param v The dependency to swap out.
|
||||
* Swap out a preexisting dependency.
|
||||
*/
|
||||
swap(key: keyof Dependencies, v: Insertable) {
|
||||
this.swap(key, v);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
type ValidDependencyConfig =
|
||||
| ((c: ReturnType<typeof dependencyBuilder>) => any)
|
||||
|
||||
/**
|
||||
* makeDependencies constructs a dependency injection container for sern handler to use.
|
||||
* This is required to start the handler, and is to be called before Sern.init.
|
||||
* @example
|
||||
* ```ts
|
||||
* await makeDependencies(({ add }) => {
|
||||
* add('@sern/client', new Client({ intents, partials })
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
export async function makeDependencies (conf: ValidDependencyConfig) {
|
||||
const container = await __init_container({ autowire: false });
|
||||
//We only include logger if it does not exist
|
||||
const includeLogger = !container.hasKey('@sern/logger');
|
||||
|
||||
if(includeLogger) {
|
||||
__add_container('@sern/logger', new __Services.DefaultLogging);
|
||||
}
|
||||
__add_container('@sern/errors', new __Services.DefaultErrorHandling);
|
||||
__add_container('@sern/modules', new Map)
|
||||
__add_container('@sern/emitter', new EventEmitter)
|
||||
__add_wiredcontainer('@sern/cron', deps => new __Services.Cron(deps))
|
||||
conf(dependencyBuilder(container));
|
||||
await container.ready();
|
||||
}
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
import { Container } from '@sern/ioc';
|
||||
import { UnpackedDependencies } from '../../types/utility';
|
||||
|
||||
//SIDE EFFECT: GLOBAL DI
|
||||
let containerSubject: Container;
|
||||
|
||||
/**
|
||||
* Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything
|
||||
* then it will swap
|
||||
*/
|
||||
export async function __swap_container(c: Container) {
|
||||
if(containerSubject) {
|
||||
await containerSubject.disposeAll()
|
||||
}
|
||||
containerSubject = c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything
|
||||
* then it will swap
|
||||
*/
|
||||
export function __add_container(key: string, v: object) {
|
||||
containerSubject.addSingleton(key, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything
|
||||
* then it will swap
|
||||
*/
|
||||
export function __add_wiredcontainer(key: string, depfn : (deps: UnpackedDependencies) => object) {
|
||||
//@ts-ignore
|
||||
containerSubject.addWiredSingleton(key, depfn);
|
||||
}
|
||||
/**
|
||||
* Initiates the global api.
|
||||
* Once this is finished, the Service api and the other global api is available
|
||||
*/
|
||||
export async function __init_container(options: {
|
||||
autowire: boolean;
|
||||
path?: string | undefined;
|
||||
}) {
|
||||
containerSubject = new Container(options);
|
||||
await containerSubject.ready()
|
||||
return containerSubject
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying data structure holding all dependencies.
|
||||
* Exposes methods from iti
|
||||
* Use the Service API. The container should be readonly
|
||||
*/
|
||||
export function useContainerRaw() {
|
||||
if (!(containerSubject && containerSubject.isReady())) {
|
||||
throw new Error("Container wasn't ready or init'd. Please ensure container is ready()");
|
||||
}
|
||||
|
||||
return containerSubject;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Service api, retrieve from the globally init'ed container
|
||||
* Note: this method only works AFTER your container has been initiated
|
||||
* @since 3.0.0
|
||||
* @example
|
||||
* ```ts
|
||||
* const client = Service('@sern/client');
|
||||
* ```
|
||||
* @param key a key that corresponds to a dependency registered.
|
||||
*
|
||||
*/
|
||||
export function Service<const T>(key: PropertyKey) {
|
||||
const dep = useContainerRaw().get<T>(key)!;
|
||||
if(!dep) {
|
||||
throw Error("Requested key " + String(key) + " returned undefined");
|
||||
}
|
||||
return dep;
|
||||
}
|
||||
/**
|
||||
* @since 3.0.0
|
||||
* The plural version of {@link Service}
|
||||
* @returns array of dependencies, in the same order of keys provided
|
||||
*/
|
||||
export function Services<const T extends string[], V>(...keys: [...T]) {
|
||||
const container = useContainerRaw();
|
||||
return keys.map(k => container.get(k)!) as V;
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
import type { IntoDependencies } from '../../types/ioc';
|
||||
import { Service as __Service, Services as __Services } from './global'
|
||||
export { makeDependencies } from './base';
|
||||
|
||||
|
||||
/**
|
||||
* The new Service api, a cleaner alternative to useContainer
|
||||
* To obtain intellisense, ensure a .d.ts file exists in the root of compilation.
|
||||
* Usually our scaffolding tool takes care of this.
|
||||
* Note: this method only works AFTER your container has been initiated
|
||||
* @since 3.0.0
|
||||
* @example
|
||||
* ```ts
|
||||
* const client = Service('@sern/client');
|
||||
* ```
|
||||
* @param key a key that corresponds to a dependency registered.
|
||||
*
|
||||
*/
|
||||
export function Service<const T extends keyof Dependencies>(key: T) {
|
||||
return __Service(key) as Dependencies[T]
|
||||
}
|
||||
/**
|
||||
* @since 3.0.0
|
||||
* The plural version of {@link Service}
|
||||
* @returns array of dependencies, in the same order of keys provided
|
||||
*/
|
||||
export function Services<const T extends (keyof Dependencies)[]>(...keys: [...T]) {
|
||||
return __Services<T, IntoDependencies<T>>(...keys)
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* Creates a singleton object.
|
||||
* @param cb
|
||||
*/
|
||||
export function single<T>(cb: () => T) {
|
||||
console.log('The `single` function is deprecated and has no effect')
|
||||
return cb();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* @since 2.0.0
|
||||
* Creates a transient object
|
||||
* @param cb
|
||||
*/
|
||||
export function transient<T>(cb: () => () => T) {
|
||||
console.log('The `transient` function is deprecated and has no effect')
|
||||
return cb()();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import * as assert from 'node:assert';
|
||||
import { Context } from '../core/structures/context';
|
||||
import { CommandType } from '../core/structures/enums'
|
||||
import { inspect } from 'node:util'
|
||||
import { disposeAll } from '../core/ioc/base';
|
||||
import { disposeAll } from '../core/ioc';
|
||||
import { resultPayload, isAutocomplete, treeSearch, fmt } from '../core/functions'
|
||||
|
||||
function handleError<C>(crashHandler: ErrorHandling, emitter: Emitter, logging?: Logging) {
|
||||
|
||||
@@ -53,7 +53,7 @@ export type Controller = typeof controller
|
||||
export * from './core/create-plugins';
|
||||
export { CommandType, PluginType, PayloadType, EventType } from './core/structures/enums';
|
||||
export { Context } from './core/structures/context';
|
||||
export * from './core/ioc';
|
||||
export { makeDependencies, single, transient, Service, Services } from './core/ioc';
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
//side effect: global container
|
||||
import { useContainerRaw } from '@sern/ioc/global';
|
||||
|
||||
import callsites from 'callsites';
|
||||
import * as Files from './core/module-loading';
|
||||
import { merge } from 'rxjs';
|
||||
@@ -7,7 +10,6 @@ import messageHandler from './handlers/message';
|
||||
import interactionHandler from './handlers/interaction';
|
||||
import { presenceHandler } from './handlers/presence';
|
||||
import { handleCrash } from './handlers/event-utils';
|
||||
import { useContainerRaw } from './core/ioc/global';
|
||||
import { UnpackedDependencies } from './types/utility';
|
||||
import type { PresenceResult } from './core/presences';
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Container } from '../core/ioc/container';
|
||||
import type { Container } from '@sern/ioc';
|
||||
import * as Contracts from '../core/interfaces';
|
||||
import type { UnpackFunction } from './utility'
|
||||
import type { Client } from 'discord.js'
|
||||
|
||||
10
yarn.lock
10
yarn.lock
@@ -553,7 +553,7 @@ __metadata:
|
||||
resolution: "@sern/handler@workspace:."
|
||||
dependencies:
|
||||
"@faker-js/faker": ^8.0.1
|
||||
"@sern/ioc": ^1.0.2
|
||||
"@sern/ioc": ^1.0.3
|
||||
"@types/node": ^20.0.0
|
||||
"@types/node-cron": ^3.0.11
|
||||
"@typescript-eslint/eslint-plugin": 5.58.0
|
||||
@@ -569,10 +569,10 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@sern/ioc@npm:^1.0.2":
|
||||
version: 1.0.2
|
||||
resolution: "@sern/ioc@npm:1.0.2"
|
||||
checksum: 7e127e07c3f7eb2eefa77eb5f7c48ba37a259f089cbadfd91e1f6d448a5083cd7f38d4d3c5a2c6efada1b62120d310a3854ba2ba6444fb6a7279a11e2a0b4705
|
||||
"@sern/ioc@npm:^1.0.3":
|
||||
version: 1.0.3
|
||||
resolution: "@sern/ioc@npm:1.0.3"
|
||||
checksum: 2c640cabbf3927d923b57233e660be1d6d4da1cd376a4ba77b118fd9aa5ef81c465d287357a15a7cd7827306dd614c73bde68751978d2030c3a4877dcbb0d02d
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
|
||||
Reference in New Issue
Block a user