Skip to content

Commit b4e9a30

Browse files
Fix #2338 - Added explicit fs.revoke(path) (#2368)
1 parent a129be8 commit b4e9a30

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

core/src/stdlib/pyscript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/stdlib/pyscript/fs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ async def mount(path, mode="readwrite", root="", id="pyscript"):
4949
mounted[path] = await interpreter.mountNativeFS(path, handler)
5050

5151

52+
async def revoke(path, id="pyscript"):
53+
from _pyscript import fs, interpreter
54+
from pyscript.magic_js import (
55+
RUNNING_IN_WORKER,
56+
sync,
57+
)
58+
59+
uid = f"{path}@{id}"
60+
61+
if RUNNING_IN_WORKER:
62+
had = sync.deleteFSHandler(uid)
63+
else:
64+
had = await fs.idb.has(uid)
65+
if had:
66+
had = await fs.idb.delete(uid)
67+
68+
if had:
69+
interpreter._module.FS.unmount(path)
70+
71+
return had
72+
73+
5274
async def sync(path):
5375
await mounted[path].syncfs()
5476

core/src/sync.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default {
1616
* Ask a user action via dialog and returns the directory handler once granted.
1717
* @param {string} uid
1818
* @param {{id?:string, mode?:"read"|"readwrite", hint?:"desktop"|"documents"|"downloads"|"music"|"pictures"|"videos"}} options
19-
* @returns {boolean}
19+
* @returns {Promise<boolean>}
2020
*/
2121
async storeFSHandler(uid, options = {}) {
2222
if (await idb.has(uid)) return true;
@@ -28,4 +28,15 @@ export default {
2828
() => false,
2929
);
3030
},
31+
32+
/**
33+
* Explicitly remove the unique identifier for the FS handler.
34+
* @param {string} uid
35+
* @returns {Promise<boolean>}
36+
*/
37+
async deleteFSHandler(uid) {
38+
const had = await idb.has(uid);
39+
if (had) await idb.delete(uid);
40+
return had;
41+
},
3142
};

core/tests/manual/fs/index.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
await fs.sync("/persistent")
2121

22+
# await fs.revoke("/persistent")
23+
2224
elif not RUNNING_IN_WORKER:
2325
from pyscript import document
2426

@@ -39,7 +41,7 @@ async def mount(event):
3941
js.alert("unable to grant access")
4042

4143
async def unmount(event):
42-
await fs.unmount("/persistent")
44+
await fs.revoke("/persistent")
4345
button.textContent = "mount"
4446
button.onclick = mount
4547

core/types/sync.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ declare namespace _default {
99
* Ask a user action via dialog and returns the directory handler once granted.
1010
* @param {string} uid
1111
* @param {{id?:string, mode?:"read"|"readwrite", hint?:"desktop"|"documents"|"downloads"|"music"|"pictures"|"videos"}} options
12-
* @returns {boolean}
12+
* @returns {Promise<boolean>}
1313
*/
1414
function storeFSHandler(uid: string, options?: {
1515
id?: string;
1616
mode?: "read" | "readwrite";
1717
hint?: "desktop" | "documents" | "downloads" | "music" | "pictures" | "videos";
18-
}): boolean;
18+
}): Promise<boolean>;
19+
/**
20+
* Explicitly remove the unique identifier for the FS handler.
21+
* @param {string} uid
22+
* @returns {Promise<boolean>}
23+
*/
24+
function deleteFSHandler(uid: string): Promise<boolean>;
1925
}
2026
export default _default;

0 commit comments

Comments
 (0)