this repo has no description
at main 213 lines 7.9 kB view raw
1"use strict"; 2Object.defineProperty(exports, "__esModule", { value: true }); 3exports.MetricsLocationTracker = void 0; 4const validation = require("../../json/validation"); 5const optional_1 = require("../../types/optional"); 6const metricsUtil = require("./util"); 7/** 8 * A type describing metrics location tracker. 9 * 10 * The tracker manages stack of metrics location items. 11 */ 12class MetricsLocationTracker { 13 // endregion 14 // region Initialization 15 /** 16 * Create new metrics location tracker with all required attributes. 17 * @param rootPosition - Current root position of the tracker. 18 * @param locations - Array of metrics location to track. 19 */ 20 constructor(rootPosition = 0, locations = []) { 21 this.rootPosition = rootPosition; 22 this.locationStack = locations.map((location) => new MetricsLocationStackItem(location)); 23 } 24 // endregion 25 // region Location Stack Management 26 /** 27 * Check whether location stack is empty or not. 28 */ 29 get isEmpty() { 30 return this.locationStack.length === 0; 31 } 32 /** 33 * Push new location to location tracker's stack. 34 * @param location - Location to push to stack. 35 */ 36 pushLocation(location) { 37 this.locationStack.push(new MetricsLocationStackItem(location)); 38 } 39 /** 40 * Pop location from location tracker's stack. 41 */ 42 popLocation() { 43 var _a; 44 if (this.locationStack.length === 0) { 45 validation.unexpectedType("ignoredValue", "non-empty location stack", "empty location stack"); 46 return null; 47 } 48 return (_a = this.locationStack.pop()) === null || _a === void 0 ? void 0 : _a.location; 49 } 50 /** 51 * Returns tracker's current position. 52 */ 53 get currentPosition() { 54 const stackItem = this.lastStackItem; 55 if ((0, optional_1.isSome)(stackItem)) { 56 return stackItem.position; 57 } 58 else { 59 return this.rootPosition; 60 } 61 } 62 /** 63 * Set current position of tracker. 64 * This is necessary when large today modules are broken apart into multipart shelves. 65 * We need to preserve the position of content within server-response, not our logical shelves. 66 * @param position - Position to set to. 67 */ 68 setCurrentPosition(position) { 69 const stackItem = this.lastStackItem; 70 if ((0, optional_1.isSome)(stackItem)) { 71 stackItem.position = position; 72 } 73 else { 74 this.rootPosition = position; 75 } 76 } 77 /** 78 * Advance tracker's position. 79 */ 80 nextPosition() { 81 const stackItem = this.lastStackItem; 82 if ((0, optional_1.isSome)(stackItem)) { 83 stackItem.position += 1; 84 } 85 else { 86 this.rootPosition += 1; 87 } 88 } 89 /** 90 * Convert location tracker's stack items to array of metric location objects. 91 */ 92 get stackItemsToLocations() { 93 return this.locationStack.map((stackItem) => stackItem.location); 94 } 95 /** 96 * Returns last stack item in location stack or `null` if stack is empty. 97 */ 98 get lastStackItem() { 99 const length = this.locationStack.length; 100 if (length === 0) { 101 return null; 102 } 103 return this.locationStack[length - 1]; 104 } 105 // endregion 106 // region Adding Location 107 /** 108 * Create new basic location and add it to existing locations of the location tracker 109 * and return resulting array of locations. 110 * @param options - Base metrics options which include location tracker to get current locations from. 111 * @param title - New location title. 112 */ 113 static locationsByAddingBasicLocation(options, title) { 114 const locations = options.locationTracker.stackItemsToLocations; 115 locations.push(MetricsLocationTracker.buildBasicLocation(options, title)); 116 return locations; 117 } 118 /** 119 * Create new content location and add it to existing locations of the location tracker 120 * and return resulting array of locations. 121 * @param options - Content metrics options which include location tracker to get current locations from. 122 * @param title - New location title. 123 */ 124 static locationsByAddingContentLocation(options, title) { 125 const locations = options.locationTracker.stackItemsToLocations; 126 locations.push(MetricsLocationTracker.buildContentLocation(options, title)); 127 return locations; 128 } 129 // endregion 130 // region Metrics Options 131 /** 132 * Create new basic location from base metrics options 133 * and push it to the stack of location tracker included into options. 134 * @param options - Base metrics options which include location tracker to push new location to. 135 * @param title - Location title. 136 */ 137 static pushBasicLocation(options, title) { 138 options.locationTracker.pushLocation(MetricsLocationTracker.buildBasicLocation(options, title)); 139 } 140 /** 141 * Create new content location from content metrics options 142 * and push it to the stack of location tracker included into options. 143 * @param options - Content metrics options which include location tracker to push new location to. 144 * @param title - Location title. 145 */ 146 static pushContentLocation(options, title) { 147 options.locationTracker.pushLocation(MetricsLocationTracker.buildContentLocation(options, title)); 148 } 149 /** 150 * Pop last location from location tracker contained in metrics options. 151 * @param options - Metrics options containing the location tracker. 152 */ 153 static popLocation(options) { 154 return options.locationTracker.popLocation(); 155 } 156 // endregion 157 // region Location Builders 158 static buildBasicLocation(options, title) { 159 let name = title; 160 if ((0, optional_1.isSome)(options.anonymizationOptions)) { 161 name = options.anonymizationOptions.anonymizationString; 162 } 163 const location = { 164 locationPosition: options.locationTracker.currentPosition, 165 locationType: metricsUtil.targetTypeForMetricsOptions(options), 166 name: name, 167 }; 168 if ((0, optional_1.isSome)(options.recoMetricsData)) { 169 Object.assign(location, options.recoMetricsData); 170 } 171 return location; 172 } 173 static buildContentLocation(options, title) { 174 const base = MetricsLocationTracker.buildBasicLocation(options, title); 175 // Use the location tracker if there is no id override. 176 if ((0, optional_1.isNothing)(options.id)) { 177 base.idType = "sequential" /* MetricsIDType.sequential */; 178 base.id = options.locationTracker.currentPosition.toString(); 179 } 180 else { 181 // If there is a id specified, use that. 182 base.idType = metricsUtil.idTypeForMetricsOptions(options); 183 let id = options.id; 184 if ((0, optional_1.isSome)(options.anonymizationOptions)) { 185 id = options.anonymizationOptions.anonymizationString; 186 } 187 base.id = id; 188 } 189 if ((0, optional_1.isSome)(options.fcKind)) { 190 base.fcKind = options.fcKind; 191 } 192 if ((0, optional_1.isSome)(options.displayStyle)) { 193 base.displayStyle = options.displayStyle; 194 } 195 return base; 196 } 197} 198exports.MetricsLocationTracker = MetricsLocationTracker; 199/** 200 * A type describing a metrics location item in location tracking stack. 201 */ 202class MetricsLocationStackItem { 203 /** 204 * Create new metrics location stack item with all required attributes. 205 * @param location - The metrics location associated with this item. 206 * @param position - The position of the item. 207 */ 208 constructor(location, position = 0) { 209 this.location = location; 210 this.position = position; 211 } 212} 213//# sourceMappingURL=location.js.map