Files
discord-decomp/stores/IdleStore.tsx
T
2026-11-01 12:12:55 +02:00

197 lines
5.7 KiB
TypeScript

// Metro module 5000 (f5001).
import AuthenticationStore from "./AuthenticationStore";
import { AppStates, IDLE_DURATION } from "../Constants";
import { SpeakingFlags } from "../_bundle/Module4250";
import { isAndroid, isPlatformEmbedded } from "../utils/PlatformUtils";
import { AfkTimeout } from "../modules/user_settings/UserSettings";
import Durations from "../utils/Durations";
import Dispatcher from "../Dispatcher";
import DiscordNative from "../lib/DiscordNative";
import SelectedChannelActionCreators from "../actions/SelectedChannelActionCreators";
import debounce from "../_bundle/debounce";
import Module589 from "../_bundle/Module589";
let c3008_3, idleSince, idle, afk, c3008_11, c3008_12, c3008_13, checkNativeIdle2;
const checkIdleAFK = function checkIdleAFK() {
L12: {
if (!(Date.now() - idleSince > IDLE_DURATION) && !(c3008_11 || c3008_12 || isAndroid() && c3008_13)) {
if (!idle) {
break L12;
}
Dispatcher.dispatch({
type: "IDLE",
idle: false
});
} else if (!idle) {
Dispatcher.dispatch({
type: "IDLE",
idle: true,
idleSince
});
}
}
L26: {
const setting = AfkTimeout.getSetting();
if (setting !== 0 && c3008_3 == null && !(Date.now() - idleSince > Math.min(setting * Durations.Millis.SECOND, IDLE_DURATION)) && !(c3008_11 || c3008_12 || isAndroid() && c3008_13)) {
if (!afk) {
break L26;
}
Dispatcher.dispatch({
type: "AFK",
afk: false
});
} else if (!afk) {
Dispatcher.dispatch({
type: "AFK",
afk: true
});
}
}
};
idleSince = Date.now();
idle = false;
afk = false;
c3008_11 = false;
c3008_12 = false;
c3008_13 = false;
if (isPlatformEmbedded) {
if (DiscordNative?.powerMonitor != null) {
checkNativeIdle2 = function checkNativeIdle() {
if ((DiscordNative == null ? void 0 : DiscordNative.powerMonitor?.getSystemIdleTimeMs) != null) {
const systemIdleTimeMs = DiscordNative.powerMonitor.getSystemIdleTimeMs();
if (systemIdleTimeMs instanceof Promise) {
systemIdleTimeMs.then(function handleIdleTime(result) {
const r9 = Date.now() - result;
if (c3008_3 == null || r9 > c3008_3) {
idleSince = Math.max(r9, idleSince);
c3008_3 = null;
}
checkIdleAFK();
setTimeout(checkNativeIdle2, 10 * Durations.Millis.SECOND);
});
} else {
const r13 = Date.now() - systemIdleTimeMs;
if (c3008_3 == null || r13 > c3008_3) {
idleSince = Math.max(r13, idleSince);
c3008_3 = null;
}
checkIdleAFK();
setTimeout(checkNativeIdle2, 10 * Durations.Millis.SECOND);
}
}
};
checkNativeIdle2();
DiscordNative.powerMonitor.on("resume", () => {
c3008_11 = false;
checkIdleAFK();
});
DiscordNative.powerMonitor.on("suspend", () => {
c3008_11 = true;
c3008_3 = Date.now();
checkIdleAFK();
SelectedChannelActionCreators.disconnect();
});
DiscordNative.powerMonitor.on("lock-screen", () => {
c3008_12 = true;
c3008_3 = Date.now();
checkIdleAFK();
});
DiscordNative.powerMonitor.on("unlock-screen", () => {
c3008_12 = false;
checkIdleAFK();
});
} else {
setInterval(checkIdleAFK, 30 * Durations.Millis.SECOND);
}
} else {
setInterval(checkIdleAFK, 30 * Durations.Millis.SECOND);
}
const handleGenericAction = function handleGenericAction(a0) {
let timestamp = a0.timestamp;
const { type, bypassIdleUpdate } = a0;
const r0 = type === "OVERLAY_SET_NOT_IDLE" && timestamp != null;
if (!(r0 && timestamp <= idleSince || bypassIdleUpdate)) {
c3008_3 = null;
if (!r0) {
timestamp = Date.now();
}
idleSince = timestamp;
checkIdleAFK();
}
return false;
};
debounce(() => {
const r2 = {};
let timestamp = r2.timestamp;
const { type, bypassIdleUpdate } = r2;
const r0 = type === "OVERLAY_SET_NOT_IDLE" && timestamp != null;
if (!(r0 && timestamp <= idleSince || bypassIdleUpdate)) {
c3008_3 = null;
if (!r0) {
timestamp = Date.now();
}
idleSince = timestamp;
checkIdleAFK();
}
}, 500);
class IdleStore extends Module589.Store {
static displayName = "IdleStore";
initialize() {
this.waitFor(AuthenticationStore);
}
isIdle() {
return idle;
}
isAFK() {
return afk;
}
getIdleSince() {
return idle ? idleSince : null;
}
getSystemSuspended() {
return c3008_11;
}
getSystemLocked() {
return c3008_12;
}
}
const idleStore = new IdleStore(Dispatcher, {
IDLE: function handleIdle(a0) {
idle = a0.idle;
},
AFK: function handleAFK(a0) {
afk = a0.afk;
},
SPEAKING: function handleSpeaking(a0) {
const userId = a0.userId;
if (a0.speakingFlags !== SpeakingFlags.NONE && userId === AuthenticationStore.getId()) {
const r12 = {};
let timestamp = r12.timestamp;
const { type, bypassIdleUpdate } = r12;
const r16 = type === "OVERLAY_SET_NOT_IDLE" && timestamp != null;
if (!(r16 && timestamp <= idleSince || bypassIdleUpdate)) {
c3008_3 = null;
if (!r16) {
timestamp = Date.now();
}
idleSince = timestamp;
checkIdleAFK();
}
}
return false;
},
APP_STATE_UPDATE: function handleAppStateUpdate(a0) {
c3008_13 = a0.state === AppStates.BACKGROUND;
c3008_3 = null;
idleSince = Date.now();
checkIdleAFK();
return false;
},
OVERLAY_SET_NOT_IDLE: handleGenericAction,
CHANNEL_SELECT: handleGenericAction,
VOICE_CHANNEL_SELECT: handleGenericAction,
WINDOW_FOCUS: handleGenericAction,
OVERLAY_INITIALIZE: handleGenericAction,
OVERLAY_SET_INPUT_LOCKED: handleGenericAction
});
export default idleStore;