Skip to content

Commit 319720f

Browse files
committed
Restore expiry/deletion logic
1 parent 447baa9 commit 319720f

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
@@ -711,42 +711,43 @@ export class Backend {
711711
return startEpoch;
712712
}
713713

714-
clearImpressionsForConversionSite(site: string): void {
715-
void site;
716-
/* TODO
717-
function shouldRemoveImpression(i: Impression): boolean {
714+
async clearImpressionsForConversionSite(site: string): Promise<void> {
715+
const db = await this.#getOrCreateDB();
716+
const txn = db.transaction("impressions", "readwrite");
717+
718+
for await (const cursor of txn.store) {
719+
const i = cursor.value;
720+
718721
if (i.intermediarySite === site) {
719-
return true;
722+
await cursor.delete();
723+
continue;
720724
}
721725
if (!i.conversionSites.has(site)) {
722-
return false;
726+
continue;
723727
}
724728
if (i.conversionSites.size > 1) {
725729
i.conversionSites.delete(site);
726-
return false;
730+
await cursor.update(i);
731+
continue;
727732
}
728-
return true;
733+
await cursor.delete();
729734
}
730-
731-
this.#impressions = this.#impressions.filter(
732-
(i) => !shouldRemoveImpression(i),
733-
);
734-
*/
735735
}
736736

737-
clearExpiredImpressions(): void {
738-
/* TODO
737+
async clearExpiredImpressions(): Promise<void> {
738+
const db = await this.#getOrCreateDB();
739+
const txn = db.transaction("impressions", "readwrite");
740+
739741
const now = this.#delegate.now();
740742

741-
this.#impressions = this.#impressions.filter((impression) => {
742-
return (
743-
Temporal.Instant.compare(
744-
now,
745-
impression.timestamp.add(impression.lifetime),
746-
) < 0
747-
);
748-
});
749-
*/
743+
for await (const cursor of txn.store.iterate()) {
744+
const i = cursor.value;
745+
const expiry = i.timestamp.toTemporalInstant().add(days(i.lifetimeDays));
746+
747+
if (Temporal.Instant.compare(now, expiry) > 0) {
748+
await cursor.delete();
749+
}
750+
}
750751
}
751752

752753
#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)