Skip to content

Commit

Permalink
Merge pull request #4351 from WalletConnect/fix/optimize-cleanup
Browse files Browse the repository at this point in the history
fix: optimize cleanup
  • Loading branch information
chris13524 committed Mar 22, 2024
2 parents 2a106e5 + be39ebc commit 0c22c5b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/core/src/controllers/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class JsonRpcHistory extends IJsonRpcHistory {
expiry: calcExpiry(THIRTY_DAYS),
};
this.records.set(record.id, record);
this.persist();
this.events.emit(HISTORY_EVENTS.created, record);
};

Expand All @@ -99,6 +100,7 @@ export class JsonRpcHistory extends IJsonRpcHistory {
? { error: response.error }
: { result: response.result };
this.records.set(record.id, record);
this.persist();
this.events.emit(HISTORY_EVENTS.updated, record);
};

Expand All @@ -121,6 +123,7 @@ export class JsonRpcHistory extends IJsonRpcHistory {
this.events.emit(HISTORY_EVENTS.deleted, record);
}
});
this.persist();
};

public exists: IJsonRpcHistory["exists"] = async (topic, id) => {
Expand Down Expand Up @@ -196,20 +199,17 @@ export class JsonRpcHistory extends IJsonRpcHistory {
const eventName = HISTORY_EVENTS.created;
this.logger.info(`Emitting ${eventName}`);
this.logger.debug({ type: "event", event: eventName, record });
this.persist();
});
this.events.on(HISTORY_EVENTS.updated, (record: JsonRpcRecord) => {
const eventName = HISTORY_EVENTS.updated;
this.logger.info(`Emitting ${eventName}`);
this.logger.debug({ type: "event", event: eventName, record });
this.persist();
});

this.events.on(HISTORY_EVENTS.deleted, (record: JsonRpcRecord) => {
const eventName = HISTORY_EVENTS.deleted;
this.logger.info(`Emitting ${eventName}`);
this.logger.debug({ type: "event", event: eventName, record });
this.persist();
});

this.core.heartbeat.on(HEARTBEAT_EVENTS.pulse, () => {
Expand All @@ -219,13 +219,20 @@ export class JsonRpcHistory extends IJsonRpcHistory {

private cleanup() {
try {
this.isInitialized();
let deleted = false;
this.records.forEach((record: JsonRpcRecord) => {
const msToExpiry = toMiliseconds(record.expiry || 0) - Date.now();
if (msToExpiry <= 0) {
this.logger.info(`Deleting expired history log: ${record.id}`);
this.delete(record.topic, record.id);
this.records.delete(record.id);
this.events.emit(HISTORY_EVENTS.deleted, record, false);
deleted = true;
}
});
if (deleted) {
this.persist();
}
} catch (e) {
this.logger.warn(e);
}
Expand Down

0 comments on commit 0c22c5b

Please sign in to comment.