Skip to main content
Version: v3

EventBus

Start from API_LEVEL 2.0 . Please refer to API_LEVEL.

EventBus is a utility class that provides event publishing/subscribing, an implementation of the publish-subscribe pattern.

Methods

on

Adds the listener function to the end of the listeners array for the event named eventName

on(eventName: string, listener: (...args: any[]) => void): void

off

Removes the specified listener from the listener array for the event named eventName

off(eventName: string, listener: (...args: any[]) => void): void

emit

Triggers the listener functions for the event named eventName

emit(eventName: string, ...args: any[]): void

once

Adds a one-time listener function for the event named eventName

once(eventName: string, listener: (...args: any[]) => void): void

clear

Removes all listeners, or those of the specified eventName

clear(): void

count

Gets the number of registered event listeners corresponding to eventName. If eventName is not passed, get the number of registered eventName types

count(eventName?: string): number

Example

import { EventBus } from '@zos/utils'

const eventBus = new EventBus()

eventBus.on('data', (data) => {
console.log(data)
})

eventBus.emit('data', 'Hello Zepp OS!')