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

162 lines
6.0 KiB
TypeScript

// Metro module 11528 (f11529).
import RTCConnectionStore from "../../stores/RTCConnectionStore";
import SelectedGuildStore from "../../stores/SelectedGuildStore";
import LurkingStore from "./LurkingStore";
import LifecycleManager from "../../lib/LifecycleManager";
import { getMobileLurkerServerPreview } from "./experiments/MobileLurkerServerPreviewExperiment";
import Dispatcher from "../../Dispatcher";
import { stopLurkingForGuild } from "./LurkerActionCreators";
class LurkerPreviewManager extends LifecycleManager {
constructor() {
let c18937_0;
super(...arguments);
const _this = this;
this.timers = new Map();
this.viewedLurkingGuildIds = new Set();
this.navigatedAwayFromLurkingGuildIds = new Set();
this.handleStoreChange = () => {
_this.trackViewedLurkingGuilds();
_this.evictOverflowingLurkers();
_this.ensureMissingExpiries();
_this.expireElapsed();
_this.syncTimers();
};
this.trackViewedLurkingGuilds = () => {
let r23;
const set = new Set(LurkingStore.lurkingGuildIds());
let r34 = Array.from(_this.viewedLurkingGuildIds)[Symbol.iterator]();
const next = r34.next;
while (true) {
const r11 = r34 === void 0 ? void 0 : (r23 = next.call(r34)).done ? r34 = void 0 : r23.value;
if (r34 === void 0) {
for (const item of Array.from(_this.navigatedAwayFromLurkingGuildIds)) {
if (!set.has(item)) {
_this.navigatedAwayFromLurkingGuildIds.delete(item);
}
}
const guildId2 = SelectedGuildStore.getGuildId();
for (const viewedLurkingGuildId of _this.viewedLurkingGuildIds) {
const r77 = viewedLurkingGuildId;
if (viewedLurkingGuildId !== guildId2) {
_this.navigatedAwayFromLurkingGuildIds.add(r77);
}
}
if (guildId2 != null && set.has(guildId2)) {
_this.viewedLurkingGuildIds.add(guildId2);
}
return void 0;
} else {
try {
if (!set.has(r11)) {
_this.viewedLurkingGuildIds.delete(r11);
}
} catch (r45) {
if (r34 !== void 0 && typeof r34.return === "function") {
try {
r34.return();
} catch {}
}
throw r45;
}
}
}
};
this.evictOverflowingLurkers = () => {
const r7 = LurkingStore.lurkingGuildIds();
if (!(r7.length <= 3) && getMobileLurkerServerPreview("LurkerPreviewManager.evictOverflowingLurkers").stickyLurk) {
_this.expireLurkerPreviews(r7.slice(0, r7.length - 3));
}
};
this.ensureMissingExpiries = () => {
const r5 = LurkingStore.lurkingGuildIds();
if (r5.length !== 0) {
const lurkingPreviewExpirations = LurkingStore.getLurkingPreviewExpirations();
const guildId = SelectedGuildStore.getGuildId();
const guildIds = r5.filter((item) => lurkingPreviewExpirations[item] == null && item !== guildId && _this.viewedLurkingGuildIds.has(item));
if (guildIds.length !== 0 && getMobileLurkerServerPreview("LurkerPreviewManager.ensureMissingExpiries").stickyLurk) {
Dispatcher.dispatch({
type: "LURKER_PREVIEW_SET_EXPIRY",
guildIds,
expiresAt: Date.now() + 60000
});
}
}
};
this.expireElapsed = () => {
const lurkingPreviewExpirations3 = LurkingStore.getLurkingPreviewExpirations();
const guildId3 = SelectedGuildStore.getGuildId();
const guildId2 = RTCConnectionStore.getGuildId();
const r4 = Date.now();
const r29 = [];
for (const item of Object.keys(lurkingPreviewExpirations3)) {
if (item !== guildId3 && item !== guildId2 && lurkingPreviewExpirations3[item] <= r4) {
r29.push(item);
}
}
if (r29.length > 0) {
_this.expireLurkerPreviews(r29);
}
};
this.syncTimers = () => {
let c27103_0 = _this;
const lurkingPreviewExpirations2 = LurkingStore.getLurkingPreviewExpirations();
for (const item of Array.from(_this.timers.keys())) {
if (lurkingPreviewExpirations2[item] == null) {
const r44 = _this.timers.get(item);
if (r44 != null) {
clearTimeout(r44);
}
_this.timers.delete(item);
}
}
const _loop = function _loop(a0) {
if (c27103_0.timers.has(a0)) {
return 0;
}
const r11 = lurkingPreviewExpirations2[a0];
if (r11 <= Date.now()) {
return 0;
}
c27103_0.timers.set(a0, setTimeout(() => {
c27103_0.timers.delete(a0);
c27103_0.handleStoreChange();
}, r11 - Date.now()));
};
for (const item2 of Object.keys(lurkingPreviewExpirations2)) {
_loop(item2);
}
};
c18937_0 = void 0;
c18937_0 = async (c30418_0) => {
await Promise.all(c30418_0.map((item) => stopLurkingForGuild(item)));
};
this.expireLurkerPreviews = function(a0) {
return c18937_0.apply(this, arguments);
};
}
hasNavigatedAway(a0) {
return this.navigatedAwayFromLurkingGuildIds.has(a0);
}
_initialize() {
LurkingStore.addChangeListener(this.handleStoreChange);
SelectedGuildStore.addChangeListener(this.handleStoreChange);
RTCConnectionStore.addChangeListener(this.handleStoreChange);
this.handleStoreChange();
}
_terminate() {
LurkingStore.removeChangeListener(this.handleStoreChange);
SelectedGuildStore.removeChangeListener(this.handleStoreChange);
RTCConnectionStore.removeChangeListener(this.handleStoreChange);
for (const item of this.timers.values()) {
clearTimeout(item);
}
this.timers.clear();
this.viewedLurkingGuildIds.clear();
this.navigatedAwayFromLurkingGuildIds.clear();
}
}
const lurkerPreviewManager = new LurkerPreviewManager();
export default lurkerPreviewManager;
export const LURKER_PREVIEW_PERSIST_MS = 60000;
export const LURKER_PREVIEW_MAX_CONCURRENT = 3;