Skip to content

Commit d4beafb

Browse files
authored
Merge pull request #69 from namecheap/perf/intl
fix: provide compatibility when running code in vm.runInNewContext()
2 parents 6996be0 + 9768b9e commit d4beafb

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/app/utils/TtlCache.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ export class TtlCache<K, V> {
6161
}
6262

6363
private scheduleCleanup(): void {
64+
/**
65+
* Required due to source code evaluation in vm context compatibility
66+
* Temporary workaround until for internal needs
67+
* Should be removed soon
68+
*/
69+
if (typeof setTimeout !== 'function') {
70+
return;
71+
}
6472
this.timeoutId = setTimeout(() => this.cleanup(), this.cleanupInterval);
6573
this.timeoutId.unref?.();
6674
}

test/app/TtlCache.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import sinon from 'sinon';
44

55
describe('TtlCache', () => {
66
let clock: sinon.SinonFakeTimers;
7+
let setTimeoutBackup: typeof setTimeout;
78

89
beforeEach(() => {
10+
setTimeoutBackup = global.setTimeout;
911
clock = sinon.useFakeTimers();
1012
});
1113

1214
afterEach(() => {
1315
clock.restore();
16+
global.setTimeout = setTimeoutBackup;
1417
});
1518

1619
it('should set and get a value', () => {
@@ -68,4 +71,13 @@ describe('TtlCache', () => {
6871
clock.tick(500); // Trigger cleanup
6972
expect(cache.get('key1')).to.be.undefined;
7073
});
74+
75+
it('should not schedule cleanup if setTimeout is not available', () => {
76+
const cache = new TtlCache<string, number>({ ttl: 1000, cleanupInterval: 500 });
77+
global.setTimeout = undefined as any;
78+
cache.set('key1', 123);
79+
expect(cache.get('key1')).to.equal(123);
80+
clock.tick(1001);
81+
expect(cache.get('key1')).to.equal(123);
82+
});
7183
});

0 commit comments

Comments
 (0)