Emits an event and call all its listeners.
// Extend the interface to type the payload.
interface JimpexEvents {
myEvent: { message: string };
}
// Add the listener.
events.on('myEvent', ({ message }) => {
console.log('Event received:', message);
});
// Trigger the event.
events.emit('myEvent', { message: 'Hello' });
// prints "Event received: Hello"
The literal type of the event, to generate the type of the payload.
The name of the events.
Context information for the event.
Adds a new event listener.
The literal type of the event, to generate the type of the listener.
The listener function.
An unsubscribe function to remove the listener.
Adds an event listener that will only be executed once.
The literal type of the event, to generate the type of the listener.
The listener function.
An unsubscribe function to remove the listener.
Asynchronously reduces a target using an event. It's like emit, but the event
listeners return a modified (or not) version of the target
.
// Extend the interface to type the target.
interface JimpexReducerEventTargets {
myReducer: unknown[];
}
// Extend the interface to type the payload.
interface JimpexReducerEventPayloads {
myReducer: {
message: string;
};
}
// Add the reducer.
events.on('myReducer', async (target, { message }) => {
const data = await fetch('https://api.example.com/' + message);
target.push(data);
return target;
});
// Trigger the event.
const result = await events.reduce('myReducer', [], { message: 'Hello' });
// result would be a list of data fetched from the API.
The literal type of the event, to generate the types of the target and the payload.
The name of the event.
The variable to reduce with the reducers/listeners.
Context information for the event.
A version of the target
processed by the listeners.
Synchronously reduces a target using an event. It's like emit, but the events
listener return a modified (or not) version of the target
.
// Extend the interface to type the target.
interface JimpexReducerEventTargets {
myReducer: string[];
}
// Extend the interface to type the payload.
interface JimpexReducerEventPayloads {
myReducer: {
message: string;
};
}
// Add the reducer.
events.on('myReducer', (target, { message }) => {
target.push(message);
return target;
});
// Trigger the event.
events.reduce('event', [], 'Hello');
// returns ['Hello']
The literal type of the event, to generate the types of the target and the payload.
The name of the event.
The variable to reduce with the reducers/listeners.
Context information for the event.
A version of the target
processed by the listeners.
The
events
service Jimpex uses. This is an alternative declaration of theEventsHub
class that uses the interfaces and types from this project.