Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose web_sys::File via FileEngine #1081

Closed
wooden-worm opened this issue Jun 9, 2023 · 4 comments · Fixed by #1258
Closed

Expose web_sys::File via FileEngine #1081

wooden-worm opened this issue Jun 9, 2023 · 4 comments · Fixed by #1258
Labels
enhancement New feature or request html Related to the html crate

Comments

@wooden-worm
Copy link
Contributor

Specific Demand

On wasm I need to access the raw web_sys::File object in order to deal with big files (bigger than available memory), however, FileEngine only provides read_to_string and read_to_bytes.

Implement Suggestion

@ealmloff ealmloff added enhancement New feature or request html Related to the html crate labels Jun 19, 2023
@ealmloff
Copy link
Member

This would be a great addition!

Dioxus does not include #[cfg...] statements for each platform so that other external renderer can be implemented. If we did take the config approach it would prevent a renderer like freya from using the html crate with it's own file engine type.

Instead we could expose the inner type as Any type and allow users to downcast it to a platform specific type. We do something similar to this with the onmounted event. We could expose an extension trait on each platform to make downcasting to the platform type more easy:

#[async_trait::async_trait(?Send)]
pub trait FileEngine {
    // get a list of file names
    fn files(&self) -> Vec<String>;

    // read a file to bytes
    async fn read_file(&self, file: &str) -> Option<Vec<u8>>;
    
    async fn read_native_file(&self, file: &str) -> Option<Box<dyn Any>>;

    // read a file to string
    async fn read_file_to_string(&self, file: &str) -> Option<String>;
}

trait WebFileEngineExt {
    fn read_web_file(&self, file: &str) -> Option<web_sys::File>;
}

impl WebFileEngineExt for std::sync::Arc<dyn FileEngine> {
    fn read_web_file(&self, file: &str) -> Option<web_sys::File> {
        self.read_native_file().map(|file| *file.downcast())
    }
}

@ealmloff
Copy link
Member

also related to #93

@wooden-worm
Copy link
Contributor Author

I can work on a PR if the above approach is acceptable, let me know.

@ealmloff
Copy link
Member

I can work on a PR if the above approach is acceptable, let me know.

That would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request html Related to the html crate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants