UNPKG

@fullstory/react-native

Version:
83 lines (82 loc) 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FSPage = void 0; var _reactNative = require("react-native"); var _utils = require("./utils"); const FullStory = _utils.isTurboModuleEnabled ? require('./NativeFullStory').default : _reactNative.NativeModules.FullStory; const { startPage, endPage, updatePage } = FullStory; class FSPage { constructor(pageName, properties = {}) { this.pageName = pageName; this.properties = properties; this.nonce = ''; this.cleanProperties(); } static FS_PAGE_NAME_KEY = 'pageName'; static isObject(value) { return value && typeof value === 'object' && !Array.isArray(value); } static merge(oldValue, newValue) { // We can only merge dictionaries and do not perform recursion whenever we // encounter a non-dictionary value // (see com.fullstory.instrumentation FSPageImpl.java) if (!FSPage.isObject(oldValue) || !FSPage.isObject(newValue)) { return newValue; } return FSPage.mergeObjects(oldValue, newValue); } static mergeObjects(oldObj, newObj) { // return new object instance on immutable "old" object frozen by RN const mergedObj = { ...oldObj }; for (const key in newObj) { const oldInnerValue = oldObj[key]; if (oldObj[key]) { mergedObj[key] = FSPage.merge(oldInnerValue, newObj[key]); } else { mergedObj[key] = newObj[key]; } } return mergedObj; } cleanProperties() { if (this.properties && this.properties[FSPage.FS_PAGE_NAME_KEY]) { delete this.properties[FSPage.FS_PAGE_NAME_KEY]; console.warn(`${FSPage.FS_PAGE_NAME_KEY} is a reserved property and has been removed.`); } } update(properties) { if (!this.nonce) { console.error('Called `updateProperties` on FSPage that has not been `start`-ed. This may ' + 'be a mistake. `updateProperties` should be called on the same FSPage ' + 'instance that the corresponding `start` is called on.'); return; } this.properties = FSPage.merge(this.properties, properties); this.cleanProperties(); updatePage(this.nonce, this.properties); } start(properties) { if (properties) { this.properties = FSPage.merge(this.properties, properties); this.cleanProperties(); } this.nonce = (0, _utils.generateUUID)(); startPage(this.nonce, this.pageName, this.properties); } end() { if (!this.nonce) { console.error('Called `end` on FSPage that has not been `start`-ed. `end` should be ' + 'called on the same FSPage instance that the corresponding `start` is ' + 'called on.'); return; } endPage(this.nonce); this.nonce = ''; } } exports.FSPage = FSPage; //# sourceMappingURL=FSPage.js.map