-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathcanSaveDisks.ts
46 lines (39 loc) · 1.54 KB
/
canSaveDisks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import Worker from "./canSaveDisks-worker?worker&inline";
export function canSaveDisks() {
if (!navigator.storage?.getDirectory) {
return false;
}
if (typeof FileSystemFileHandle !== "function") {
return false;
}
if (workerSyncFileSupport !== "unknown") {
return workerSyncFileSupport === "supported";
}
function update(support: "supported" | "unsupported") {
if (workerSyncFileSupport === "unsupported") {
// Ignore updates if one mechanism determined that it's unsupported.
return;
}
workerSyncFileSupport = support;
localStorage["workerSyncFileSupport"] = support;
}
// We can't check for the presence of FileSystemSyncAccessHandle in the
// main window, it's only available in workers. So we have to create a
// temporary one to check, but we cache the result in localStorage so that
// we can avoid doing this every time.
if (!worker) {
worker = new Worker();
worker.addEventListener("message", e => {
update(e.data ? "supported" : "unsupported");
worker?.terminate();
});
}
// In private contexts Safari exposes the storage API, but calls to it will
// fail, check for that too.
navigator.storage.getDirectory().catch(() => update("unsupported"));
// Assume supported until we hear otherwise.
return true;
}
let workerSyncFileSupport: "unknown" | "supported" | "unsupported" =
localStorage["workerSyncFileSupport"] ?? "unknown";
let worker: Worker | undefined;