@@ -7,13 +7,13 @@ export const provideTimingFunctions = (
77 maxIntervalCount : number
88 } ,
99) => {
10- const scope = new Scope ( )
10+ const scope = new Scope ( )
1111
12- const timeouts = new Map < number , ReturnType < typeof setTimeout > > ( )
13- let timeoutCounter = 0
12+ const timeouts = new Map < number , ReturnType < typeof setTimeout > > ( )
13+ let timeoutCounter = 0
1414
15- const immediates = new Map < number , ReturnType < typeof setTimeout > > ( )
16- let immediateCounter = 0
15+ const immediates = new Map < number , ReturnType < typeof setTimeout > > ( )
16+ let immediateCounter = 0
1717
1818 const intervals = new Map < number , ReturnType < typeof setTimeout > > ( )
1919 let intervalCounter = 0
@@ -59,49 +59,49 @@ export const provideTimingFunctions = (
5959 } )
6060
6161 scope . manage ( _clearTimeout )
62- ctx . setProp ( ctx . global , 'clearTimeout' , _clearTimeout )
63-
64- const _setImmediate = ctx . newFunction ( 'setImmediate' , vmFnHandle => {
65- const currentCounter = immediateCounter ++
66- if ( timeouts . size + 1 > max . maxTimeoutCount ) {
67- throw new Error (
68- `Client tries to use setImmediate, which exceeds the limit of max ${ max . maxTimeoutCount } concurrent running timeout functions` ,
69- )
70- }
71-
72- const vmFnHandleCopy = vmFnHandle . dup ( )
73- scope . manage ( vmFnHandleCopy )
74-
75- const timeoutID = setTimeout ( ( ) => {
76- const t = immediates . get ( currentCounter )
77- if ( t ) {
78- clearTimeout ( t )
79- immediates . delete ( currentCounter )
80- }
81- ctx . callFunction ( vmFnHandleCopy , ctx . undefined )
82- } , 0 )
83-
84- immediates . set ( currentCounter , timeoutID )
85-
86- return ctx . newNumber ( currentCounter )
87- } )
88-
89- scope . manage ( _setImmediate )
90- ctx . setProp ( ctx . global , 'setImmediate' , _setImmediate )
91-
92- const _clearImmediate = ctx . newFunction ( 'clearImmediate' , idHandle => {
93- const id : number = ctx . dump ( idHandle )
94- idHandle . dispose ( )
95-
96- const t = immediates . get ( id )
97- if ( t ) {
98- clearTimeout ( t )
99- immediates . delete ( id )
100- }
101- } )
102-
103- scope . manage ( _clearImmediate )
104- ctx . setProp ( ctx . global , 'clearImmediate' , _clearImmediate )
62+ ctx . setProp ( ctx . global , 'clearTimeout' , _clearTimeout )
63+
64+ const _setImmediate = ctx . newFunction ( 'setImmediate' , vmFnHandle => {
65+ const currentCounter = immediateCounter ++
66+ if ( timeouts . size + 1 > max . maxTimeoutCount ) {
67+ throw new Error (
68+ `Client tries to use setImmediate, which exceeds the limit of max ${ max . maxTimeoutCount } concurrent running timeout functions` ,
69+ )
70+ }
71+
72+ const vmFnHandleCopy = vmFnHandle . dup ( )
73+ scope . manage ( vmFnHandleCopy )
74+
75+ const timeoutID = setTimeout ( ( ) => {
76+ const t = immediates . get ( currentCounter )
77+ if ( t ) {
78+ clearTimeout ( t )
79+ immediates . delete ( currentCounter )
80+ }
81+ ctx . callFunction ( vmFnHandleCopy , ctx . undefined )
82+ } , 0 )
83+
84+ immediates . set ( currentCounter , timeoutID )
85+
86+ return ctx . newNumber ( currentCounter )
87+ } )
88+
89+ scope . manage ( _setImmediate )
90+ ctx . setProp ( ctx . global , 'setImmediate' , _setImmediate )
91+
92+ const _clearImmediate = ctx . newFunction ( 'clearImmediate' , idHandle => {
93+ const id : number = ctx . dump ( idHandle )
94+ idHandle . dispose ( )
95+
96+ const t = immediates . get ( id )
97+ if ( t ) {
98+ clearTimeout ( t )
99+ immediates . delete ( id )
100+ }
101+ } )
102+
103+ scope . manage ( _clearImmediate )
104+ ctx . setProp ( ctx . global , 'clearImmediate' , _clearImmediate )
105105
106106 const _setInterval = ctx . newFunction ( 'setInterval' , ( vmFnHandle , intervalHandle ) => {
107107 const currentCounter = intervalCounter ++
@@ -140,23 +140,23 @@ export const provideTimingFunctions = (
140140 scope . manage ( _clearInterval )
141141 ctx . setProp ( ctx . global , 'clearInterval' , _clearInterval )
142142
143- const dispose = ( ) => {
144- for ( const [ _key , value ] of timeouts ) {
145- clearTimeout ( value )
146- }
147- timeouts . clear ( )
148- timeoutCounter = 0
149-
150- for ( const [ _key , value ] of immediates ) {
151- clearTimeout ( value )
152- }
153- immediates . clear ( )
154- immediateCounter = 0
155-
156- for ( const [ _key , value ] of intervals ) {
157- clearInterval ( value )
158- }
159- intervals . clear ( )
143+ const dispose = ( ) => {
144+ for ( const [ _key , value ] of timeouts ) {
145+ clearTimeout ( value )
146+ }
147+ timeouts . clear ( )
148+ timeoutCounter = 0
149+
150+ for ( const [ _key , value ] of immediates ) {
151+ clearTimeout ( value )
152+ }
153+ immediates . clear ( )
154+ immediateCounter = 0
155+
156+ for ( const [ _key , value ] of intervals ) {
157+ clearInterval ( value )
158+ }
159+ intervals . clear ( )
160160 intervalCounter = 0
161161
162162 scope . dispose ( )
0 commit comments