docs(api-ref): enhance event hub documentation on Strapi class

This commit is contained in:
nathan-pichon 2023-04-03 11:35:08 +02:00
parent 0f9506877b
commit e477b52196
No known key found for this signature in database

View File

@ -105,7 +105,25 @@ Wrapper around [FS NodeJS module](https://nodejs.org/docs/latest-v18.x/api/fs.ht
### `strapi.eventHub`
- [<Type>EventHub</Type>](EventHub)
Module to manipulate events around Strapi project. It is an instance of [EventEmitter](https://nodejs.org/docs/latest-v18.x/api/events.html#class-eventemitter) from NodeJS.
The `strapi.eventHub` object is used to manipulate events within a Strapi project. It is an instance of the built-in EventEmitter class from Node.js, which provides a simple way to emit and listen for events.
The `strapi.eventHub` object is created using the `createEventHub()` function in the [EventHub](EventHub) module of the Strapi core. This function returns a new instance of the EventHub class, which extends the EventEmitter class and adds some additional functionality specific to Strapi.
#### Examples:
```javascript
// Listen for a 'user.updated' event and log the data
strapi.eventHub.on('user.updated', (data) => {
console.log(`User ${data.id} has been updated`);
});
// Emit a 'user.created' event with some data
strapi.eventHub.emit('user.created', { username: 'johndoe', email: 'johndoe@example.com' });
```
In this example, we are emitting a `user.created` event with some data attached to it, and then listening for a user.updated event and logging the data. These events can be used to trigger actions within the Strapi application or to communicate with external systems.
For more information on how to use the EventEmitter class and its methods, see the [Node.js documentation](ttps://nodejs.org/docs/latest-v18.x/api/events.html#class-eventemitter).
### `strapi.startupLogger`
- [<Type>StartupLogger</Type>](StartupLogger)