owtlab-tracking
Version:
A simple Tracking system
57 lines (48 loc) • 1.52 kB
JavaScript
import { deepExtend } from './utils/deepExtend';
import each from './core/utils/each';
export function extendEvent(eventCollection, eventModifier) {
if (
arguments.length !== 2 ||
typeof eventCollection !== 'string' ||
(typeof eventModifier !== 'object' && typeof eventModifier !== 'function')
) {
handleValidationError.call(
this,
'Incorrect arguments provided to #extendEvent method',
);
return;
}
this.extensions.collections[eventCollection] =
this.extensions.collections[eventCollection] || [];
this.extensions.collections[eventCollection].push(eventModifier);
this.emit('extendEvent', eventCollection, eventModifier);
return this;
}
export function extendEvents(eventsModifier) {
if (
arguments.length !== 1 ||
(typeof eventsModifier !== 'object' && typeof eventsModifier !== 'function')
) {
handleValidationError.call(
this,
'Incorrect arguments provided to #extendEvents method',
);
return;
}
this.extensions.events.push(eventsModifier);
this.emit('extendEvents', eventsModifier);
return this;
}
function handleValidationError(message) {
this.emit('error', `Event(s) not extended: ${message}`);
}
export function getExtendedEventBody(result, queue) {
if (queue && queue.length > 0) {
each(queue, (eventModifier, i) => {
const modifierResult =
typeof eventModifier === 'function' ? eventModifier() : eventModifier;
deepExtend(result, modifierResult);
});
}
return result;
}