Standard libraries that lift up your code.
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.
 
 
 

110 lines
2.9 KiB

import {Awaitable, JSONState, Maybe, Pipeline, TypeTag, uuid4} from '../../util'
import {Instantiable, StaticInstantiable} from '../../di'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface SerialPayload<TActual, TSerial extends JSONState> extends JSONState {
serializer: string
payload: TSerial
}
export interface Serializer<TActual, TSerial extends JSONState> {
matchActual(some: TActual): boolean
matchSerial(serial: SerialPayload<TActual, TSerial>): boolean
encode(actual: TActual): Awaitable<SerialPayload<TActual, TSerial>>
decode(serial: SerialPayload<TActual, TSerial>): Awaitable<TActual>
}
export interface Event {
eventUuid: string
eventName: string
originBusUuid?: string
shouldBroadcast?: boolean | (() => Awaitable<boolean>)
}
export abstract class BaseEvent implements Event {
public readonly eventUuid = uuid4()
public abstract eventName: string
public shouldBroadcast(): Awaitable<boolean> {
return true
}
}
export type EventHandlerReturn = Awaitable<boolean | void | undefined>
export type EventHandler<T extends Event> = (event: T) => EventHandlerReturn
export interface EventHandlerSubscription {
unsubscribe(): Awaitable<void>
}
export interface Queueable {
execute(): Awaitable<void>
shouldQueue?: boolean | (() => boolean)
defaultQueue?: string | (() => string)
}
export type ShouldQueue<T extends Queueable> = T & TypeTag<'@extollo/lib:ShouldQueue'>
export function shouldQueue<T extends Queueable>(something: T): something is ShouldQueue<T> {
if ( typeof something.shouldQueue === 'function' ) {
return something.shouldQueue()
}
return (
typeof something.shouldQueue === 'undefined'
|| something.shouldQueue
)
}
export interface EventBus<TEvent extends Event = Event> {
readonly uuid: string
subscribe<T extends TEvent>(eventKey: StaticInstantiable<T>, handler: EventHandler<T>): Awaitable<EventHandlerSubscription>
pipe<T extends TEvent>(eventKey: StaticInstantiable<T>, line: Pipeline<T, EventHandlerReturn>): Awaitable<EventHandlerSubscription>
push(event: TEvent): Awaitable<void>
up(): Awaitable<void>
down(): Awaitable<void>
isConnected(): boolean
}
/** Internal storage format for local event bus subscribers. */
export interface BusSubscriber<T extends Event> {
uuid: string
eventName: string
eventKey: StaticInstantiable<T>
handler: EventHandler<T>
}
export interface BusQueue {
readonly name: string
dispatch<T extends Queueable>(item: T): Promise<void>
pop(): Promise<Maybe<Queueable>>
}
export interface RedisBusConnectorConfig {
type: 'redis'
}
export type BusConnectorConfig = RedisBusConnectorConfig
export interface QueueConfig {
driver?: Instantiable<BusQueue>,
/* queues?: ({
name: string,
driver: Instantiable<BusQueue>,
})[] */
}