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

157 lines
4.8 KiB
TypeScript

// Metro module 4273 (f4274).
import Module12 from "../_bundle/Module12";
import Module3771 from "../_bundle/Module3771";
let DEFAULT_WEIGHT;
const DEFAULT_FRECENCY = (a0, a1, a2) => Math.ceil(a0 * (a1 / a2.numOfRecentUses));
DEFAULT_WEIGHT = (a0) => a0 <= 3 ? 100 : a0 <= 15 ? 70 : a0 <= 30 ? 50 : a0 <= 45 ? 30 : a0 <= 80 ? 10 : 1;
class Frecency {
constructor({ computeBonus, computeWeight = DEFAULT_WEIGHT, computeFrecency = DEFAULT_FRECENCY, calculateMaxTotalUse = false, lookupKey, afterCompute, numFrequentlyItems = 32, maxSamples = 10 }) {
this.computeBonus = computeBonus;
this.computeWeight = computeWeight;
this.computeFrecency = computeFrecency;
this.calculateMaxTotalUse = calculateMaxTotalUse;
this.afterCompute = afterCompute;
this.lookupKey = lookupKey;
this.usageHistory = {};
this.frequently = [];
this.maxSamples = maxSamples;
this.numFrequentlyItems = numFrequentlyItems;
this.dirty = false;
this.version = 0;
}
overwriteHistory(a0, a1) {
this.usageHistory = Module12.mapValues(a0 == null ? {} : a0, (a0) => ({
...a0,
frecency: -1
}));
if (a1 != null) {
a1.forEach((item) => this.track(item.key, {
timestamp: item.timestamp
}));
}
this.markDirty();
}
markDirty() {
this.dirty = true;
this.version = this.version + 1;
}
isDirty() {
return this.dirty;
}
track(a0, r3 = {}) {
let r33, length2, maxSamples2;
let timestamp = r3.timestamp;
let usesSinceLastTrack = r3.usesSinceLastTrack;
if (a0 != null) {
const r11 = Object.hasOwn(this.usageHistory, a0) ? this.usageHistory[a0] : void 0;
if (r11 == null) {
if (usesSinceLastTrack == null) {
usesSinceLastTrack = 1;
}
if (timestamp == null) {
timestamp = Date.now();
}
r33 = {
totalUses: usesSinceLastTrack,
recentUses: [timestamp],
frecency: -1,
score: 0
};
} else {
r11.frecency = -1;
r11.totalUses = r11.totalUses + (usesSinceLastTrack == null ? 1 : usesSinceLastTrack);
if (timestamp == null) {
r11.recentUses.push(Date.now());
} else {
r11.recentUses.push(timestamp);
r11.recentUses.sort();
}
const length = r11.recentUses.length;
const maxSamples = this.maxSamples;
r33 = r11;
if (length > maxSamples) {
do {
r11.recentUses.shift();
length2 = r11.recentUses.length;
maxSamples2 = this.maxSamples;
r33 = r11;
} while (length2 > maxSamples2);
}
}
this.usageHistory[a0] = r33;
this.markDirty();
}
}
getEntry(a0) {
let r1 = null;
if (a0 != null) {
if (this.dirty) {
this.compute();
}
r1 = Object.prototype.hasOwnProperty.call(this.usageHistory, a0) ? this.usageHistory[a0] : void 0;
}
return r1;
}
getScore(a0) {
const entry = this.getEntry(a0);
return entry != null ? entry.score : null;
}
getFrecency(a0) {
const entry = this.getEntry(a0);
return entry != null ? entry.frecency : null;
}
compute() {
let c14070_1 = Module3771();
let c14070_0 = this.calculateMaxTotalUse ? Module12.maxBy(Object.values(this.usageHistory), (a0) => a0.totalUses) : null;
Module12.forEach(this.usageHistory, (a0, a1) => {
let c25020_1;
let c25020_0 = a0;
const { totalUses, recentUses } = a0;
if (a0.frecency === -1) {
c25020_1 = this.computeBonus(a1) / 100;
a0.score = 0;
Module12.forEach(recentUses, (a0, a1) => {
if (a1 >= this.maxSamples) {
return false;
}
const r20 = this.computeWeight(c14070_1.diff(Module3771(a0), "days"));
c25020_0.score = c25020_0.score + c25020_1 * r20;
});
if (a0.score > 0) {
if (a0.recentUses.length > 0) {
a0.frecency = this.computeFrecency(totalUses, a0.score, {
numOfRecentUses: recentUses.length,
maxTotalUse: c14070_0?.totalUses
});
}
this.usageHistory[a1] = a0;
} else {
delete this.usageHistory[a1];
}
}
});
this.frequently = Module12(this.usageHistory).map((item, index) => {
const r4 = this.lookupKey(index);
return r4 != null ? [r4, item.frecency] : null;
}).filter((item) => item !== null).sortBy((item) => {
const [, r13] = item;
return -r13;
}).map((item) => {
const [r8] = item;
return r8;
}).take(this.numFrequentlyItems).value();
this.dirty = false;
this.afterCompute(this.usageHistory, this._frequently);
}
get frequently() {
if (this.dirty) {
this.compute();
}
return this._frequently;
}
set frequently(a0) {
this._frequently = a0;
}
}
export default Frecency;