Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/commands/emails/receiving/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { buildHelpText } from '../../../lib/help-text';
import { errorMessage, outputError } from '../../../lib/output';
import { createSpinner } from '../../../lib/spinner';
import { isInteractive } from '../../../lib/tty';
import { createBoundedSet } from '../../../utils/bounded-set';

const PAGE_SIZE = 100;

Expand Down Expand Up @@ -78,7 +79,7 @@ Ctrl+C exits cleanly.`,
globalOpts.quiet || jsonMode,
);

const seenIds = new Set<string>();
const seenIds = createBoundedSet<string>();
let consecutiveErrors = 0;

try {
Expand Down
38 changes: 38 additions & 0 deletions src/utils/bounded-set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export type BoundedSet<T> = {
readonly has: (value: T) => boolean;
readonly add: (value: T) => void;
readonly size: () => number;
};

const DEFAULT_MAX_SIZE = 10_000;

export const createBoundedSet = <T>(
maxSize: number = DEFAULT_MAX_SIZE,
): BoundedSet<T> => {
const map = new Map<T, true>();

const evict = () => {
const oldest = map.keys().next();
if (!oldest.done) {
map.delete(oldest.value);
}
};

return {
has: (value: T) => map.has(value),

add: (value: T) => {
if (map.has(value)) {
map.delete(value);
map.set(value, true);
return;
}
if (map.size >= maxSize) {
evict();
}
map.set(value, true);
},

size: () => map.size,
};
};
79 changes: 79 additions & 0 deletions tests/utils/bounded-set.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { describe, expect, it } from 'vitest';
import { createBoundedSet } from '../../src/utils/bounded-set';

describe('createBoundedSet', () => {
it('tracks membership correctly', () => {
const set = createBoundedSet<string>(5);
set.add('a');
set.add('b');

expect(set.has('a')).toBe(true);
expect(set.has('b')).toBe(true);
expect(set.has('c')).toBe(false);
});

it('evicts oldest entries when capacity is exceeded', () => {
const set = createBoundedSet<string>(3);
set.add('a');
set.add('b');
set.add('c');
set.add('d');

expect(set.has('a')).toBe(false);
expect(set.has('b')).toBe(true);
expect(set.has('c')).toBe(true);
expect(set.has('d')).toBe(true);
expect(set.size()).toBe(3);
});

it('refreshes recently re-added entries to prevent premature eviction', () => {
const set = createBoundedSet<string>(3);
set.add('a');
set.add('b');
set.add('c');

set.add('a');

set.add('d');

expect(set.has('a')).toBe(true);
expect(set.has('b')).toBe(false);
expect(set.has('c')).toBe(true);
expect(set.has('d')).toBe(true);
});

it('does not exceed the configured capacity', () => {
const set = createBoundedSet<number>(5);
const entries = Array.from({ length: 20 }, (_, i) => i);
entries.forEach((n) => {
set.add(n);
});

expect(set.size()).toBe(5);

const retained = entries.filter((n) => set.has(n));
expect(retained).toEqual([15, 16, 17, 18, 19]);
});

it('handles duplicate adds without growing', () => {
const set = createBoundedSet<string>(3);
set.add('x');
set.add('x');
set.add('x');

expect(set.size()).toBe(1);
expect(set.has('x')).toBe(true);
});

it('uses default capacity when none is provided', () => {
const set = createBoundedSet<number>();
const entries = Array.from({ length: 10_001 }, (_, i) => i);
entries.forEach((n) => {
set.add(n);
});

expect(set.size()).toBe(10_000);
expect(set.has(0)).toBe(false);
expect(set.has(10_000)).toBe(true);
});
});
Loading