Skip to content

eventBus.once

The once method allows to listen to an event 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 event emission.

Example

To listen to the event until it fires once by the Event Bus you can use the once method:

useStorefront(async (storefront) => {
    storefront.eventBus.once('example-event', () => {
        console.log('Listening to the event...')
    });
});

Example

You can have multiple listeners for every event:

useStorefront(async (storefront) => {
    storefront.eventBus.on('example-event', () => {
        console.log('Listening to the event...')
    });

    storefront.eventBus.on('example-event', () => {
        console.log('Listening to the event twice...')
    });

    storefront.eventBus.once('example-event', () => {
        console.log('Listening to the event thrice...')
    });
});

Methods reference