400-881-9892

文档中心

官方文档,可查阅产品介绍、快速入门、用户指南、开发指南、API参考、SDK参考、帮助等信息。

文档中心 互动课堂

Event Dispatching Class

    Event dispatching class(EventDispatcher)

    • The event subscription and dispatch mechanism is the core mechanism for the operation of the room, and most of the communication between room functions is completed through this mechanism. EventDispatcher is the core implementation of this mechanism.
    EventDispatcher
    • This method instantiates an EventDispatcher object, which controls the listening and dispatching of various events within the room.
    • Method Example
    1
    var dispatcher = TK.EventDispatcher();
    
    dispatchEvent
    • This method dispatches a specific event to all listeners
    Parameter Name Required Type Description
    eventData Yes Object Event description object. Detailed object properties are described below
    log No Boolean Whether to print logs (at debug level), defaults to true
    • eventData
    Parameter Name Type Description
    type String Event type, corresponding to the eventType passed when registering the event listener
    ... any Other attributes can be defined and added as needed. Listeners will receive the entire event object. By unpacking it according to the encapsulation format, the data carried by the event can be obtained
    • Method Example
    1
    2
    3
    var event = {
        type : dispatch-test,
        data : this is event data};dispatcher.dispatchEvent(event);
    
    addEventListener
    • This method adds a listener (callback function) for a specific type of event to the event dispatcher. When the event is triggered, the event dispatcher will notify (invoke) this listener
    Parameter Name Required Type Description
    eventType Yes String Event type. Corresponds to the type property of the event dispatched by the dispatchEvent method
    listener Yes Function Event listener callback function. This function will be triggered after the event is dispatched
    • Method Example
    1
    2
    const listener1 = e => console.log(e.data)
    dispatcher.addEventListener(dispatch-test, listener1);
    
    removeEventListener
    • This method removes a listener for a specific type of event. After removal, the event dispatcher will no longer notify this listener when the event is dispatched
    Parameter Name Required Type Description
    eventType Yes String Event type. Corresponds to the type property of the event dispatched by the dispatchEvent method
    listener Yes Function Event listener. After removal, this function will no longer be triggered
    • Method Example
    1
    dispatcher.removeEventListener(‘dispatch-test’, listener1);
    
    removeAllEventListener
    • This method removes all listeners for the specified event
    Parameter Name Required Type Description
    eventTypeArray Yes Array Array of event types
    • Method Example
    1
    const removeListenerTypeArray = ['dispatch-test', 'aaa', 'bbb']dispatcher.removeAllEventListener(removeListenerTypeArray);