File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed
Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change @@ -4,13 +4,16 @@ import sinon from 'sinon';
44
55describe ( '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} ) ;
You can’t perform that action at this time.
0 commit comments