From 87585dcc699c399cbccb9078d62a43f3aafe0304 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sat, 6 May 2023 22:13:09 -0500 Subject: [PATCH] refactor: change all files to camelcase and refactor --- .../{errorHandling.ts => error-handling.ts} | 0 src/core/contracts/index.ts | 4 +- .../{moduleManager.ts => module-manager.ts} | 0 src/core/dependencies.ts | 63 +++++++++---------- .../{createPlugin.ts => create-plugin.ts} | 0 src/core/plugins/index.ts | 2 +- src/core/structures/index.ts | 2 +- .../{sernEmitter.ts => sern-emitter.ts} | 0 8 files changed, 35 insertions(+), 36 deletions(-) rename src/core/contracts/{errorHandling.ts => error-handling.ts} (100%) rename src/core/contracts/{moduleManager.ts => module-manager.ts} (100%) rename src/core/plugins/{createPlugin.ts => create-plugin.ts} (100%) rename src/core/structures/{sernEmitter.ts => sern-emitter.ts} (100%) diff --git a/src/core/contracts/errorHandling.ts b/src/core/contracts/error-handling.ts similarity index 100% rename from src/core/contracts/errorHandling.ts rename to src/core/contracts/error-handling.ts diff --git a/src/core/contracts/index.ts b/src/core/contracts/index.ts index 725919d..a9bb82f 100644 --- a/src/core/contracts/index.ts +++ b/src/core/contracts/index.ts @@ -1,3 +1,3 @@ -export { type ErrorHandling, DefaultErrorHandling } from './errorHandling'; +export { type ErrorHandling, DefaultErrorHandling } from './error-handling'; export { type Logging, DefaultLogging } from './logging'; -export { type ModuleManager, DefaultModuleManager } from './moduleManager'; +export { type ModuleManager, DefaultModuleManager } from './module-manager'; diff --git a/src/core/contracts/moduleManager.ts b/src/core/contracts/module-manager.ts similarity index 100% rename from src/core/contracts/moduleManager.ts rename to src/core/contracts/module-manager.ts diff --git a/src/core/dependencies.ts b/src/core/dependencies.ts index c9c9b01..bb8ffc4 100644 --- a/src/core/dependencies.ts +++ b/src/core/dependencies.ts @@ -1,19 +1,16 @@ -import type { Container } from 'iti'; -import type { AnyDependencies, DependencyConfiguration, MapDeps, Wrapper } from '../types/core'; +import { Container } from 'iti'; +import type { Dependencies, DependencyConfiguration, MapDeps, Wrapper } from '../types/core'; import { DefaultErrorHandling, DefaultLogging, DefaultModuleManager } from './contracts'; import { Result } from 'ts-results-es'; -import { BehaviorSubject } from 'rxjs'; import { createContainer } from 'iti'; import { SernEmitter } from './structures'; - -export const containerSubject = new BehaviorSubject(defaultContainer()); - +import { SernError } from './structures/errors'; +export let containerSubject: Container<{}, {}> +const requiredDependencyKeys = ['@sern/emitter', '@sern/errors', '@sern/logger'] as const; /** * @__PURE__ * @since 2.0.0. - * Please note that on intellij, the deprecation is for all signatures, which is unintended behavior (and - * very annoying). - * For future versions, ensure that single is being passed as a **callback!!** + * use single if you want a singleton, or an object that is called once. * @param cb */ export function single(cb: () => T) { @@ -36,31 +33,31 @@ export function transient(cb: () => () => T) { * Finally, update the containerSubject with the new container state * @param conf */ -export function composeRoot(conf: DependencyConfiguration) { +export function composeRoot(conf: DependencyConfiguration) { //This should have no client or logger yet. - const currentContainer = containerSubject.getValue(); const excludeLogger = conf.exclude?.has('@sern/logger'); if (!excludeLogger) { - currentContainer.add({ + containerSubject.add({ '@sern/logger': () => new DefaultLogging(), }); } //Build the container based on the callback provided by the user - const container = conf.build(currentContainer); - //Check if the built container contains @sern/client or throw - // a runtime exception - //Result.wrap(() => container.get('@sern/client')).expect(SernError.MissingRequired); + const container = conf.build(containerSubject as Container, {}>); + try { + container.get('@sern/client'); + } catch { + throw new Error(SernError.MissingRequired + " No client was provided") + } if (!excludeLogger) { container.get('@sern/logger')?.info({ message: 'All dependencies loaded successfully.' }); } - containerSubject.next(container as any); } -export function useContainer() { - const container = containerSubject.getValue() as Container; +export function useContainer() { + const container = containerSubject as Container; return (...keys: [...V]) => - keys.map(key => Result.wrap(() => container.get(key)).unwrapOr(undefined)) as MapDeps; + keys.map(key => Result.wrap(() => container.get(key)).expect(`Unregistered dependency: ${String(key)}`)) as MapDeps; } /** @@ -68,8 +65,11 @@ export function useContainer() { * Please be careful as this only gets the client's current state. * Exposes some methods from iti */ -export function useContainerRaw() { - return containerSubject.getValue() as Container; +export function useContainerRaw() { + if(!containerSubject) { + throw Error("Could not find container. Did you call makeDependencies?") + } + return containerSubject as Container; } /** @@ -78,32 +78,30 @@ export function useContainerRaw() { */ function defaultContainer() { return createContainer() - .add({ '@sern/errors': () => new DefaultErrorHandling() }) - .add({ '@sern/store': () => new Map() }) + .add({ + '@sern/errors': () => new DefaultErrorHandling(), + '@sern/store': () => new Map(), + '@sern/emitter': () => new SernEmitter() + }) .add(ctx => { return { '@sern/modules': () => new DefaultModuleManager(ctx['@sern/store']), }; }) - .add({ '@sern/emitter': () => new SernEmitter() }) as Container< - Omit, - {} - >; } -const requiredDependencyKeys = ['@sern/emitter', '@sern/errors', '@sern/logger'] as const; /** * A way for sern to grab only the necessary dependencies. * Returns a function which allows for the user to call for more dependencies. */ -export function makeFetcher( +export function makeFetcher( containerConfig: Wrapper['containerConfig'], ) { return (otherKeys: [...Keys]) => containerConfig.get( ...requiredDependencyKeys, - ...(otherKeys as (keyof AnyDependencies)[]), + ...(otherKeys as (keyof Dependencies)[]), ) as MapDeps; } @@ -111,9 +109,10 @@ export function makeFetcher( * @since 2.0.0 * @param conf a configuration for creating your project dependencies */ -export function makeDependencies( +export function makeDependencies( conf: DependencyConfiguration, ) { + containerSubject = defaultContainer() //Until there are more optional dependencies, just check if the logger exists composeRoot(conf); return useContainer(); diff --git a/src/core/plugins/createPlugin.ts b/src/core/plugins/create-plugin.ts similarity index 100% rename from src/core/plugins/createPlugin.ts rename to src/core/plugins/create-plugin.ts diff --git a/src/core/plugins/index.ts b/src/core/plugins/index.ts index e03a2f5..593a16e 100644 --- a/src/core/plugins/index.ts +++ b/src/core/plugins/index.ts @@ -1,2 +1,2 @@ export * from './args'; -export * from './createPlugin'; +export * from './create-plugin'; diff --git a/src/core/structures/index.ts b/src/core/structures/index.ts index 5bd8ce9..913cfd6 100644 --- a/src/core/structures/index.ts +++ b/src/core/structures/index.ts @@ -1,3 +1,3 @@ export * from './enums'; export * from './context'; -export * from './sernEmitter'; +export * from './sern-emitter'; diff --git a/src/core/structures/sernEmitter.ts b/src/core/structures/sern-emitter.ts similarity index 100% rename from src/core/structures/sernEmitter.ts rename to src/core/structures/sern-emitter.ts