Event System
Event System used for handling events between components
Event System is a package used for handling different events between parts of code written in Typescript.
EventsSystem
new EventSystem(options: EventsSystemOptions)
Initializes the Event System class.
Parameters:
- options
bufferDirection {"FIFO" | "LIFO"} - The direction of the events buffer. Defaults to "FIFO".
bufferSize {number} - The size of the buffer.
Example:
const eventSystem = new EventSystem();
const eventSystem = new EventSystem({
bufferDirection: "LIFO",
bufferSize: 10
});
subscribe(event, handler)
Subscribes handler to be called when the event is triggered.
Parameters:
event {keyof E} The event name.
handler {Function} The handler function.
Example:
type MyEvents = {
"event": () => void;
}
const eventSystem = new EventSystem<MyEvents>();
eventSystem.subscribe("event", () => {
});
unsubscribe(event, handler)
Unsubscribes the handler from the event.
Parameters:
event {keyof E} The event name.
handler {Function} The handler function.
Example:
type MyEvents = {
"event": () => void;
}
const eventSystem = new EventSystem<MyEvents>();
eventSystem.subscribe("event", () => {
});
eventSystem.unsubscribe("event", () => {
});
notify(event, ...args)
Triggers the event with the given arguments.
Parameters:
event {keyof E} The event name.
args {Parameters<E[keyof E]>} The arguments to pass to the handler.
Example:
type MyEvents = {
"event": (a: number, b: string) => void;
}
const eventSystem = new EventSystem<MyEvents>();
eventSystem.subscribe("event", (a, b) => {
});
eventSystem.notify("event", 1, "Hello");