Skip to content

Commit 7268c20

Browse files
committed
Restore expiry/deletion logic
1 parent acb5000 commit 7268c20

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

impl/src/backend.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -718,42 +718,43 @@ export class Backend {
718718
return startEpoch;
719719
}
720720

721-
clearImpressionsForConversionSite(site: string): void {
722-
void site;
723-
/* TODO
724-
function shouldRemoveImpression(i: Impression): boolean {
721+
async clearImpressionsForConversionSite(site: string): Promise<void> {
722+
const db = await this.#getOrCreateDB();
723+
const txn = db.transaction("impressions", "readwrite");
724+
725+
for await (const cursor of txn.store) {
726+
const i = cursor.value;
727+
725728
if (i.intermediarySite === site) {
726-
return true;
729+
await cursor.delete();
730+
continue;
727731
}
728732
if (!i.conversionSites.has(site)) {
729-
return false;
733+
continue;
730734
}
731735
if (i.conversionSites.size > 1) {
732736
i.conversionSites.delete(site);
733-
return false;
737+
await cursor.update(i);
738+
continue;
734739
}
735-
return true;
740+
await cursor.delete();
736741
}
737-
738-
this.#impressions = this.#impressions.filter(
739-
(i) => !shouldRemoveImpression(i),
740-
);
741-
*/
742742
}
743743

744-
clearExpiredImpressions(): void {
745-
/* TODO
744+
async clearExpiredImpressions(): Promise<void> {
745+
const db = await this.#getOrCreateDB();
746+
const txn = db.transaction("impressions", "readwrite");
747+
746748
const now = this.#delegate.now();
747749

748-
this.#impressions = this.#impressions.filter((impression) => {
749-
return (
750-
Temporal.Instant.compare(
751-
now,
752-
impression.timestamp.add(impression.lifetime),
753-
) < 0
754-
);
755-
});
756-
*/
750+
for await (const cursor of txn.store.iterate()) {
751+
const i = cursor.value;
752+
const expiry = i.timestamp.toTemporalInstant().add(days(i.lifetimeDays));
753+
754+
if (Temporal.Instant.compare(now, expiry) > 0) {
755+
await cursor.delete();
756+
}
757+
}
757758
}
758759

759760
#fairlyAllocateCredit(credit: number[], value: number): number[] {

impl/src/simulator.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ async function updateImpressionsTable() {
125125

126126
now = now.add(days(daysInput.valueAsNumber));
127127
time.innerText = now.toString();
128-
backend.clearExpiredImpressions();
129-
void updateImpressionsTable();
128+
void backend.clearExpiredImpressions().then(updateImpressionsTable);
130129
});
131130
}
132131

0 commit comments

Comments
 (0)