Skip to content

Commit

Permalink
refactor: do not make manager classes constructable via container
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 27, 2023
1 parent 619a0a9 commit 6ddc941
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 28 deletions.
16 changes: 6 additions & 10 deletions providers/app_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
*/

import { Config } from '../modules/config.js'
import { Emitter } from '../modules/events.js'
import { Logger } from '../modules/logger.js'
import { validator } from '../legacy/validator.js'
import { Encryption } from '../modules/encryption.js'
import { Logger, LoggerManager } from '../modules/logger.js'
import type { AbstractConstructor } from '../types/container.js'
import type { ApplicationService, EmitterService, LoggerService } from '../src/types.js'
import type { ApplicationService, LoggerService } from '../src/types.js'

/**
* The Application Service provider registers all the baseline
Expand Down Expand Up @@ -63,12 +61,11 @@ export default class AppServiceProvider {
* Registers the logger manager to the container
*/
protected registerLoggerManager() {
const LoggerServiceManager = LoggerManager as unknown as AbstractConstructor<LoggerService>
this.app.container.singleton(LoggerServiceManager, () => {
this.app.container.singleton('logger', async () => {
const { LoggerManager } = await import('../modules/logger.js')
const config = this.app.config.get<any>('logger')
return new LoggerManager(config) as LoggerService
})
this.app.container.alias('logger', LoggerServiceManager)
}

/**
Expand All @@ -83,11 +80,10 @@ export default class AppServiceProvider {
* Registers emitter service to the container
*/
protected registerEmitter() {
this.app.container.singleton<AbstractConstructor<EmitterService>>(Emitter, () => {
this.app.container.singleton('emitter', async () => {
const { Emitter } = await import('../modules/events.js')
return new Emitter(this.app)
})

this.app.container.alias('emitter', Emitter)
}

/**
Expand Down
9 changes: 4 additions & 5 deletions providers/hash_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
* file that was distributed with this source code.
*/

import { Hash, HashManager } from '../modules/hash/main.js'
import type { AbstractConstructor } from '../types/container.js'
import type { ApplicationService, HashService } from '../src/types.js'
import { Hash } from '../modules/hash/main.js'
import type { ApplicationService } from '../src/types.js'
import hashDriversCollection from '../modules/hash/drivers_collection.js'

/**
Expand Down Expand Up @@ -41,11 +40,11 @@ export default class HashServiceProvider {
* Registers the hash manager with the container
*/
protected registerHashManager() {
this.app.container.singleton<AbstractConstructor<HashService>>(HashManager, async () => {
this.app.container.singleton('hash', async () => {
const { HashManager } = await import('../modules/hash/main.js')
const config = this.app.config.get<any>('hash')
return new HashManager(config)
})
this.app.container.alias('hash', HashManager)
}

/**
Expand Down
4 changes: 1 addition & 3 deletions services/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
*/

import app from './app.js'
import { Emitter } from '../modules/events.js'
import type { EmitterService } from '../src/types.js'
import { AbstractConstructor } from '@adonisjs/fold/types'

let emitter: EmitterService

Expand All @@ -19,7 +17,7 @@ let emitter: EmitterService
* from the container
*/
await app.booted(async () => {
emitter = await app.container.make<AbstractConstructor<EmitterService>>(Emitter)
emitter = await app.container.make('emitter')
})

export { emitter as default }
3 changes: 1 addition & 2 deletions services/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import app from './app.js'
import { Encryption } from '../modules/encryption.js'
import type { EncryptionService } from '../src/types.js'

let encryption: EncryptionService
Expand All @@ -18,7 +17,7 @@ let encryption: EncryptionService
* from the container
*/
await app.booted(async () => {
encryption = await app.container.make(Encryption)
encryption = await app.container.make('encryption')
})

export { encryption as default }
3 changes: 1 addition & 2 deletions services/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import app from './app.js'
import type { HashService } from '../src/types.js'
import { HashManager } from '../modules/hash/main.js'

let hash: HashService

Expand All @@ -18,7 +17,7 @@ let hash: HashService
* container
*/
await app.booted(async () => {
hash = await app.container.make(HashManager)
hash = await app.container.make('hash')
})

export { hash as default }
3 changes: 1 addition & 2 deletions services/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import app from './app.js'
import { LoggerManager } from '@adonisjs/logger'
import type { LoggerService } from '../src/types.js'

let logger: LoggerService
Expand All @@ -18,7 +17,7 @@ let logger: LoggerService
* from the container
*/
await app.booted(async () => {
logger = (await app.container.make(LoggerManager)) as LoggerService
logger = await app.container.make('logger')
})

export { logger as default }
3 changes: 1 addition & 2 deletions services/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import app from './app.js'
import { Router } from '../modules/http.js'
import type { HttpRouterService } from '../src/types.js'

let router: HttpRouterService
Expand All @@ -18,7 +17,7 @@ let router: HttpRouterService
* the container
*/
await app.booted(async () => {
router = await app.container.make(Router)
router = await app.container.make('router')
})

export { router as default }
3 changes: 1 addition & 2 deletions services/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import app from './app.js'
import { Server } from '../modules/http.js'
import type { HttpServerService } from '../src/types.js'

let server: HttpServerService
Expand All @@ -18,7 +17,7 @@ let server: HttpServerService
* from the container
*/
await app.booted(async () => {
server = await app.container.make(Server)
server = await app.container.make('server')
})

export { server as default }

0 comments on commit 6ddc941

Please sign in to comment.