mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[ADD] runtime/utils: optional validation to EventBus
Currently the event bus allows sending and listening to arbitrary events, I got got by that when I pushed a fix using `addEventListener` on a bus across an events renaming, and on the other side the fix did nothing anymore. Entirely my fault, but if the list of events sent on a bus is known and documented (e.g. a jsdoc has `@emits` tags) it would make sense for both the listening and the dispatching to also be validated. This proposal performs validation only when the eventbus is created: - in dev mode (which also requires being in a component context) - if an iterable of events is passed to the ctor The dev-mode check might be overkill but it seems like a good idea at least for an initial version, as the validation does have a cost however low, and validation errors can occur essentially anywhere.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { OwlError } from "../common/owl_error";
|
||||
import { ComponentNode, getCurrent } from "./component_node";
|
||||
export type Callback = () => void;
|
||||
|
||||
/**
|
||||
@@ -81,10 +82,52 @@ export function validateTarget(target: HTMLElement | ShadowRoot) {
|
||||
}
|
||||
|
||||
export class EventBus extends EventTarget {
|
||||
constructor(events?: string[]) {
|
||||
if (events) {
|
||||
let node: ComponentNode | null = null;
|
||||
try {
|
||||
node = getCurrent();
|
||||
} catch {}
|
||||
if (node?.app?.dev) {
|
||||
return new DebugEventBus(events);
|
||||
}
|
||||
}
|
||||
super();
|
||||
}
|
||||
trigger(name: string, payload?: any) {
|
||||
this.dispatchEvent(new CustomEvent(name, { detail: payload }));
|
||||
}
|
||||
}
|
||||
class DebugEventBus extends EventBus {
|
||||
private events: Set<string>;
|
||||
constructor(events: string[]) {
|
||||
super();
|
||||
this.events = new Set(events);
|
||||
}
|
||||
addEventListener(
|
||||
type: string,
|
||||
listener: EventListenerOrEventListenerObject | null,
|
||||
options?: boolean | AddEventListenerOptions
|
||||
): void {
|
||||
if (!this.events.has(type)) {
|
||||
throw new OwlError(`EventBus: subscribing to unknown event '${type}'`);
|
||||
}
|
||||
super.addEventListener(type, listener, options);
|
||||
}
|
||||
trigger(name: string, payload?: any) {
|
||||
if (!this.events.has(name)) {
|
||||
throw new OwlError(`EventBus: triggering unknown event '${name}'`);
|
||||
}
|
||||
super.trigger(name, payload);
|
||||
}
|
||||
|
||||
dispatchEvent(event: Event): boolean {
|
||||
if (!this.events.has(event.type)) {
|
||||
throw new OwlError(`EventBus: dispatching unknown event '${event.type}'`);
|
||||
}
|
||||
return super.dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
export function whenReady(fn?: any): Promise<void> {
|
||||
return new Promise(function (resolve) {
|
||||
|
||||
Reference in New Issue
Block a user