@datadog/mobile-react-native
Version:
A client-side React Native module to interact with Datadog
65 lines (61 loc) • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.EventMapper = void 0;
var _InternalLog = require("../../InternalLog");
var _SdkVerbosity = require("../../SdkVerbosity");
var _DdSdk = require("../../sdk/DdSdk");
var _AttributesSingleton = require("../AttributesSingleton/AttributesSingleton");
var _UserInfoSingleton = require("../UserInfoSingleton/UserInfoSingleton");
var _deepClone = require("./utils/deepClone");
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
/**
* Generic class for applying event mappers.
*
* Calls params in this order: formatRawEventForMapper, eventMapper, formatMapperEventForNative.
*
* @param eventMapper the user-registered event mapper
* @param formatRawEventForMapper formatter that gets the raw event (from javascript call) and returns the input given to the mapper
* @param formatMapperEventForNative formatter that gets the output of the mapper, and returns the input given to the native SDKs
* @param formatRawEventForNative called when no event mapper is registered
*/
class EventMapper {
constructor(eventMapper, formatRawEventForMapper, formatMapperEventForNative, formatRawEventForNative) {
this.eventMapper = eventMapper;
this.formatRawEventForMapper = formatRawEventForMapper;
this.formatMapperEventForNative = formatMapperEventForNative;
this.formatRawEventForNative = formatRawEventForNative;
}
applyEventMapper = rawEvent => {
if (!this.eventMapper) {
return this.formatRawEventForNative(rawEvent);
}
// formatting
const userInfo = _UserInfoSingleton.UserInfoSingleton.getInstance().getUserInfo();
const attributes = _AttributesSingleton.AttributesSingleton.getInstance().getAttributes();
const initialEvent = this.formatRawEventForMapper(rawEvent, {
userInfo,
attributes
});
// mapper
const backupEvent = (0, _deepClone.deepClone)(initialEvent);
try {
const mappedEvent = this.eventMapper(initialEvent);
if (!mappedEvent) {
return null;
}
return this.formatMapperEventForNative(mappedEvent, backupEvent);
} catch (error) {
_InternalLog.InternalLog.log(`The event mapper crashed when mapping ${JSON.stringify(backupEvent)}: ${error}`, _SdkVerbosity.SdkVerbosity.WARN);
_DdSdk.DdSdk.telemetryDebug('Error while running the event mapper');
return this.formatMapperEventForNative(backupEvent, backupEvent);
}
};
}
exports.EventMapper = EventMapper;
//# sourceMappingURL=EventMapper.js.map