Skip to content

Commit

Permalink
make window available in web worker
Browse files Browse the repository at this point in the history
  • Loading branch information
crwen authored and ethe committed Nov 27, 2024
1 parent 4e7e7d0 commit af66ff6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 2 additions & 0 deletions fusio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ web-sys = { version = "0.3", optional = true, features = [
"Storage",
"StorageManager",
"Window",
"WorkerGlobalScope",
"WorkerNavigator",

] }

Expand Down
6 changes: 3 additions & 3 deletions fusio/src/impls/disk/opfs/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
error::wasm_err,
fs::{FileMeta, FileSystemTag, Fs, OpenOptions},
path::Path,
Error, MaybeSend,
Error,
};

pub struct OPFS;
Expand Down Expand Up @@ -122,7 +122,7 @@ impl OPFS {
path: &Path,
options: &FileSystemGetDirectoryOptions,
) -> Result<FileSystemDirectoryHandle, Error> {
let mut parent = storage().await;
let mut parent = storage().await?;
let segments: Vec<&str> = path.as_ref().trim_matches('/').split("/").collect();

if segments.len() == 1 && segments[0].is_empty() {
Expand All @@ -148,7 +148,7 @@ impl OPFS {
path: &Path,
options: &FileSystemGetDirectoryOptions,
) -> Result<FileSystemDirectoryHandle, Error> {
let mut parent = storage().await;
let mut parent = storage().await?;
let segments: Vec<&str> = path.as_ref().trim_matches('/').split("/").collect();
let part_len = segments.len();

Expand Down
23 changes: 15 additions & 8 deletions fusio/src/impls/disk/opfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use wasm_bindgen_futures::JsFuture;
use web_sys::{
wasm_bindgen::JsCast, window, File, FileSystemCreateWritableOptions, FileSystemDirectoryHandle,
FileSystemFileHandle, FileSystemWritableFileStream, ReadableStreamDefaultReader,
ReadableStreamReadResult,
ReadableStreamReadResult, WorkerGlobalScope,
};

use crate::{error::wasm_err, Error, IoBuf, IoBufMut, Read, Write};
Expand Down Expand Up @@ -257,11 +257,18 @@ impl Read for OPFSFile {
}
}

pub(crate) async fn storage() -> FileSystemDirectoryHandle {
let storage_promise = window().unwrap().navigator().storage().get_directory();
JsFuture::from(storage_promise)
.await
.unwrap()
.dyn_into::<FileSystemDirectoryHandle>()
.unwrap()
pub(crate) async fn storage() -> Result<FileSystemDirectoryHandle, Error> {
let storage_promise = match window() {
Some(window) => window.navigator().storage(),
None => match js_sys::eval("self")
.unwrap()
.dyn_into::<WorkerGlobalScope>()
{
Ok(worker) => worker.navigator().storage(),
Err(err) => return Err(wasm_err(err)),
},
}
.get_directory();

promise::<FileSystemDirectoryHandle>(storage_promise).await
}

0 comments on commit af66ff6

Please sign in to comment.