queryBus.once¶
The once method allows to listen to an query only until it fires once. Then the listener is being removed.
Input parameters¶
messageName¶
messageName is a mandatory parameter of the string type which represents the name of the message to listen to.
listener¶
listener is a mandatory parameter of type TMessageListener which represents a callback for the query emission.
Example¶
To listen to the query until it fires once by the Query Bus you can use the once method:
useStorefront(async (storefront) => {
storefront.queryBus.once('example-query', () => {
console.log('Listening to the query...')
});
});
Example¶
Remember that the query bus does not allow two listeners:
useStorefront(async (storefront) => {
storefront.queryBus.once('example-query', () => {
console.log('Listening to the query...')
});
storefront.queryBus.once('example-query', () => { // Error: Maximum number of listeners reached
console.log('Listening to the query second time...')
});
});
Listening with on and once at the same time is also not allowed:
useStorefront(async (storefront) => {
storefront.queryBus.once('example-query', () => {
console.log('Listening to the query...')
});
storefront.queryBus.on('example-query', () => { // Error: Maximum number of listeners reached
console.log('Listening to the query with on...')
});
});