UI5 EventBus

180 阅读1分钟

Question

ui5的getEventBus底层是怎么实现的?效率怎么样?

Answer

No special implementation, just normal subscriber - publisher design pattern.
Event subscriber gets EventBus instance via

sap.ui.getCore().getEventBus()

Subscriber code uses API subscribe to register the event handler provided by application with a given event.
In my example, my event handler is function onOrderApproved, and the event I would like to listen to is “OrderApproved”.

clipboard1
The event registration is done based on channel id.
clipboard2
In EventBus singleton instance there is a central repository mEventRegistery which is actually a map: key is event id, value is an array containing all listeners for this event.

clipboard3
When the event publisher has event to raise,
clipboard4
it simply goes through the previously mentioned event registry mEventRegistery,
clipboard5
and:
clipboard6
this is how the event handler defined in application is called:
clipboard7
regarding the performance, as you see this implementation is quite lean and no additional runtime overhead is there, performance should be quite good.