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

124 lines
4.8 KiB
TypeScript

// Metro module 12703 (f12704).
import { DISPATCHER_IDEAL_TIME_LIMIT_MS, DISPATCHER_LONG_TIMEOUT_MS, DISPATCHER_STANDARD_TIMEOUT_MS, MAX_DISPATCHER_FLUSH_DEADLINE_TRIES } from "./DispatcherWorkConstants";
import Logger from "../debug/Logger";
import { WorkSchedulerTelemetry, WorkSchedulerTelemetryEvent, WorkSchedulerTelemetryMeasurement, WorkSchedulerTelemetryTiming } from "./WorkSchedulerTelemetry";
import Module38 from "../../_bundle/Module38";
const _logger = new Logger("DispatcherWorkScheduler");
export class BasicWorkScheduler {
constructor() {
return {
__proto__: new.target.prototype,
_flushTimeoutHandler: null,
_flushIdleHandler: null,
_nextDispatchTimeout: DISPATCHER_STANDARD_TIMEOUT_MS,
_workCallbackFn: null,
_consecutiveFlushesBeforeQueueEmpty: 0,
_isBackgrounded: false,
_enableRequestIdleCallback: true,
_criticalWorkScheduled: false,
telemetry: new WorkSchedulerTelemetry(),
_logger
};
}
_trackAppBackgrounded(a0) {
if (this._isBackgrounded !== a0) {
this._isBackgrounded = a0;
if (this._isBackgrounded && this.hasWorkScheduled) {
this.telemetry.track(WorkSchedulerTelemetryEvent.SKIP_IDLE_CALLBACK_DUE_TO_BACKGROUNDED);
this._processWorkCallback();
}
}
}
_queueIdleCallback() {
throw new Error("Not implemented");
}
_clearIdleCallback() {
throw new Error("Not implemented");
}
get _hasExceededMaxConsecutiveFlushes() {
return this._consecutiveFlushesBeforeQueueEmpty >= MAX_DISPATCHER_FLUSH_DEADLINE_TRIES;
}
_processWorkCallback(a0) {
if (this._workCallbackFn != null) {
if (this._hasExceededMaxConsecutiveFlushes) {
_logger.log("Unable to fully flush work queue after max retries, skipping future deadline.");
this._workCallbackFn();
this.clearWorkTimeout();
this.telemetry.measure(WorkSchedulerTelemetryMeasurement.COUNT_FLUSH_BEFORE_QUEUE_EMPTY, this._consecutiveFlushesBeforeQueueEmpty);
this.telemetry.track(WorkSchedulerTelemetryEvent.EXCEEDED_MAX_CONSECUTIVE_FLUSHES);
this._consecutiveFlushesBeforeQueueEmpty = 0;
this._nextDispatchTimeout = DISPATCHER_LONG_TIMEOUT_MS;
return void 0;
}
const r13 = performance.now();
const r15 = this._workCallbackFn(a0);
const r17 = performance.now();
this.clearWorkTimeout();
this._nextDispatchTimeout = r17 - r13 > DISPATCHER_IDEAL_TIME_LIMIT_MS ? DISPATCHER_LONG_TIMEOUT_MS : DISPATCHER_STANDARD_TIMEOUT_MS;
const _consecutiveFlushesBeforeQueueEmpty = this._consecutiveFlushesBeforeQueueEmpty;
if (r15) {
if (_consecutiveFlushesBeforeQueueEmpty > 0) {
this.telemetry.measure(WorkSchedulerTelemetryMeasurement.COUNT_FLUSH_BEFORE_QUEUE_EMPTY, parseInt(`${this._consecutiveFlushesBeforeQueueEmpty}`));
}
this._consecutiveFlushesBeforeQueueEmpty = 0;
this._criticalWorkScheduled = false;
} else {
this._consecutiveFlushesBeforeQueueEmpty = _consecutiveFlushesBeforeQueueEmpty + 1;
}
}
}
markCriticalWorkScheduled() {
this._criticalWorkScheduled = true;
if (this._flushIdleHandler != null) {
this._clearIdleCallback();
this._processWorkCallback();
}
}
toggleRequestIdleCallback(a0) {
this._enableRequestIdleCallback = a0;
if (a0 ? !a0 : this.hasWorkScheduled) {
this._clearIdleCallback();
this._processWorkCallback();
}
}
get isBackgrounded() {
return this._isBackgrounded;
}
get hasWorkScheduled() {
return this._flushTimeoutHandler != null || this._flushIdleHandler != null;
}
get isRequestIdleCallbackEnabled() {
return this._enableRequestIdleCallback;
}
clearWorkTimeout() {
if (this._flushTimeoutHandler != null) {
clearTimeout(this._flushTimeoutHandler);
this._flushTimeoutHandler = null;
}
this._clearIdleCallback();
this._nextDispatchTimeout = DISPATCHER_STANDARD_TIMEOUT_MS;
this._workCallbackFn = null;
}
requestWorkTimeout(a0, r4 = false) {
this._workCallbackFn = a0;
if (!this.hasWorkScheduled) {
this.telemetry.time(WorkSchedulerTelemetryTiming.TIME_TO_QUEUE_EMPTY);
if (this._nextDispatchTimeout === DISPATCHER_LONG_TIMEOUT_MS) {
this.telemetry.track(WorkSchedulerTelemetryEvent.LONGER_DISPATCH);
}
if (r4) {
this._queueIdleCallback();
} else {
this._flushTimeoutHandler = setTimeout(() => {
Module38(this._workCallbackFn != null, "Work callback should be set");
if (this._isBackgrounded) {
this.telemetry.track(WorkSchedulerTelemetryEvent.SKIP_IDLE_CALLBACK_DUE_TO_BACKGROUNDED);
return this._processWorkCallback();
}
this._queueIdleCallback();
}, this._nextDispatchTimeout);
}
}
}
}