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

258 lines
8.0 KiB
TypeScript

// Metro module 9138 (f9139).
import Module38 from "../_bundle/Module38";
import Dispatcher from "../Dispatcher";
let env5993 = {};
const r15 = {
FORUM_CHANNEL: "forum_channel"
};
const c5993_3 = {
IMMEDIATE: 0,
0: "IMMEDIATE",
IMMEDIATE_WITH_COOLDOWN: 1,
1: "IMMEDIATE_WITH_COOLDOWN",
IMMEDIATE_WITH_DELAY: 2,
2: "IMMEDIATE_WITH_DELAY"
};
export class TrackedFeedItem {
seenIntervals = [];
maybeMarkSeen(a0) {
const r9 = this.seenIntervals[this.seenIntervals.length - 1];
let r1 = r9 == null || r9.endTimeMillis != null;
if (r1) {
this.seenIntervals.push({
startTimeMillis: a0
});
r1 = true;
}
return r1;
}
maybeMarkUnseen(a0) {
const r8 = this.seenIntervals[this.seenIntervals.length - 1];
let r1 = r8 != null && r8.endTimeMillis == null;
if (r1) {
r8.endTimeMillis = a0;
r1 = true;
}
return r1;
}
isVisible() {
const r10 = this.seenIntervals[this.seenIntervals.length - 1];
return r10?.startTimeMillis != null && r10?.endTimeMillis == null;
}
computeSeenTimeDestructive(a0) {
let r0 = 0;
const seenIntervals = [];
for (const seenInterval of this.seenIntervals) {
const r28 = seenInterval;
if (seenInterval.endTimeMillis == null) {
if (a0) {
const startTimeMillis = Date.now();
r0 = r0 + (startTimeMillis - r28.startTimeMillis);
seenIntervals.push({
startTimeMillis
});
} else {
seenIntervals.push(r28);
}
} else {
const r30 = r0;
r0 = r30 + (r28.endTimeMillis - r28.startTimeMillis);
}
}
Module38(seenIntervals.length < 2, "there should only be a single left over data");
this.seenIntervals = seenIntervals;
return Math.round(r0);
}
}
export { r15 as AnalyticsFeedTypes };
export { c5993_3 as ForceFlushType };
export class AnalyticsFeedItemSeenManager {
constructor(a0) {
const { id, windowId } = a0;
let isPaused = a0.isPaused;
this.initialize = () => {
Dispatcher.subscribe("ANALYTICS_FEED_ITEM_SEEN", this.handleFeedItemSeen);
Dispatcher.subscribe("ANALYTICS_FEED_ITEM_UNSEEN", this.handleFeedItemUnseen);
Dispatcher.subscribe("ANALYTICS_FEED_FLUSH", this.handleFeedItemFlush);
Dispatcher.subscribe("APP_STATE_UPDATE", this.handleAppStateUpdate);
Dispatcher.subscribe("WINDOW_FOCUS", this.handleWindowFocus);
const onInitialize = this.onInitialize;
if (onInitialize != null) {
onInitialize.call(this);
}
};
this.terminate = () => {
Dispatcher.unsubscribe("ANALYTICS_FEED_ITEM_SEEN", this.handleFeedItemSeen);
Dispatcher.unsubscribe("ANALYTICS_FEED_ITEM_UNSEEN", this.handleFeedItemUnseen);
Dispatcher.unsubscribe("ANALYTICS_FEED_FLUSH", this.handleFeedItemFlush);
Dispatcher.unsubscribe("APP_STATE_UPDATE", this.handleAppStateUpdate);
Dispatcher.unsubscribe("WINDOW_FOCUS", this.handleWindowFocus);
const onTerminate = this.onTerminate;
if (onTerminate != null) {
onTerminate.call(this);
}
this.maybeFlushSeenItems(c5993_3.IMMEDIATE);
};
this.handleFeedItemFlush = ({ id, force }) => {
if (this._id === id) {
this.maybeFlushSeenItems(force);
}
};
this.handleFeedItemSeen = ({ id, timestampMillis, feedItemId }) => {
if (id === this._id) {
if (this._paused) {
this._pausedFeedItemIds.add(feedItemId);
} else {
const r12 = this.getTrackedFeedItem(feedItemId).maybeMarkSeen(timestampMillis);
const onFeedItemSeen = this.onFeedItemSeen;
if (onFeedItemSeen != null) {
onFeedItemSeen.call(this, feedItemId, r12);
}
}
}
};
this.handleFeedItemUnseen = ({ id, timestampMillis, feedItemId }) => {
if (id === this._id) {
if (this._paused) {
this._pausedFeedItemIds.delete(feedItemId);
}
const r15 = this.getTrackedFeedItem(feedItemId).maybeMarkUnseen(timestampMillis);
const onFeedItemUnseen = this.onFeedItemUnseen;
if (onFeedItemUnseen != null) {
onFeedItemUnseen.call(this, feedItemId, r15);
}
this.maybeFlushSeenItems();
}
};
this.getTrackedFeedItem = (a0) => {
if (this.trackedFeedItems[a0] == null) {
const trackedFeedItems = this.trackedFeedItems;
const r13 = {
__proto__: TrackedFeedItem.prototype
};
r13.seenIntervals = [];
trackedFeedItems[a0] = r13;
}
return this.trackedFeedItems[a0];
};
this.getVisibleFeedItemIds = () => new Set(Object.keys(this.trackedFeedItems).filter((item) => {
const r3 = this.trackedFeedItems[item];
return r3 == null ? void 0 : r3.isVisible();
}));
this.handleAppStateUpdate = ({ state }) => {
if (state === "active" && this._isReactNavigationFocused) {
this.resume();
}
if (state === "background") {
if (this._isReactNavigationFocused) {
this.pause();
}
this.maybeFlushSeenItems(c5993_3.IMMEDIATE);
}
};
this.clearPausedFeedItemIds = () => {
this._pausedFeedItemIds = new Set();
this._paused = false;
};
this.pause = () => {
if (!this._paused) {
const visibleFeedItemIds = this.getVisibleFeedItemIds();
visibleFeedItemIds.forEach((visibleFeedItemId) => {
this.handleFeedItemUnseen({
id: this._id,
feedItemId: visibleFeedItemId,
timestampMillis: Date.now(),
type: "ANALYTICS_FEED_ITEM_UNSEEN"
});
});
this._paused = true;
this._pausedFeedItemIds = visibleFeedItemIds;
}
};
this.resume = () => {
if (this._paused) {
this._paused = false;
this._pausedFeedItemIds.forEach((_pausedFeedItemId) => {
this.handleFeedItemSeen({
id: this._id,
feedItemId: _pausedFeedItemId,
timestampMillis: Date.now(),
type: "ANALYTICS_FEED_ITEM_SEEN"
});
});
this.clearPausedFeedItemIds();
}
};
this.handleReactNavigationFocus = (a0) => {
this._isReactNavigationFocused = a0;
if (this._isReactNavigationFocused) {
this.resume();
} else {
this.pause();
}
};
this.handleWindowFocus = (a0) => {
if (this._windowId === a0.windowId) {
if (a0.focused) {
this.resume();
} else {
this.pause();
}
}
};
this.trackedFeedItems = {};
this._id = id;
this._windowId = windowId;
this._pausedFeedItemIds = new Set();
if (isPaused == null) {
isPaused = false;
}
this._paused = isPaused;
this._isReactNavigationFocused = true;
this._lastFlushTimeMillis = Date.now();
}
maybeFlushSeenItems(a0) {
let r34;
if (a0 == null && Date.now() - this._lastFlushTimeMillis < 60000) {
return Promise.resolve();
}
if (a0 === c5993_3.IMMEDIATE_WITH_COOLDOWN && Date.now() - this._lastFlushTimeMillis < 3000) {
return Promise.resolve();
}
let env17022;
L11: {
env17022 = {
$parent: env5993
};
const flushSeenItemsFunction = this.createFlushSeenItemsFunction(a0);
env17022[0] = flushSeenItemsFunction;
if (flushSeenItemsFunction == null) {
r34 = Promise.resolve();
} else {
this._lastFlushTimeMillis = Date.now();
if (a0 !== c5993_3.IMMEDIATE && a0 !== c5993_3.IMMEDIATE_WITH_COOLDOWN) {
r34 = new Promise((c26367_0) => {
setTimeout(async () => {
let c30034_0;
c30034_0 = void 0;
await (0, env17022[0])();
(0, c26367_0)();
}, 100);
});
break L11;
}
const r35 = Promise;
let c17023_0 = async (c30033_0) => {
let c31487_0 = c30033_0;
await (0, env17022[0])();
(0, c31487_0)();
};
r34 = new r35(function() {
return c17023_0.apply(this, arguments);
});
}
}
return r34;
}
}