mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
add bus class
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
type Callback = (...args: any[]) => void;
|
||||
|
||||
interface Subscription {
|
||||
owner: any;
|
||||
callback: Callback;
|
||||
}
|
||||
|
||||
export default class Bus {
|
||||
private subscriptions: { [eventType: string]: Subscription[] } = {};
|
||||
|
||||
on(eventType: string, owner: any, callback: Callback) {
|
||||
if (!this.subscriptions[eventType]) {
|
||||
this.subscriptions[eventType] = [];
|
||||
}
|
||||
this.subscriptions[eventType].push({
|
||||
owner,
|
||||
callback
|
||||
});
|
||||
}
|
||||
off(eventType: string, owner: any) {
|
||||
const subs = this.subscriptions[eventType];
|
||||
if (subs) {
|
||||
this.subscriptions[eventType] = subs.filter(s => s.owner !== owner);
|
||||
}
|
||||
}
|
||||
trigger(eventType: string, ...args: any[]) {
|
||||
const subs = this.subscriptions[eventType] || [];
|
||||
for (let sub of subs) {
|
||||
sub.callback(...args);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user