Files
discord-decomp/modules/gateway/GatewaySocketDispatcher.tsx
T
2026-11-01 12:12:55 +02:00

312 lines
10 KiB
TypeScript

// Metro module 12700 (f12701).
import { DISPATCHER_IDEAL_TIME_LIMIT_MS } from "./DispatcherWorkConstants";
import Logger from "../debug/Logger";
import { createDispatcherWorkScheduler } from "./DispatcherWorkScheduler.native";
import { createResumeAnalytics, getReadyPayloadByteSizeAnalytics, logReadyPayloadReceived, logResumeAnalytics } from "./GatewaySocketAnalytics";
import { WorkSchedulerTelemetryTiming, WorkSchedulerTelemetryMeasurement } from "./WorkSchedulerTelemetry";
import ConnectionState from "./ConnectionState";
import Module589 from "../../_bundle/Module589";
import ActionBatcher from "./ActionBatcher";
let c8750_5 = new Logger("GatewaySocket");
let c8750_6 = new Set(["INITIAL_GUILD", "READY"]);
let c8750_7 = new Set(["READY", "INITIAL_GUILD"]);
let c8750_8 = new Set(["READY", "READY_SUPPLEMENTAL", "RESUMED"]);
let c8750_9 = new Set(["READY", "INITIAL_GUILD", "READY_SUPPLEMENTAL", "RESUMED", "VOICE_CHANNEL_SELECT", "VOICE_STATE_UPDATE", "VOICE_SERVER_UPDATE", "RTC_CONNECTION_STATE", "RTC_CONNECTION_VIDEO", "RTC_CONNECTION_CLIENT_CONNECT", "RTC_CONNECTION_PING", "MEDIA_SESSION_JOINED", "MEDIA_ENGINE_PERMISSION", "SESSIONS_REPLACE", "STREAM_CREATE", "STREAM_SERVER_UPDATE", "STREAM_DELETE", "STREAM_UPDATE"]);
let c8750_10 = {
NotStarted: 0,
0: "NotStarted",
Loading: 1,
1: "Loading",
Loaded: 2,
2: "Loaded"
};
let c8750_11 = {};
class GatewaySocketDispatcher {
scheduler = createDispatcherWorkScheduler();
queue = [];
paused = true;
resumeAnalytics = createResumeAnalytics();
getDispatchHandler = null;
flush = (a0) => {
let Loaded2, status2;
if (this.paused) {
return true;
}
const r10 = performance.now();
const r5 = 0 < this.queue.length;
let r2 = 0;
let r24 = this;
if (r5) {
const status = this.queue[0].status;
const Loaded = c8750_10.Loaded;
let r0 = 0;
r2 = 0;
r24 = this;
if (status === Loaded) {
do {
const r1 = r0 + 1;
const length = this.queue.length;
r2 = r1;
r24 = this;
if (!(r1 < length)) {
break;
}
status2 = this.queue[r1].status;
Loaded2 = c8750_10.Loaded;
r0 = r1;
r2 = r0;
r24 = this;
} while (status2 === Loaded2);
}
}
if (r2 === 0) {
return true;
}
const r40 = r24.queue.splice(0, r2);
const r42 = r24.dispatchMultiple(r40, a0);
if (r42) {
r24.scheduler.telemetry.timeEnd(WorkSchedulerTelemetryTiming.TIME_TO_QUEUE_EMPTY);
}
const r15 = performance.now() - r10;
if (r15 > DISPATCHER_IDEAL_TIME_LIMIT_MS && !r42) {
c8750_5.log(`Dispatched ${r40.length} messages in ${r15}ms`);
}
return r42;
};
constructor(a0) {
this.socket = a0;
}
hasStuffToDispatchNow() {
return this.queue.length > 0 && this.queue[0].status === c8750_10.Loaded;
}
processFirstQueuedDispatch(a0) {
const r3 = [];
if (this.queue.length > 0 && a0.has(this.queue[0].type)) {
while (this.queue[0].status === c8750_10.Loaded) {
r3.push(this.queue.shift());
if (!(this.queue.length > 0)) {
break;
}
if (!a0.has(this.queue[0].type)) {
break;
}
}
}
this.dispatchMultiple(r3);
}
unpauseDispatchQueue() {
this.paused = false;
for (const item of this.queue) {
this.maybePreload(item);
}
this.flush();
}
receiveDispatch(a0, a1, a2) {
if (this.getDispatchHandler == null) {
throw Error("getDispatchHandler needs to be passed in first!");
}
const r4 = {
data: a0,
type: a1,
compressionAnalytics: a2,
status: c8750_10.NotStarted,
preloadPromise: null,
preloadedData: null
};
this.queue.push(r4);
if (!this.maybePreload(r4)) {
this.scheduleFlush(a1);
}
}
maybePreload(a0) {
let c19980_0 = a0;
if (this.paused && !c8750_6.has(a0.type)) {
return false;
}
if (a0.status === c8750_10.NotStarted) {
const dispatchHandler = this.getDispatchHandler(a0.type);
const preloadPromise = dispatchHandler == null ? void 0 : dispatchHandler.preload(a0.data);
a0.status = preloadPromise == null ? c8750_10.Loaded : c8750_10.Loading;
a0.preloadPromise = preloadPromise;
if (preloadPromise != null) {
preloadPromise.then((result) => {
c19980_0.preloadedData = result;
c19980_0.status = c8750_10.Loaded;
this.scheduleFlush(c19980_0.type);
}).catch((a0) => this.socket.resetSocketOnDispatchError({
error: a0,
action: c19980_0.type
}));
return true;
}
}
return false;
}
scheduleFlush(a0) {
if (!this.paused) {
const r9 = c8750_7.has(a0);
const scheduler = this.scheduler;
if (r9) {
scheduler.clearWorkTimeout();
this.flush();
} else if (!scheduler.hasWorkScheduled) {
this.scheduler.requestWorkTimeout(this.flush);
}
if (c8750_9.has(a0)) {
this.scheduler.markCriticalWorkScheduled();
}
}
}
getDispatchTimings() {
return c8750_11;
}
getSchedulerTelemetry() {
return this.scheduler.telemetry;
}
getIsSchedulerBackgrounded() {
return this.scheduler.isBackgrounded;
}
toggleRequestIdleCallback(a0) {
this.scheduler.toggleRequestIdleCallback(a0);
}
getIsRequestIdleCallbackEnabled() {
return this.scheduler.isRequestIdleCallbackEnabled;
}
dispatchMultiple(a0, c19981_2) {
let c19981_0, c19981_5;
if (a0.length === 0) {
return true;
}
const type = "none";
let c19981_4 = false;
this.scheduler.telemetry.measure(WorkSchedulerTelemetryMeasurement.COUNT_INITIAL_DISPATCHS_LENGTH, a0.length);
L9: {
try {
c19981_5 = [];
if (this.socket.connectionState === ConnectionState.RESUMING) {
Module589.Emitter.pause(150);
}
c19981_0 = 0;
Module589.Emitter.batched(() => {
let length;
const r8 = 0 < a0.length;
let r1 = 0;
if (r8) {
do {
const r14 = a0[r1];
type = r14.type;
let r25 = c19981_4;
const r0 = r1;
if (!r25) {
r25 = c8750_8.has(r14.type);
}
c19981_4 = r25;
const r28 = performance.now();
this.dispatchOne(r14);
c19981_0 = performance.now() - r28;
const type2 = r14.type;
const r22 = c19981_0;
let r38 = c8750_11[type2];
if (r38 == null) {
r38 = [0, 0];
}
const [r41, r42] = r38;
c8750_11[type2] = [(r41 * r42 + r22) / (r42 + 1), r42 + 1];
const r7 = c19981_2 == null;
let r49 = false;
if (!r7) {
const r50 = a0[r0];
const r52 = a0.length - 1;
const r53 = r0 < r52 ? a0[r0 + 1] : null;
let r55 = c19981_2 == null ? void 0 : c19981_2.timeRemaining();
if (r55 == null) {
r55 = 0;
}
const type3 = r50.type;
const type4 = r53?.type;
r49 = c19981_2 != null && r55 <= 0 && !(type3 === type4) && r0 !== r52;
}
const r61 = r0 + 1;
if (r49) {
c19981_5 = a0.slice(r61);
if (!(c19981_2 != null && c19981_2.timeRemaining() <= 0)) {
break;
}
this.scheduler.telemetry.timeTrack(WorkSchedulerTelemetryTiming.TIME_OVER_DEADLINE, c19981_2.timeSinceExpiration);
break;
}
length = a0.length;
r1 = r61;
} while (r1 < length);
}
ActionBatcher.flush();
});
if (c19981_4) {
Module589.Emitter.resume();
}
if (c19981_5.length > 0) {
this.scheduler.telemetry.measure(WorkSchedulerTelemetryMeasurement.COUNT_DISPATCHES_LEFT_AFTER_YIELD, c19981_5.length);
const queue = this.queue;
queue.unshift.apply(queue, [...c19981_5]);
this.scheduler.requestWorkTimeout(this.flush, true);
break L9;
}
} catch ($e) {
this.socket.resetSocketOnDispatchError({
error: $e,
action: type
});
}
return true;
}
return false;
}
dispatchOne({ data, type: type2, compressionAnalytics, preloadedData }) {
const lastUpdateTime = performance.now();
if (this.socket.connectionState === ConnectionState.RESUMING) {
const initialWaitTime = lastUpdateTime - this.resumeAnalytics.lastUpdateTime;
if (this.resumeAnalytics.numEvents === 0) {
this.resumeAnalytics.initialWaitTime = initialWaitTime;
} else if (initialWaitTime > this.resumeAnalytics.largestWaitTime) {
this.resumeAnalytics.largestWaitTime = initialWaitTime;
}
const resumeAnalytics = this.resumeAnalytics;
resumeAnalytics.totalWaitTime = resumeAnalytics.totalWaitTime + initialWaitTime;
this.resumeAnalytics.lastUpdateTime = lastUpdateTime;
const resumeAnalytics2 = this.resumeAnalytics;
resumeAnalytics2.numEvents = resumeAnalytics2.numEvents + 1;
}
ActionBatcher.flush(type2, data);
if (type2 === "READY") {
const readyPayloadByteSizeAnalytics = getReadyPayloadByteSizeAnalytics(data);
const dispatchHandler3 = this.getDispatchHandler(type2);
if (dispatchHandler3 != null) {
dispatchHandler3.dispatch(data, type2, preloadedData);
}
logReadyPayloadReceived(this.socket, data, lastUpdateTime, compressionAnalytics, readyPayloadByteSizeAnalytics);
} else if (type2 === "RESUMED") {
const dispatchHandler2 = this.getDispatchHandler(type2);
if (dispatchHandler2 != null) {
dispatchHandler2.dispatch(data, type2, preloadedData);
}
logResumeAnalytics(this.resumeAnalytics);
this.socket.handleResumeDispatched();
this.resumeAnalytics = createResumeAnalytics();
} else {
const dispatchHandler = this.getDispatchHandler(type2);
if (dispatchHandler != null) {
dispatchHandler.dispatch(data, type2, preloadedData);
}
}
if (this.socket.connectionState === ConnectionState.RESUMING) {
const resumeAnalytics3 = this.resumeAnalytics;
resumeAnalytics3.dispatchTime = resumeAnalytics3.dispatchTime + (performance.now() - lastUpdateTime);
}
}
clear() {
this.paused = false;
this.queue.length = 0;
}
}
export default GatewaySocketDispatcher;