Skip to content

Commit f3f4784

Browse files
committed
fix: (fs) rm also removes signal
1 parent 0037773 commit f3f4784

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

.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": minor
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
@@ -11,7 +11,7 @@ import {
1111
makeTauriFileSystem,
1212
rsync,
1313
} from "../src/index.js";
14-
import { createEffect, createRoot } from "solid-js";
14+
import { catchError, createEffect, createRoot } from "solid-js";
1515

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

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

0 commit comments

Comments
 (0)