Search…
Eventrix
Method
attributes
description
constructor
initialState, eventsReceivers
Eventrix class constructor
listen
eventName, listener
Add event listener
unlisten
eventName, listener
Remove event listener
emit
eventName, eventData
Emit event for eventrix instance it will run listeners and events receivers
getState
path
Get eventrix state
useReceiver
eventsReceiver
Register events receiver in eventrix instance it will be used on events emitted
removeReceiver
eventsReceiver
Unregister events receiver from eventrix instance

Example of simple use of Eventrix

Javascript
Typescript
import { Eventrix, EventsReceiver } from 'eventrix';
const usersEventsReceiver = new EventsReceiver(
'createUserEventName',
(eventName, eventData, stateManager) => {
const user = eventData.user;
const users = stateManager.getState('usersList');
stateManager.setState('usersList', [user, ...users]);
}
);
const initialState = {
usersList: [],
}
const eventsReceivers = [
usersEventsReceiver,
];
const eventrix = new Eventrix(initialState, eventsReceivers);
export default eventrix;
import { Eventrix, EventsReceiver } from 'eventrix';
import StateManager from 'eventrix/types/StateManager';
interface User {
id: number;
name: string;
age: number;
}
interface UserEventData {
user: User;
}
interface StoreState {
usersList: User[];
}
const usersEventsReceiver = new EventsReceiver<UserEventData, void>(
'createUserEventName',
(eventName, eventData, stateManager) => {
const user = eventData.user;
const users = stateManager.getState('usersList');
stateManager.setState('usersList', [user, ...users]);
}
);
const initialState = {
usersList: [],
}
const eventsReceivers = [
usersEventsReceiver,
];
const eventrix = new Eventrix<StoreState>(initialState, eventsReceivers);
export default eventrix;
When You emit event with name "createUserEventName" usersEventsReceiver will be invoked and change state of "usersList".
Javascript
Second Tab
const user = {
id: 1,
name: 'Max',
age: 30,
}
eventrix.emit('createUserEventName', { user });
const user: User = {
id: 1,
name: 'Max',
age: 30,
}
eventrix.emit<UserEventData>('createUserEventName', { user });
Last modified 7mo ago
Copy link