Skip to content

Commit

Permalink
refactor: update emitter parameter type to be EmitterLike
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 25, 2023
1 parent fa0eff4 commit c7bcb24
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
3 changes: 1 addition & 2 deletions providers/mail_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import { configProvider } from '@adonisjs/core'
import { RuntimeException } from '@poppinss/utils'
import type { Emitter } from '@adonisjs/core/events'
import type { ApplicationService } from '@adonisjs/core/types'

import { MailManager, Mailer, Message } from '../index.js'
Expand Down Expand Up @@ -61,7 +60,7 @@ export default class MailProvider {
)
}

return new MailManager(emitter as unknown as Emitter<MailEvents>, config)
return new MailManager(emitter, config)
})

this.app.container.bind(Mailer, async (resolver) => {
Expand Down
4 changes: 2 additions & 2 deletions src/fake_mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import string from '@poppinss/utils/string'
import { AssertionError } from 'node:assert'
import type { Emitter } from '@adonisjs/core/events'
import type { EmitterLike } from '@adonisjs/core/types/events'
import type { SentMessageInfo } from 'nodemailer/lib/json-transport/index.js'

import { Mailer } from './mailer.js'
Expand Down Expand Up @@ -511,7 +511,7 @@ export class FakeMailer extends Mailer<JSONTransport> implements MailerContract<
mails = new MailsCollection()
messages = new MessagesCollection()

constructor(name: string, emitter: Emitter<MailEvents>, config: MailerConfig) {
constructor(name: string, emitter: EmitterLike<MailEvents>, config: MailerConfig) {
super(name, new JSONTransport(), emitter, config)
super.setMessenger({
queue: async (mail, sendConfig) => {
Expand Down
6 changes: 3 additions & 3 deletions src/mail_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { RuntimeException } from '@poppinss/utils'
import type { Emitter } from '@adonisjs/core/events'
import type { EmitterLike } from '@adonisjs/core/types/events'

import debug from './debug.js'
import { Mailer } from './mailer.js'
Expand All @@ -28,7 +28,7 @@ import type {
* their lifecycle and switch between them.
*/
export class MailManager<KnownMailers extends Record<string, MailManagerTransportFactory>> {
#emitter: Emitter<MailEvents>
#emitter: EmitterLike<MailEvents>

/**
* Messenger to use on all mailers created
Expand All @@ -47,7 +47,7 @@ export class MailManager<KnownMailers extends Record<string, MailManagerTranspor
#mailersCache: Partial<Record<keyof KnownMailers, Mailer<MailTransportContract>>> = {}

constructor(
emitter: Emitter<MailEvents>,
emitter: EmitterLike<MailEvents>,
public config: MailerConfig & {
default?: keyof KnownMailers
mailers: KnownMailers
Expand Down
8 changes: 4 additions & 4 deletions src/mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/

import type { Emitter } from '@adonisjs/core/events'
import type { EmitterLike } from '@adonisjs/core/types/events'

import debug from './debug.js'
import { Message } from './message.js'
Expand All @@ -32,7 +32,7 @@ export class Mailer<Transport extends MailTransportContract> implements MailerCo
/**
* Reference to AdonisJS application emitter
*/
#emitter: Emitter<MailEvents>
#emitter: EmitterLike<MailEvents>

/**
* Messenger to use for queuing emails
Expand All @@ -42,8 +42,8 @@ export class Mailer<Transport extends MailTransportContract> implements MailerCo
constructor(
public name: string,
public transport: Transport,
emitter: Emitter<MailEvents>,
public config: MailerConfig
emitter: EmitterLike<MailEvents>,
public config: MailerConfig = {}
) {
this.#emitter = emitter
this.#messenger = new MemoryQueueMessenger(this, this.#emitter)
Expand Down
6 changes: 3 additions & 3 deletions src/messengers/memory_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import fastq, { type done } from 'fastq'
import type { Emitter } from '@adonisjs/core/events'
import type { EmitterLike } from '@adonisjs/core/types/events'

import debug from '../debug.js'
import type { MailResponse } from '../mail_response.js'
Expand Down Expand Up @@ -43,7 +43,7 @@ function sendEmail(
* emails within memory and send them in the chunks of 10
*/
export class MemoryQueueMessenger implements MailerMessenger {
#emitter: Emitter<MailEvents>
#emitter: EmitterLike<MailEvents>
#queue = fastq(this, sendEmail, 10)
#jobCompletedCallback?: (error: Error | null, result: MailResponse<unknown>) => void = (
error
Expand All @@ -58,7 +58,7 @@ export class MemoryQueueMessenger implements MailerMessenger {

constructor(
public mailer: MailerContract<MailTransportContract>,
emitter: Emitter<MailEvents>
emitter: EmitterLike<MailEvents>
) {
this.#emitter = emitter
}
Expand Down
8 changes: 4 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
*/

import type { TlsOptions } from 'node:tls'
import { SendMailOptions } from 'nodemailer'
import type { SendMailOptions } from 'nodemailer'
import type { SESClientConfig } from '@aws-sdk/client-ses'
import type { ConfigProvider } from '@adonisjs/core/types'
import type MimeNode from 'nodemailer/lib/mime-node/index.js'

import { BaseMail } from './base_mail.js'
import type { Message } from './message.js'
import { MailManager } from './mail_manager.js'
import { MailResponse } from './mail_response.js'
import type { BaseMail } from './base_mail.js'
import type { MailManager } from './mail_manager.js'
import type { MailResponse } from './mail_response.js'

/**
* Shape of the envelope node after the email has been
Expand Down

0 comments on commit c7bcb24

Please sign in to comment.