Skip to content

Commit

Permalink
feat(core): reimplement readTextFile for performance (#3631)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 7, 2022
1 parent 06ab85b commit 834ccc5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .changes/read-fs-read-file.md
@@ -0,0 +1,6 @@
---
"tauri": patch
"api": patch
---

Reimplement endpoint to read file as string for performance.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion core/tauri/src/endpoints/file_system.rs
Expand Up @@ -72,11 +72,16 @@ pub struct FileOperationOptions {
#[derive(Deserialize, CommandModule)]
#[serde(tag = "cmd", rename_all = "camelCase")]
pub enum Cmd {
/// The read text file API.
/// The read binary file API.
ReadFile {
path: SafePathBuf,
options: Option<FileOperationOptions>,
},
/// The read binary file API.
ReadTextFile {
path: SafePathBuf,
options: Option<FileOperationOptions>,
},
/// The write file API.
WriteFile {
path: SafePathBuf,
Expand Down Expand Up @@ -137,6 +142,24 @@ impl Cmd {
.map_err(Into::into)
}

#[module_command_handler(fs_read_file, "fs > readFile")]
fn read_text_file<R: Runtime>(
context: InvokeContext<R>,
path: SafePathBuf,
options: Option<FileOperationOptions>,
) -> super::Result<String> {
let resolved_path = resolve_path(
&context.config,
&context.package_info,
&context.window,
path,
options.and_then(|o| o.dir),
)?;
file::read_string(&resolved_path)
.with_context(|| format!("path: {}", resolved_path.0.display()))
.map_err(Into::into)
}

#[module_command_handler(fs_write_file, "fs > writeFile")]
fn write_file<R: Runtime>(
context: InvokeContext<R>,
Expand Down
2 changes: 1 addition & 1 deletion examples/api/dist/assets/index.js

Large diffs are not rendered by default.

24 changes: 0 additions & 24 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tooling/api/src/fs.ts
Expand Up @@ -139,14 +139,14 @@ async function readTextFile(
filePath: string,
options: FsOptions = {}
): Promise<string> {
return invokeTauriCommand<number[]>({
return invokeTauriCommand<string>({
__tauriModule: 'Fs',
message: {
cmd: 'readFile',
cmd: 'readTextFile',
path: filePath,
options
}
}).then((data) => new TextDecoder().decode(new Uint8Array(data)))
})
}

/**
Expand Down

0 comments on commit 834ccc5

Please sign in to comment.