You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.1 KiB
36 lines
1.1 KiB
import {Event} from './types'
|
|
import {Container, StaticInstantiable} from '../../di'
|
|
import {ErrorWithContext} from '../../util'
|
|
|
|
export function getEventName<T extends Event>(eventKey: StaticInstantiable<T>): string {
|
|
const protoName = eventKey.prototype.eventName
|
|
if ( protoName ) {
|
|
return protoName
|
|
}
|
|
|
|
try {
|
|
const inst = Container.getContainer().make<T>(eventKey)
|
|
if ( inst.eventName ) {
|
|
return inst.eventName
|
|
}
|
|
} catch (e: unknown) {} // eslint-disable-line no-empty
|
|
|
|
let stringParseName = eventKey.toString()
|
|
.split('\n')
|
|
.map(x => x.trim())
|
|
.filter(x => x.startsWith('this.eventName = \'') || x.startsWith('this.eventName = "'))?.[0]
|
|
?.split('=')?.[1]
|
|
?.trim()
|
|
|
|
if ( stringParseName ) {
|
|
stringParseName = stringParseName.endsWith(';') ? stringParseName.slice(1, -2) : stringParseName.slice(1, -1)
|
|
if ( stringParseName ) {
|
|
return stringParseName
|
|
}
|
|
}
|
|
|
|
throw new ErrorWithContext('Unable to determine eventName from eventKey', {
|
|
eventKey,
|
|
})
|
|
}
|