Refactor event bus and queue system; detect cycles in DI realization and make
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-01-26 19:37:54 -06:00
parent 506fb55c74
commit 6d1cf18680
69 changed files with 1673 additions and 720 deletions

View File

@@ -3,9 +3,9 @@ import {QueryResult} from '../types'
import {SQLDialect} from '../dialect/SQLDialect'
import {AppClass} from '../../lifecycle/AppClass'
import {Inject, Injectable} from '../../di'
import {EventBus} from '../../event/EventBus'
import {QueryExecutedEvent} from './event/QueryExecutedEvent'
import {Schema} from '../schema/Schema'
import {Bus} from '../../support/bus'
/**
* Error thrown when a connection is used before it is ready.
@@ -25,7 +25,7 @@ export class ConnectionNotReadyError extends ErrorWithContext {
@Injectable()
export abstract class Connection extends AppClass {
@Inject()
protected bus!: EventBus
protected readonly bus!: Bus
constructor(
/**
@@ -82,6 +82,6 @@ export abstract class Connection extends AppClass {
*/
protected async queryExecuted(query: string): Promise<void> {
const event = new QueryExecutedEvent(this.name, this, query)
await this.bus.dispatch(event)
await this.bus.push(event)
}
}

View File

@@ -1,67 +1,17 @@
import {Event} from '../../../event/Event'
import {Inject, Injectable} from '../../../di'
import {InvalidJSONStateError, JSONState} from '../../../util'
import {Connection} from '../Connection'
import {DatabaseService} from '../../DatabaseService'
import {BaseEvent} from '../../../support/bus'
/**
* Event fired when a query is executed.
*/
@Injectable()
export class QueryExecutedEvent extends Event {
@Inject()
protected database!: DatabaseService
/**
* The name of the connection where the query was executed.
* @protected
*/
public connectionName!: string
/**
* The connection where the query was executed.
*/
public connection!: Connection
/**
* The query that was executed.
*/
public query!: string
export class QueryExecutedEvent extends BaseEvent {
constructor(
connectionName?: string,
connection?: Connection,
query?: string,
public readonly connectionName: string,
public readonly connection: Connection,
public readonly query: string,
) {
super()
if ( connectionName ) {
this.connectionName = connectionName
}
if ( connection ) {
this.connection = connection
}
if ( query ) {
this.query = query
}
}
async dehydrate(): Promise<JSONState> {
return {
connectionName: this.connectionName,
query: this.query,
}
}
rehydrate(state: JSONState): void {
if ( !state.connectionName || !state.query ) {
throw new InvalidJSONStateError('Missing connectionName or query from QueryExecutedEvent state.')
}
this.query = String(state.query)
this.connectionName = String(state.connectionName)
this.connection = this.database.get(this.connectionName)
}
eventName = '@extollo/lib.QueryExecutedEvent'
}

View File

@@ -0,0 +1,38 @@
import {BaseSerializer} from '../../../support/bus'
import {QueryExecutedEvent} from './QueryExecutedEvent'
import {Awaitable, JSONState} from '../../../util'
import {Container, Inject, Injectable} from '../../../di'
import {DatabaseService} from '../../DatabaseService'
import {ObjectSerializer} from '../../../support/bus/serial/decorators'
export interface QueryExecutedEventSerialPayload extends JSONState {
connectionName: string
query: string
}
@ObjectSerializer()
@Injectable()
export class QueryExecutedEventSerializer extends BaseSerializer<QueryExecutedEvent, QueryExecutedEventSerialPayload> {
@Inject()
protected readonly injector!: Container
protected decodeSerial(serial: QueryExecutedEventSerialPayload): Awaitable<QueryExecutedEvent> {
const connection = this.injector.make<DatabaseService>(DatabaseService).get(serial.connectionName)
return new QueryExecutedEvent(serial.connectionName, connection, serial.query)
}
protected encodeActual(actual: QueryExecutedEvent): Awaitable<QueryExecutedEventSerialPayload> {
return {
connectionName: actual.connectionName,
query: actual.query,
}
}
protected getName(): string {
return '@extollo/lib.QueryExecutedEventSerializer'
}
matchActual(some: QueryExecutedEvent): boolean {
return some instanceof QueryExecutedEvent
}
}