Skip to content

Commit a46ce38

Browse files
Copilotna-trium-144
andcommitted
Extract readAllFiles helper function to reduce duplication
Co-authored-by: na-trium-144 <[email protected]>
1 parent 2d8bb3c commit a46ce38

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

public/pyodide.worker.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
let pyodide;
33
let pyodideOutput = [];
44

5+
// Helper function to read all files from the Pyodide file system
6+
function readAllFiles() {
7+
const dirFiles = pyodide.FS.readdir(HOME);
8+
const updatedFiles = [];
9+
for (const filename of dirFiles) {
10+
if (filename === "." || filename === "..") continue;
11+
const filepath = HOME + filename;
12+
const stat = pyodide.FS.stat(filepath);
13+
if (pyodide.FS.isFile(stat.mode)) {
14+
const content = pyodide.FS.readFile(filepath, { encoding: "utf8" });
15+
updatedFiles.push([filename, content]);
16+
}
17+
}
18+
return updatedFiles;
19+
}
20+
521
async function init(id, payload) {
622
const { PYODIDE_CDN, interruptBuffer } = payload;
723
if (!pyodide) {
@@ -73,18 +89,7 @@ async function runPython(id, payload) {
7389
}
7490
}
7591

76-
// Use Pyodide FS API to read all files
77-
const dirFiles = pyodide.FS.readdir(HOME);
78-
const updatedFiles = [];
79-
for (const filename of dirFiles) {
80-
if (filename === "." || filename === "..") continue;
81-
const filepath = HOME + filename;
82-
const stat = pyodide.FS.stat(filepath);
83-
if (pyodide.FS.isFile(stat.mode)) {
84-
const content = pyodide.FS.readFile(filepath, { encoding: "utf8" });
85-
updatedFiles.push([filename, content]);
86-
}
87-
}
92+
const updatedFiles = readAllFiles();
8893

8994
const output = [...pyodideOutput];
9095
pyodideOutput = []; // 出力をクリア
@@ -145,18 +150,7 @@ async function runFile(id, payload) {
145150
}
146151
}
147152

148-
// Use Pyodide FS API to read all files
149-
const dirFiles = pyodide.FS.readdir(HOME);
150-
const updatedFiles = [];
151-
for (const filename of dirFiles) {
152-
if (filename === "." || filename === "..") continue;
153-
const filepath = HOME + filename;
154-
const stat = pyodide.FS.stat(filepath);
155-
if (pyodide.FS.isFile(stat.mode)) {
156-
const content = pyodide.FS.readFile(filepath, { encoding: "utf8" });
157-
updatedFiles.push([filename, content]);
158-
}
159-
}
153+
const updatedFiles = readAllFiles();
160154

161155
const output = [...pyodideOutput];
162156
pyodideOutput = []; // 出力をクリア

0 commit comments

Comments
 (0)