2021-03-09 10:50:51 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getObserversByPriority } from './utils';
|
|
|
|
import type { EventObserversType } from './types';
|
2020-04-03 11:50:54 +00:00
|
|
|
|
2020-03-20 16:46:24 +00:00
|
|
|
/**
|
|
|
|
* Emits events on registered observers for the provided type and passes along
|
|
|
|
* the provided data.
|
|
|
|
*
|
|
|
|
* This event emitter will silently catch promise errors, but doesn't care
|
|
|
|
* otherwise if any errors are caused by observers. So events that do care
|
|
|
|
* should use `emitEventWithAbort` instead.
|
|
|
|
*
|
|
|
|
* @param {Object} observers The registered observers to omit to.
|
|
|
|
* @param {string} eventType The event type being emitted.
|
2021-03-09 10:50:51 +00:00
|
|
|
* @param {*} data Data passed along to the observer when it is invoked.
|
2020-03-20 16:46:24 +00:00
|
|
|
*
|
2021-03-09 10:50:51 +00:00
|
|
|
* @return {Promise} A promise that resolves to true after all observers have executed.
|
2020-03-20 16:46:24 +00:00
|
|
|
*/
|
2021-03-09 10:50:51 +00:00
|
|
|
export const emitEvent = async (
|
|
|
|
observers: EventObserversType,
|
|
|
|
eventType: string,
|
|
|
|
data: unknown
|
|
|
|
): Promise< unknown > => {
|
2020-04-03 11:50:54 +00:00
|
|
|
const observersByType = getObserversByPriority( observers, eventType );
|
2020-04-06 20:36:19 +00:00
|
|
|
const observerResponses = [];
|
2020-04-02 09:27:54 +00:00
|
|
|
for ( const observer of observersByType ) {
|
2020-03-20 16:46:24 +00:00
|
|
|
try {
|
2020-04-06 20:36:19 +00:00
|
|
|
const observerResponse = await Promise.resolve(
|
|
|
|
observer.callback( data )
|
|
|
|
);
|
|
|
|
if ( typeof observerResponse === 'object' ) {
|
|
|
|
observerResponses.push( observerResponse );
|
|
|
|
}
|
2020-03-20 16:46:24 +00:00
|
|
|
} catch ( e ) {
|
2021-03-09 10:50:51 +00:00
|
|
|
// we don't care about errors blocking execution, but will console.error for troubleshooting.
|
2020-03-20 16:46:24 +00:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error( e );
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 20:36:19 +00:00
|
|
|
return observerResponses.length ? observerResponses : true;
|
2020-03-20 16:46:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emits events on registered observers for the provided type and passes along
|
|
|
|
* the provided data. This event emitter will abort and return any value from
|
2020-07-30 10:57:22 +00:00
|
|
|
* observers that return an object which should contain a type property.
|
2020-03-20 16:46:24 +00:00
|
|
|
*
|
|
|
|
* @param {Object} observers The registered observers to omit to.
|
|
|
|
* @param {string} eventType The event type being emitted.
|
2021-03-09 10:50:51 +00:00
|
|
|
* @param {*} data Data passed along to the observer when it is invoked.
|
2020-03-20 16:46:24 +00:00
|
|
|
*
|
2021-03-09 10:50:51 +00:00
|
|
|
* @return {Promise} Returns a promise that resolves to either boolean or the return value of the aborted observer.
|
2020-03-20 16:46:24 +00:00
|
|
|
*/
|
2021-03-09 10:50:51 +00:00
|
|
|
export const emitEventWithAbort = async (
|
|
|
|
observers: EventObserversType,
|
|
|
|
eventType: string,
|
|
|
|
data: unknown
|
|
|
|
): Promise< unknown > => {
|
2020-04-03 11:50:54 +00:00
|
|
|
const observersByType = getObserversByPriority( observers, eventType );
|
2020-04-02 09:27:54 +00:00
|
|
|
for ( const observer of observersByType ) {
|
2020-03-20 16:46:24 +00:00
|
|
|
try {
|
2020-04-03 11:50:54 +00:00
|
|
|
const response = await Promise.resolve( observer.callback( data ) );
|
2021-03-09 10:50:51 +00:00
|
|
|
if ( typeof response !== 'object' || response === null ) {
|
2020-10-09 08:52:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-09 10:50:51 +00:00
|
|
|
if ( ! response.hasOwnProperty( 'type' ) ) {
|
2020-04-21 02:33:16 +00:00
|
|
|
throw new Error(
|
|
|
|
'If you want to abort event emitter processing, your observer must return an object with a type property'
|
|
|
|
);
|
|
|
|
}
|
2020-10-09 08:52:04 +00:00
|
|
|
return response;
|
2020-03-20 16:46:24 +00:00
|
|
|
} catch ( e ) {
|
2020-07-30 10:57:22 +00:00
|
|
|
// We don't handle thrown errors but just console.log for troubleshooting.
|
2020-03-20 16:46:24 +00:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error( e );
|
2020-04-21 02:33:16 +00:00
|
|
|
return { type: 'error' };
|
2020-03-20 16:46:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-09 08:52:04 +00:00
|
|
|
return true;
|
2020-03-20 16:46:24 +00:00
|
|
|
};
|