Skip to content

Commit

Permalink
Merge pull request #26 from sebastianwessel/7-improve-settimeout-and-…
Browse files Browse the repository at this point in the history
…setinterval

fix: Improve setTimeout and setInterval #7
  • Loading branch information
sebastianwessel authored Jul 11, 2024
2 parents 1847c7f + 2627723 commit 940efec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/provideEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ export const provideEnv = (arena: Arena, options: RuntimeOptions) => {
dangerousSync = arena.sync(options.dangerousSync)
}

const dispose = () => {
for (const [_key, value] of timeouts) {
clearTimeout(value)
}
timeouts.clear()

for (const [_key, value] of intervals) {
clearInterval(value)
}
intervals.clear()
}

arena.expose({
__dangerousSync: dangerousSync,
env: options.env ?? {},
Expand All @@ -30,6 +42,7 @@ export const provideEnv = (arena: Arena, options: RuntimeOptions) => {
const timeout = timeouts.get(id)
if (timeout) {
clearTimeout(timeout)
timeouts.delete(id)
}
},
setInterval: (fn: () => void, time: number) => {
Expand All @@ -43,7 +56,10 @@ export const provideEnv = (arena: Arena, options: RuntimeOptions) => {
const interval = intervals.get(id)
if (interval) {
clearInterval(interval)
intervals.delete(id)
}
},
})

return { dispose }
}
8 changes: 7 additions & 1 deletion src/quickJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const quickJS = async (wasmVariantName = '@jitl/quickjs-ng-wasmfile-relea

provideFs(arena, runtimeOptions, fs)
provideConsole(arena, runtimeOptions)
provideEnv(arena, runtimeOptions)
const { dispose: disposeEnvironment } = provideEnv(arena, runtimeOptions)
provideHttp(arena, runtimeOptions, { fs: runtimeOptions.allowFs ? fs : undefined })

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

const dispose = () => {
let err: unknown
try {
disposeEnvironment()
} catch (error) {
err = error
console.error('Failed to dispose environment')
}
try {
arena.dispose()
} catch (error) {
Expand Down

0 comments on commit 940efec

Please sign in to comment.