mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-04 00:56:01 +00:00
refactored mutator to listen for returned data from event listeners
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
export type EventClass = {
|
||||
new (params: any): Event<any, any>;
|
||||
slug: string;
|
||||
};
|
||||
|
||||
export abstract class Event<Params = any, Returning = void> {
|
||||
_returning!: Returning;
|
||||
|
||||
@@ -9,7 +14,9 @@ export abstract class Event<Params = any, Returning = void> {
|
||||
params: Params;
|
||||
returned: boolean = false;
|
||||
|
||||
validate(value: Returning): Event<Params, Returning> | void {}
|
||||
validate(value: Returning): Event<Params, Returning> | void {
|
||||
throw new EventReturnedWithoutValidation(this as any, value);
|
||||
}
|
||||
|
||||
protected clone<This extends Event<Params, Returning> = Event<Params, Returning>>(
|
||||
this: This,
|
||||
@@ -39,3 +46,13 @@ export class InvalidEventReturn extends Error {
|
||||
super(`Expected "${expected}", got "${given}"`);
|
||||
}
|
||||
}
|
||||
|
||||
export class EventReturnedWithoutValidation extends Error {
|
||||
constructor(
|
||||
event: EventClass,
|
||||
public data: any
|
||||
) {
|
||||
// @ts-expect-error slug is static
|
||||
super(`Event "${event.constructor.slug}" returned without validation`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user