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.
An unsubscribe function to remove the listener.
const unsubscribe = events.on('afterStart', ({ app }) => {
app.getLogger().info('Hello world!');
unsubscribe();
});
The literal type of the event, to generate the type of the listener.
The listener function.
Adds an event listener that will only be executed once.
An unsubscribe function to remove the listener.
events.once('afterStart', ({ app }) => {
app.getLogger().info('Hello world!');
unsubscribe();
});
The literal type of the event, to generate the type of the listener.
The listener function.
Asynchronously reduces a target using an event. It's like emit, but the event
listeners return a modified (or not) version of the target
.
A version of the target
processed by the listeners.
// 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.
Synchronously reduces a target using an event. It's like emit, but the events
listener return a modified (or not) version of the target
.
A version of the target
processed by the listeners.
// 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.
Generated using TypeDoc
The
events
service Jimpex uses. This is an alternative declaration of theEventsHub
class that uses the interfaces and types from this project.