Files
discord-decomp/_bundle/Module1164.js
T
2026-11-01 12:12:55 +02:00

248 lines
8.8 KiB
JavaScript

// Metro module 1164 (f1165).
import { AppState } from "react-native";
import { timestampInSeconds, debug, spanToJSON, getRootSpan } from "./Module817";
import { isRootSpan } from "./Module1122";
import { isNearToNow, getLatestChildSpanEndTimestamp, setSpanMeasurement } from "./createSpanJSON";
import { STALL_COUNT, STALL_TOTAL_TIME, STALL_LONGEST_TIME } from "./Module1159";
export const stallTrackingIntegration = function(r3 = {}) {
let minimumStallThresholdMs = r3.minimumStallThresholdMs;
if (minimumStallThresholdMs === void 0) {
minimumStallThresholdMs = 50;
}
let c12871_2, _onSpanStart2, _onSpanEnd2, _onChildSpanEnd2, _markSpanFinish2, _getCurrentStats2, _startTracking2, _shouldStopTracking2, _reset2, _flushLeakedTransactions2;
let c12871_0 = minimumStallThresholdMs;
c12871_2 = void 0;
_onSpanStart2 = void 0;
_onSpanEnd2 = void 0;
_onChildSpanEnd2 = void 0;
_markSpanFinish2 = void 0;
_getCurrentStats2 = void 0;
_startTracking2 = void 0;
_shouldStopTracking2 = void 0;
_reset2 = void 0;
_flushLeakedTransactions2 = void 0;
let c12871_1 = new Map();
const _internalState = {
isTracking: false,
timeout: null,
isBackground: false,
lastIntervalMs: 0,
totalStallTime: 0,
stallCount: 0,
backgroundEventListener: (a0) => {
if (a0 === "active") {
c12871_2.isBackground = false;
if (c12871_2.timeout != null) {
c12871_2.lastIntervalMs = 1000 * timestampInSeconds();
c12871_2.iteration();
}
} else {
c12871_2.isBackground = true;
if (c12871_2.timeout !== null) {
clearTimeout(c12871_2.timeout);
}
}
},
iteration: () => {
const lastIntervalMs = 1000 * timestampInSeconds();
const r44 = lastIntervalMs - c12871_2.lastIntervalMs;
if (r44 >= 50 + c12871_0) {
const r10 = r44 - 50;
c12871_2.stallCount = c12871_2.stallCount + 1;
c12871_2.totalStallTime = c12871_2.totalStallTime + r10;
for (const item of c12871_1.entries()) {
const [r61, r62] = item;
const r17 = Math;
const longestStallTime = r62.longestStallTime;
c12871_1.set(r61, {
...r62,
longestStallTime: r17.max(longestStallTime !== null ? longestStallTime !== void 0 ? longestStallTime : 0 : 0, r10)
});
}
}
c12871_2.lastIntervalMs = lastIntervalMs;
if (c12871_2.isTracking && !c12871_2.isBackground) {
c12871_2.timeout = setTimeout(c12871_2.iteration, 50);
}
}
};
c12871_2 = _internalState;
_onSpanStart2 = (a0) => {
if (isRootSpan(a0)) {
if (c12871_1.has(a0)) {
debug.error("[StallTracking] Tried to start stall tracking on a transaction already being tracked. Measurements might be lost.");
} else {
if (!c12871_2.isTracking) {
c12871_2.isTracking = true;
c12871_2.lastIntervalMs = Math.floor(1000 * timestampInSeconds());
c12871_2.iteration();
}
const r42 = c12871_1.get(a0);
const r44 = r42 !== null ? r42 !== void 0 ? r42.longestStallTime : void 0 : void 0;
c12871_1.set(a0, {
longestStallTime: 0,
atTimestamp: null,
atStart: {
stall_count: {
value: c12871_2.stallCount,
unit: "none"
},
stall_total_time: {
value: c12871_2.totalStallTime,
unit: "millisecond"
},
stall_longest_time: {
value: r44 !== null ? r44 !== void 0 ? r44 : 0 : 0,
unit: "millisecond"
}
}
});
_flushLeakedTransactions2();
}
}
};
_onSpanEnd2 = (a0) => {
let r142;
if (isRootSpan(a0)) {
const r90 = c12871_1.get(a0);
if (r90) {
const timestamp2 = spanToJSON(a0).timestamp;
if (isNearToNow(timestamp2)) {
const r154 = c12871_1.get(a0);
const r156 = r154 !== null ? r154 !== void 0 ? r154.longestStallTime : void 0 : void 0;
r142 = {
stall_count: {
value: c12871_2.stallCount,
unit: "none"
},
stall_total_time: {
value: c12871_2.totalStallTime,
unit: "millisecond"
},
stall_longest_time: {
value: r156 !== null ? r156 !== void 0 ? r156 : 0 : 0,
unit: "millisecond"
}
};
} else {
const latestChildSpanEndTimestamp = getLatestChildSpanEndTimestamp(a0);
if (latestChildSpanEndTimestamp !== timestamp2) {
debug.log("[StallTracking] Stall measurements not added due to a custom `endTimestamp` (root end is not equal to the latest child span end).");
}
if (!r90.atTimestamp) {
debug.log("[StallTracking] Stall measurements not added due to `endTimestamp` not being close to now. And no previous stats from child end were found.");
}
r142 = latestChildSpanEndTimestamp === timestamp2 && r90.atTimestamp ? r90.atTimestamp.stats : void 0;
}
c12871_1.delete(a0);
if (c12871_1.size === 0) {
c12871_2.isTracking = false;
if (c12871_2.timeout !== null) {
clearTimeout(c12871_2.timeout);
c12871_2.timeout = null;
}
c12871_2.stallCount = 0;
c12871_2.totalStallTime = 0;
c12871_2.lastIntervalMs = 0;
c12871_1.clear();
}
if (r142) {
setSpanMeasurement(a0, STALL_COUNT, r142.stall_count.value - r90.atStart.stall_count.value, r90.atStart.stall_count.unit);
setSpanMeasurement(a0, STALL_TOTAL_TIME, r142.stall_total_time.value - r90.atStart.stall_total_time.value, r90.atStart.stall_total_time.unit);
setSpanMeasurement(a0, STALL_LONGEST_TIME, r142.stall_longest_time.value, r142.stall_longest_time.unit);
} else if (timestamp2 !== void 0) {
debug.log("[StallTracking] Stall measurements not added due to `endTimestamp` not being close to now.", "endTimestamp", timestamp2, "now", timestampInSeconds());
}
return void 0;
}
debug.log("[StallTracking] Stall measurements were not added to transaction due to exceeding the max count.");
c12871_1.delete(a0);
if (c12871_1.size === 0) {
c12871_2.isTracking = false;
if (c12871_2.timeout !== null) {
clearTimeout(c12871_2.timeout);
c12871_2.timeout = null;
}
c12871_2.stallCount = 0;
c12871_2.totalStallTime = 0;
c12871_2.lastIntervalMs = 0;
c12871_1.clear();
}
return void 0;
}
const rootSpan = getRootSpan(a0);
const timestamp = spanToJSON(a0).timestamp;
if (timestamp) {
const r15 = c12871_1.get(rootSpan);
if (r15) {
if (Math.abs(timestampInSeconds() - timestamp) > 0.02) {
debug.log("[StallTracking] Span end not logged due to end timestamp being outside the margin of error from now.");
if (r15.atTimestamp && r15.atTimestamp.timestamp < timestamp) {
c12871_1.set(rootSpan, {
...r15,
atTimestamp: null
});
}
} else {
const r16 = Object;
const r53 = {
...r15
};
const r62 = c12871_1.get(rootSpan);
const r63 = r62 !== null ? r62 !== void 0 ? r62.longestStallTime : void 0 : void 0;
c12871_1.set(rootSpan, r16.assign(r53, {
atTimestamp: {
timestamp,
stats: {
stall_count: {
value: c12871_2.stallCount,
unit: "none"
},
stall_total_time: {
value: c12871_2.totalStallTime,
unit: "millisecond"
},
stall_longest_time: {
value: r63 !== null ? r63 !== void 0 ? r63 : 0 : 0,
unit: "millisecond"
}
}
}
}));
}
}
}
};
_onChildSpanEnd2 = (a0) => {};
_markSpanFinish2 = (a0, a1) => {};
_getCurrentStats2 = (a0) => {};
_startTracking2 = () => {};
_shouldStopTracking2 = () => {};
_reset2 = () => {};
_flushLeakedTransactions2 = () => {
if (c12871_1.size > 10) {
let r24 = 0;
const r4 = c12871_1.size - 10;
for (const item of c12871_1.keys()) {
if (!(r24 >= r4)) {
r24 = r24 + 1;
c12871_1.delete(item);
continue;
}
break;
}
}
};
if (AppState == null ? void 0 : AppState.isAvailable) {
AppState.addEventListener("change", _internalState.backgroundEventListener);
}
return {
name: "StallTracking",
setup: (a0) => {
a0.on("spanStart", _onSpanStart2);
a0.on("spanEnd", _onSpanEnd2);
},
_internalState
};
};