2020-04-14 22:29:11 +02:00
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
|
2019-08-04 11:10:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
2019-08-04 11:15:40 +02:00
|
|
|
const timer = require('./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' }))
|
|
|
|
const wrapTimer = (eventBus, event, args = {}) => {
|
|
|
|
const t = timer.new();
|
|
|
|
return data => {
|
|
|
|
args.time = t();
|
|
|
|
eventBus.emit(event, args);
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
wrapTimer,
|
|
|
|
};
|