Skip to content

Commit f4bf6c9

Browse files
fix: prevent memory leaks in clearApiCache
1 parent 02bca04 commit f4bf6c9

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Diff for: src/lib/py/api.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
import type { PyCallable, PyProxy } from "pyodide/ffi";
2+
13
import { py } from "./load";
24

3-
const cache = new Map<string, any>();
5+
const cache = new Map<string, PyProxy>();
46

57
export function getApi<T = () => any>(slug: string): T {
68
if (cache.has(slug)) {
79
return cache.get(slug) as T;
810
} else {
9-
const api = py.pyimport(slug) as T;
11+
const api = py.pyimport(slug) as T & PyCallable;
1012
cache.set(slug, api);
1113
return api;
1214
}
1315
}
1416

1517
export function clearApiCache() {
18+
cache.forEach(value => value.destroy());
1619
cache.clear();
1720
}

0 commit comments

Comments
 (0)