Skip to content

Commit

Permalink
Delete the interval once the brain gets cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed Jul 6, 2023
1 parent 0fd9b7a commit 898f62c
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/brain/local-brain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { Brain } from './brain';

export class LocalBrain extends Brain {
memory: Map<string, FN.Brain.BrainRecord> = new Map();
cleanupInterval: NodeJS.Timer;

constructor() {
super();

setInterval(() => {
this.cleanupInterval = setInterval(() => {
for (let [key, { ttlSeconds, setTime }] of [...this.memory]) {
let currentTime = parseInt((new Date().getTime() / 1000) as unknown as string);

Expand Down Expand Up @@ -41,4 +42,9 @@ export class LocalBrain extends Brain {
async delete(key: string): Promise<void> {
this.memory.delete(key);
}

async cleanup(): Promise<void> {
this.memory.clear();
clearInterval(this.cleanupInterval);
}
}

0 comments on commit 898f62c

Please sign in to comment.