Skip to content

Commit 940efec

Browse files
Merge pull request #26 from sebastianwessel/7-improve-settimeout-and-setinterval
fix: Improve setTimeout and setInterval #7
2 parents 1847c7f + 2627723 commit 940efec

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/provideEnv.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ export const provideEnv = (arena: Arena, options: RuntimeOptions) => {
1414
dangerousSync = arena.sync(options.dangerousSync)
1515
}
1616

17+
const dispose = () => {
18+
for (const [_key, value] of timeouts) {
19+
clearTimeout(value)
20+
}
21+
timeouts.clear()
22+
23+
for (const [_key, value] of intervals) {
24+
clearInterval(value)
25+
}
26+
intervals.clear()
27+
}
28+
1729
arena.expose({
1830
__dangerousSync: dangerousSync,
1931
env: options.env ?? {},
@@ -30,6 +42,7 @@ export const provideEnv = (arena: Arena, options: RuntimeOptions) => {
3042
const timeout = timeouts.get(id)
3143
if (timeout) {
3244
clearTimeout(timeout)
45+
timeouts.delete(id)
3346
}
3447
},
3548
setInterval: (fn: () => void, time: number) => {
@@ -43,7 +56,10 @@ export const provideEnv = (arena: Arena, options: RuntimeOptions) => {
4356
const interval = intervals.get(id)
4457
if (interval) {
4558
clearInterval(interval)
59+
intervals.delete(id)
4660
}
4761
},
4862
})
63+
64+
return { dispose }
4965
}

src/quickJS.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const quickJS = async (wasmVariantName = '@jitl/quickjs-ng-wasmfile-relea
5353

5454
provideFs(arena, runtimeOptions, fs)
5555
provideConsole(arena, runtimeOptions)
56-
provideEnv(arena, runtimeOptions)
56+
const { dispose: disposeEnvironment } = provideEnv(arena, runtimeOptions)
5757
provideHttp(arena, runtimeOptions, { fs: runtimeOptions.allowFs ? fs : undefined })
5858

5959
await arena.evalCode(`
@@ -64,6 +64,12 @@ export const quickJS = async (wasmVariantName = '@jitl/quickjs-ng-wasmfile-relea
6464

6565
const dispose = () => {
6666
let err: unknown
67+
try {
68+
disposeEnvironment()
69+
} catch (error) {
70+
err = error
71+
console.error('Failed to dispose environment')
72+
}
6773
try {
6874
arena.dispose()
6975
} catch (error) {

0 commit comments

Comments
 (0)