2020-04-14 22:29:11 +02:00
|
|
|
/* eslint-disable no-param-reassign */
|
2021-04-22 10:07:10 +02:00
|
|
|
import timer from './timer';
|
2019-08-04 11:10:51 +02:00
|
|
|
|
|
|
|
// wrapTimer keeps track of the timing of a async operation and emits
|
|
|
|
// a event on the given eventBus once the operation is complete
|
|
|
|
//
|
|
|
|
// the returned function is designed to be used as a .then(<func>) argument.
|
|
|
|
// It transparently passes the data to the following .then(<func>)
|
|
|
|
//
|
|
|
|
// usage: promise.then(wrapTimer(bus, type, { payload: 'ok' }))
|
2021-04-22 10:07:10 +02:00
|
|
|
const wrapTimer: (EventEmitter, string, object) => (any) => any = (
|
|
|
|
eventBus,
|
|
|
|
event,
|
|
|
|
args = {},
|
|
|
|
) => {
|
2019-08-04 11:10:51 +02:00
|
|
|
const t = timer.new();
|
2021-08-12 15:04:37 +02:00
|
|
|
return (data) => {
|
2019-08-04 11:10:51 +02:00
|
|
|
args.time = t();
|
|
|
|
eventBus.emit(event, args);
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const metricsHelper = {
|
|
|
|
wrapTimer,
|
|
|
|
};
|
|
|
|
export default metricsHelper;
|
2019-08-04 11:10:51 +02:00
|
|
|
module.exports = {
|
|
|
|
wrapTimer,
|
|
|
|
};
|