Skip to content

Commit 96a1ebd

Browse files
authored
fix: (fs) rm also removes signal (#796)
2 parents 461ab9e + db2de8e commit 96a1ebd

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

.changeset/clean-baboons-kneel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solid-primitives/filesystem": patch
3+
---
4+
5+
fix: removing a file deletes its signal correctly

packages/filesystem/src/reactive.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ export const createSyncFileSystem = (
112112
getTypeMap.delete(item);
113113
}
114114
});
115+
if (readFileMap.has(path)) {
116+
readFileMap.get(path)?.[1](undefined);
117+
readFileMap.delete(path);
118+
}
115119
readdirMap.get(getParentDir(path))?.[1](
116120
(items = []) => items.filter(item => item === getItemName(path)) as [] | DirEntries,
117121
);

packages/filesystem/test/index.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
makeTauriFileSystem,
1313
rsync,
1414
} from "../src/index.js";
15-
import { createEffect, createRoot } from "solid-js";
15+
import { catchError, createEffect, createRoot } from "solid-js";
1616

1717
describe("makeNoFileSystem", () => {
1818
const fs = makeNoFileSystem();
@@ -52,6 +52,15 @@ describe("makeVirtualFileSystem", () => {
5252
});
5353
test("fs.readFile returns file content", () =>
5454
expect(fs.readFile("src/test.ts")).toBe("// test"));
55+
test("fs.readFile throws on attempting to read a directory as file", () => {
56+
expect(() => fs.readFile("src")).toThrow('"src" is not a file');
57+
});
58+
test("fs.readFile throws on attempting to read a non-existent file", () => {
59+
expect(() => fs.readFile("src/nonexistent.ts")).toThrow('"src/nonexistent.ts" is not a file');
60+
});
61+
test("fs.readFile throws on attempting to read from a non-existing directory", () => {
62+
expect(() => fs.readFile("nonexistent/test.ts")).toThrow('"nonexistent" is not a directory')
63+
});
5564
test("fs.writeFile creates and overwrites file", () => {
5665
expect(fs.readdir("src")).toHaveLength(1);
5766
fs.writeFile("src/test2.ts", "// data");
@@ -115,6 +124,20 @@ describe("createFileSystem (sync) calls the underlying fs", () => {
115124
});
116125
});
117126

127+
describe("createFileSystem (sync) relays file system errors", () => {
128+
test("a deleted file stored in a signal throws an error", () => new Promise<void>((done, fail) => {
129+
setTimeout(() => fail(new Error('did not throw')), 100);
130+
const fs = createFileSystem(makeVirtualFileSystem({ 'test.json': '{}' }));
131+
catchError(() => {
132+
createEffect(() => fs.readFile("test.json"));
133+
setTimeout(() => fs.rm("test.json"), 30);
134+
}, (error) => {
135+
expect(error).toEqual(new Error('"test.json" is not a file'));
136+
done();
137+
});
138+
}));
139+
});
140+
118141
describe("createFileSystem (async) calls the underlying fs", () => {
119142
const afs = makeNoAsyncFileSystem();
120143
instrumentFs(afs);

0 commit comments

Comments
 (0)