@discordx/di
Advanced tools
Comparing version 3.1.0 to 3.1.1
@@ -1,7 +0,9 @@ | ||
import type { DependencyContainer } from "tsyringe"; | ||
import type { DependencyContainer, FactoryFunction } from "tsyringe"; | ||
import type { InstanceOf } from "../../index.js"; | ||
import { AbstractConfigurableDependencyInjector } from "../AbstractConfigurableDependencyInjector.js"; | ||
type Factory = <T>(factoryFunc: FactoryFunction<T>) => FactoryFunction<T>; | ||
export declare class TsyringeDependencyRegistryEngine extends AbstractConfigurableDependencyInjector<DependencyContainer> { | ||
static token: symbol; | ||
private static _instance; | ||
private factory; | ||
static get instance(): TsyringeDependencyRegistryEngine; | ||
@@ -12,3 +14,5 @@ addService<T>(classType: T): void; | ||
setToken(token: symbol): this; | ||
setCashingSingletonFactory(factory: Factory): this; | ||
private getInstanceCashingSingletonFactory; | ||
} | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TsyringeDependencyRegistryEngine = void 0; | ||
const tsyringe_1 = require("tsyringe"); | ||
const AbstractConfigurableDependencyInjector_js_1 = require("../AbstractConfigurableDependencyInjector.js"); | ||
@@ -9,2 +8,3 @@ class TsyringeDependencyRegistryEngine extends AbstractConfigurableDependencyInjector_js_1.AbstractConfigurableDependencyInjector { | ||
static _instance; | ||
factory = null; | ||
static get instance() { | ||
@@ -23,4 +23,7 @@ if (!TsyringeDependencyRegistryEngine._instance) { | ||
const clazz = classType; | ||
const instanceCashingSingletonFactory = this.getInstanceCashingSingletonFactory(clazz); | ||
if (this.useToken) { | ||
if (!this.factory) { | ||
throw new Error("Unable to init tokenization without instance factory"); | ||
} | ||
const instanceCashingSingletonFactory = this.getInstanceCashingSingletonFactory(clazz); | ||
this.injector.register(TsyringeDependencyRegistryEngine.token, { | ||
@@ -39,3 +42,3 @@ useFactory: instanceCashingSingletonFactory, | ||
const clazz = classType; | ||
if (this.useToken && !tsyringe_1.container.isRegistered(clazz)) { | ||
if (this.useToken && !this.injector.isRegistered(clazz)) { | ||
return (this.injector | ||
@@ -64,4 +67,11 @@ .resolveAll(TsyringeDependencyRegistryEngine.token) | ||
} | ||
setCashingSingletonFactory(factory) { | ||
this.factory = factory; | ||
return this; | ||
} | ||
getInstanceCashingSingletonFactory(clazz) { | ||
return (0, tsyringe_1.instanceCachingFactory)((c) => { | ||
if (!this.factory) { | ||
throw new Error("Unable to init tokenization without instance factory"); | ||
} | ||
return this.factory((c) => { | ||
if (!c.isRegistered(clazz)) { | ||
@@ -68,0 +78,0 @@ c.registerSingleton(clazz); |
@@ -1,7 +0,9 @@ | ||
import type { DependencyContainer } from "tsyringe"; | ||
import type { DependencyContainer, FactoryFunction } from "tsyringe"; | ||
import type { InstanceOf } from "../../index.js"; | ||
import { AbstractConfigurableDependencyInjector } from "../AbstractConfigurableDependencyInjector.js"; | ||
type Factory = <T>(factoryFunc: FactoryFunction<T>) => FactoryFunction<T>; | ||
export declare class TsyringeDependencyRegistryEngine extends AbstractConfigurableDependencyInjector<DependencyContainer> { | ||
static token: symbol; | ||
private static _instance; | ||
private factory; | ||
static get instance(): TsyringeDependencyRegistryEngine; | ||
@@ -12,3 +14,5 @@ addService<T>(classType: T): void; | ||
setToken(token: symbol): this; | ||
setCashingSingletonFactory(factory: Factory): this; | ||
private getInstanceCashingSingletonFactory; | ||
} | ||
export {}; |
@@ -1,2 +0,1 @@ | ||
import { container, instanceCachingFactory } from "tsyringe"; | ||
import { AbstractConfigurableDependencyInjector } from "../AbstractConfigurableDependencyInjector.js"; | ||
@@ -6,2 +5,3 @@ class TsyringeDependencyRegistryEngine extends AbstractConfigurableDependencyInjector { | ||
static _instance; | ||
factory = null; | ||
static get instance() { | ||
@@ -20,4 +20,7 @@ if (!TsyringeDependencyRegistryEngine._instance) { | ||
const clazz = classType; | ||
const instanceCashingSingletonFactory = this.getInstanceCashingSingletonFactory(clazz); | ||
if (this.useToken) { | ||
if (!this.factory) { | ||
throw new Error("Unable to init tokenization without instance factory"); | ||
} | ||
const instanceCashingSingletonFactory = this.getInstanceCashingSingletonFactory(clazz); | ||
this.injector.register(TsyringeDependencyRegistryEngine.token, { | ||
@@ -36,3 +39,3 @@ useFactory: instanceCashingSingletonFactory, | ||
const clazz = classType; | ||
if (this.useToken && !container.isRegistered(clazz)) { | ||
if (this.useToken && !this.injector.isRegistered(clazz)) { | ||
return (this.injector | ||
@@ -61,4 +64,11 @@ .resolveAll(TsyringeDependencyRegistryEngine.token) | ||
} | ||
setCashingSingletonFactory(factory) { | ||
this.factory = factory; | ||
return this; | ||
} | ||
getInstanceCashingSingletonFactory(clazz) { | ||
return instanceCachingFactory((c) => { | ||
if (!this.factory) { | ||
throw new Error("Unable to init tokenization without instance factory"); | ||
} | ||
return this.factory((c) => { | ||
if (!c.isRegistered(clazz)) { | ||
@@ -65,0 +75,0 @@ c.registerSingleton(clazz); |
@@ -0,1 +1,5 @@ | ||
## Fixed | ||
- when no di method used, exception was thrown ([#943](https://github.com/discordx-ts/discordx/issues/943)) ([a85a8f](https://github.com/discordx-ts/discordx/commit/a85a8feb45d7422fe483360017e20718ef90af6d)) | ||
# [3.1.0](https://github.com/discordx-ts/discordx/releases/tag/di-3.1.0) (2023-05-05) | ||
@@ -2,0 +6,0 @@ |
{ | ||
"name": "@discordx/di", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "description": "dependency injection service with TSyringe support", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
61952
760