@cfasim-ui/pyodide 0.1.8 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cfasim-ui/pyodide",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "type": "module",
5
5
  "description": "Pyodide (Python-in-browser) integration for cfasim-ui",
6
6
  "license": "Apache-2.0",
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "pyodide": "^0.29.3",
24
- "@cfasim-ui/shared": "0.1.8"
24
+ "@cfasim-ui/shared": "0.1.9"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "vue": "^3.5.0"
@@ -14,7 +14,6 @@ interface WorkerMessage {
14
14
  }
15
15
 
16
16
  let wheelMap: Record<string, string> = {};
17
- const loadedModules = new Set<string>();
18
17
 
19
18
  const baseUrl = import.meta.env.BASE_URL ?? "/";
20
19
 
@@ -44,28 +43,43 @@ const pyodideReadyPromise = (async () => {
44
43
  return pyodide;
45
44
  })();
46
45
 
47
- let wheelsInstalled = false;
46
+ let installPromise: Promise<void> | null = null;
48
47
 
49
- async function installAllWheels() {
50
- if (wheelsInstalled) return;
51
- wheelsInstalled = true;
52
- const urls = Object.values(wheelMap).map(
53
- (f) => `${self.location.origin}${baseUrl}${f}`,
54
- );
55
- if (urls.length > 0) {
56
- await micropip.install(urls);
48
+ function installAllWheels(): Promise<void> {
49
+ if (!installPromise) {
50
+ installPromise = (async () => {
51
+ const urls = Object.values(wheelMap).map(
52
+ (f) => `${self.location.origin}${baseUrl}${f}`,
53
+ );
54
+ if (urls.length > 0) {
55
+ await micropip.install(urls);
56
+ }
57
+ })();
58
+ installPromise.catch(() => {
59
+ installPromise = null;
60
+ });
57
61
  }
62
+ return installPromise;
58
63
  }
59
64
 
60
- async function ensureModule(
65
+ const modulePromises = new Map<string, Promise<void>>();
66
+
67
+ function ensureModule(
61
68
  pyodide: Awaited<typeof pyodideReadyPromise>,
62
69
  moduleName: string,
63
- ) {
64
- if (loadedModules.has(moduleName)) return;
65
- if (!wheelMap[moduleName]) throw new Error(`Unknown module: ${moduleName}`);
66
- await installAllWheels();
67
- pyodide.pyimport(moduleName);
68
- loadedModules.add(moduleName);
70
+ ): Promise<void> {
71
+ if (!modulePromises.has(moduleName)) {
72
+ if (!wheelMap[moduleName]) throw new Error(`Unknown module: ${moduleName}`);
73
+ const promise = (async () => {
74
+ await installAllWheels();
75
+ pyodide.pyimport(moduleName);
76
+ })();
77
+ promise.catch(() => {
78
+ modulePromises.delete(moduleName);
79
+ });
80
+ modulePromises.set(moduleName, promise);
81
+ }
82
+ return modulePromises.get(moduleName)!;
69
83
  }
70
84
 
71
85
  // Map Python struct format characters to TypedArray constructors