Skip to content
GitSiteEmail

Sometimes it is crucial to synchronize updates between different parts of the UI. One way to achieve this is by using JavaScript custom events, which facilitate communication regardless of the source context.

GamefaceUI provides a tool called EventBus to enable such communication.

The EventBus is a utility class that allows components to communicate with each other. It can be used to send and receive events across components without the need to pass props through the component tree.

The EventBus class can be imported and used in any component.

import eventBus from '@components/tools/EventBus/EventBus';
const Button = () => {
const handleClick = () => {
eventBus.emit('button-clicked', 'Button was clicked');
};
return (
<button onClick={handleClick}>Click Me</button>
);
};
export default Button;
import { onMount } from "solid-js";
import Button from './Button';
import eventBus from '@components/tools/EventBus/EventBus';
const App = () => {
const handleClickButton = (data: string) => {
console.log(data);
};
onMount(() => {
eventBus.on('button-clicked', handleClickButton);
});
onCleanup(() => {
eventBus.off('button-clicked', handleClickButton);
});
return (
<Button />
);
};
export default App;

The EventCallback type defines a function that is called when an event is emitted.

type EventCallback = (...args: any[]) => any;

The LogLevel type represents the logging level of the event bus.

type LogLevel = 'warn' | 'none';

The emit method sends an event to all registered listeners.

eventBus.emit(eventName: string, ...args: any[]): void
Parameter Type Description
eventName string The name of the event to emit
...args any[] The arguments to send to the listeners

The on method registers a listener for a specific event.

eventBus.on(eventName: string, callback: EventCallback): void
Parameter Type Description
eventName string The name of the event to listen for
callback EventCallback The function to call when the event is emitted

The off method removes a listener for a specific event.

eventBus.off(eventName: string, callback: EventCallback): void
Parameter Type Description
eventName string The name of the event to stop listening for
callback EventCallback The callback that was passed to on

The once method registers a listener that is removed automatically after it fires. It returns a cleanup function that removes the listener.

eventBus.once(eventName: string, callback: EventCallback): () => void
Parameter Type Description
eventName string The name of the event to listen for
callback EventCallback The function to call when the event is emitted

The hasRegistered method checks if an event has been registered or if a specific callback is associated with it.

eventBus.hasRegistered(eventName: string, callback?: EventCallback): boolean
Parameter Type Description
eventName string The name of the event to check
callback EventCallback The callback function to check (optional)

The logLevel property sets the logging level of the event bus.

eventBus.logLevel: LogLevel

The default log level is 'warn'. To disable warning logs, set the logLevel property to 'none'.

eventBus.logLevel = 'none';

To listen for an event, use the on method of the EventBus class.

const callback = (data: string) => {
console.log(data);
};
onMount(() => {
eventBus.on('button-clicked', callback);
});

To emit an event, use the emit method of the EventBus class.

const handleClick = () => {
eventBus.emit('button-clicked');
};

To stop listening for an event, use the off method of the EventBus class.

const callback = (data: string) => {
console.log(data);
};
onMount(() => {
eventBus.on('button-clicked', callback);
});
onCleanup(() => {
eventBus.off('button-clicked', callback);
});

You can register multiple listeners for the same event.

const callback = (data: string) => {
console.log(data);
};
const callback2 = (data: string) => {
console.log(data);
};
onMount(() => {
eventBus.on('button-clicked', callback);
eventBus.on('button-clicked', callback2);
});
onCleanup(() => {
eventBus.off('button-clicked', callback);
eventBus.off('button-clicked', callback2);
});

You can register a listener that is triggered only once.

onMount(() => {
eventBus.once('button-clicked', (data: string) => {
console.log(data);
});
});

This is particularly useful when using arrow functions, as they will be automatically removed after being triggered.

If the component can unmount before the event fires, remove the listener in onCleanup. Otherwise it stays registered and keeps the component in memory. Pass the function once returns:

onMount(() => {
onCleanup(eventBus.once('button-clicked', (data: string) => {
console.log(data);
}));
});

off can’t remove a once listener, because once registers a wrapper around your callback.

You can check if an event or a specific callback has been registered using the hasRegistered method.

For example you can:

  • Avoid registering the same event multiple times:
const callback = (data: string) => {
console.log(data);
};
onMount(() => {
if (!eventBus.hasRegistered('button-clicked', callback)) {
eventBus.on('button-clicked', callback);
}
});
  • Avoid emitting an event if it has no listeners:
const handleClick = () => {
if (eventBus.hasRegistered('button-clicked')) {
eventBus.emit('button-clicked', 'Button was clicked');
}
};
  • Clean up an event listener if it has been registered:
const callback = (data: string) => {
console.log(data);
};
onMount(() => {
if (eventBus.hasRegistered('button-clicked', callback)) {
eventBus.off('button-clicked', callback);
}
});

If an event is not registered and something goes wrong, EventBus will log a warning message to the console.