Skip to content

Commit

Permalink
refactor: do not use import.meta.resolve API
Browse files Browse the repository at this point in the history
The API is unustable and recently had a major breaking change. So we
are going to get rid of it everywhere
  • Loading branch information
thetutlage committed Jan 5, 2024
1 parent a05b775 commit 7c462a8
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 90 deletions.
9 changes: 6 additions & 3 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
import is from '@sindresorhus/is'
import type { Application } from '@adonisjs/application'
import Emittery, { type UnsubscribeFunction } from 'emittery'
import { moduleExpression, moduleCaller, moduleImporter } from '@adonisjs/fold'
import { moduleCaller, moduleImporter } from '@adonisjs/fold'

import debug from './debug.js'
import { EventsBuffer } from './events_buffer.js'
import type {
Listener,
LazyImport,
EmitterLike,
Constructor,
ListenerMethod,
AllowedEventTypes,
ListenerClassWithHandleMethod,
EmitterLike,
} from './types.js'

/**
Expand Down Expand Up @@ -132,7 +132,10 @@ export class Emitter<EventsList extends Record<string | symbol | number, any>>
* Parse string based listener
*/
if (typeof listener === 'string') {
return moduleExpression(listener, this.#app.appRoot.toString()).toCallable(
const parts = listener.split('.')
const method = parts.length === 1 ? 'handle' : parts.pop()!
const moduleRefId = parts.join('.')
return moduleImporter(() => this.#app.import(moduleRefId), method).toCallable(
this.#app.container
)
}
Expand Down
3 changes: 2 additions & 1 deletion src/events_buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
*/

import is from '@sindresorhus/is'
import string from '@poppinss/utils/string'
import { AssertionError } from 'node:assert'

import type { AllowedEventTypes, BufferedEvent, BufferedEventsList, Constructor } from './types.js'
import string from '@poppinss/utils/string'

/**
* Callback function to narrow down an event from
Expand Down
8 changes: 4 additions & 4 deletions tests/base_event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const BASE_URL = new URL('./app/', import.meta.url)
test.group('Base event', () => {
test('dispatch event using the event class', async ({ assert }) => {
const stack: UserRegistered[] = []
const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

class UserRegistered extends BaseEvent {}
Expand All @@ -32,7 +32,7 @@ test.group('Base event', () => {

test('pass event arguments via dispatch method', async ({ assert }) => {
const stack: UserRegistered[] = []
const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

class UserRegistered extends BaseEvent {
Expand All @@ -56,7 +56,7 @@ test.group('Base event', () => {
assert,
}) => {
const stack: UserRegistered[] = []
const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

class EntityRegistered extends BaseEvent {
Expand Down Expand Up @@ -86,7 +86,7 @@ test.group('Base event', () => {
assert,
}) => {
const stack: UserRegistered[] = []
const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

class EntityRegistered extends BaseEvent {
Expand Down
50 changes: 30 additions & 20 deletions tests/emitter/emit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test.group('Emitter | emit', (group) => {
test('emit event multiple times', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.on('new:user', (data) => {
Expand All @@ -43,7 +43,7 @@ test.group('Emitter | emit', (group) => {
test('emit event for class based events', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

class UserRegistered {
Expand All @@ -62,7 +62,7 @@ test.group('Emitter | emit', (group) => {
test('validate emit types', async ({ assert, expectTypeOf }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter<{ 'new:user': NewUserEvent }>(app)

class UserRegistered {
Expand All @@ -85,7 +85,7 @@ test.group('Emitter | emit', (group) => {
})

test('raise exception when listener fails', async ({ assert }) => {
const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.on('new:user', () => {
Expand All @@ -107,7 +107,12 @@ test.group('Emitter | emit', (group) => {
`
)

const app = new Application(fs.baseUrl, { environment: 'web', importer: () => {} })
const app = new Application(fs.baseUrl, {
environment: 'web',
importer(filePath) {
return import(filePath)
},
})
const emitter = new Emitter(app)
await app.init()

Expand All @@ -126,7 +131,7 @@ test.group('Emitter | emit', (group) => {
}
}

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)
await app.init()

Expand All @@ -141,7 +146,7 @@ test.group('Emitter | emit', (group) => {
}
}

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)
await app.init()

Expand All @@ -152,7 +157,7 @@ test.group('Emitter | emit', (group) => {
test('invoke listeners serially', async ({ assert }) => {
let stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.on('new:user', async () => {
Expand Down Expand Up @@ -184,7 +189,7 @@ test.group('Emitter | emit | with error handler', (group) => {
})

test('capture error using onError handler', async ({ assert }, done) => {
const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.onError((event, error) => {
Expand Down Expand Up @@ -212,7 +217,12 @@ test.group('Emitter | emit | with error handler', (group) => {
`
)

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, {
environment: 'web',
importer(filePath) {
return import(filePath)
},
})
const emitter = new Emitter(app)
await app.init()

Expand All @@ -237,7 +247,7 @@ test.group('Emitter | emit | with error handler', (group) => {
}
}

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)
await app.init()

Expand All @@ -258,7 +268,7 @@ test.group('Emitter | emit | with error handler', (group) => {
}
}

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)
await app.init()

Expand All @@ -273,7 +283,7 @@ test.group('Emitter | emit | with error handler', (group) => {
}).waitForDone()

test('capture error using onError handler during emitSerial', async ({ assert }, done) => {
const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.onError((event, error) => {
Expand All @@ -290,7 +300,7 @@ test.group('Emitter | emit | with error handler', (group) => {
}).waitForDone()

test('throw error when no error listener is defined during emitSerial', async () => {
const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.on('new:user', () => {
Expand All @@ -305,7 +315,7 @@ test.group('Emitter | fake', () => {
test('fake event', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.on('new:user', (data) => {
Expand All @@ -322,7 +332,7 @@ test.group('Emitter | fake', () => {
test('fake event with emitSerial', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.on('new:user', (data) => {
Expand All @@ -339,7 +349,7 @@ test.group('Emitter | fake', () => {
test('faking multiple times should drop old fakes', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.on('new:user', (data) => {
Expand All @@ -363,7 +373,7 @@ test.group('Emitter | fake', () => {
test('fake all events', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.on('new:user', (data) => {
Expand All @@ -387,7 +397,7 @@ test.group('Emitter | fake', () => {
test('do not invoke "onAny" listeners when all events are faked', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.onAny((data) => {
Expand All @@ -408,7 +418,7 @@ test.group('Emitter | fake', () => {
test('invoke "onAny" listeners when some events are not faked', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

emitter.onAny((name, data) => {
Expand Down
6 changes: 3 additions & 3 deletions tests/emitter/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test.group('Class based events', () => {
test('listen for a class based event', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

class UserRegistered {
Expand All @@ -36,7 +36,7 @@ test.group('Class based events', () => {
test('attach multiple listeners for class based events', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

class UserRegistered {
Expand All @@ -59,7 +59,7 @@ test.group('Class based events', () => {
test('get listeners for a class based event', async ({ assert }) => {
const stack: any[] = []

const app = new Application(BASE_URL, { environment: 'web', importer: () => {} })
const app = new Application(BASE_URL, { environment: 'web' })
const emitter = new Emitter(app)

class UserRegistered {
Expand Down
Loading

0 comments on commit 7c462a8

Please sign in to comment.