feat(fs): improve `readTextFile` performance

pull/1962/head
amrbashir 9 months ago
parent 525abc4be5
commit 91424276aa
No known key found for this signature in database
GPG Key ID: BBD7A47A2003FF33

File diff suppressed because one or more lines are too long

@ -758,10 +758,14 @@ async function readTextFile(
throw new TypeError('Must be a file URL.') throw new TypeError('Must be a file URL.')
} }
return await invoke<string>('plugin:fs|read_text_file', { const arr = await invoke<ArrayBuffer | number[]>('plugin:fs|read_text_file', {
path: path instanceof URL ? path.toString() : path, path: path instanceof URL ? path.toString() : path,
options options
}) })
const bytes = arr instanceof ArrayBuffer ? arr : Uint8Array.from(arr)
return new TextDecoder().decode(bytes)
} }
/** /**

@ -369,6 +369,7 @@ pub async fn read_file<R: Runtime>(
Ok(tauri::ipc::Response::new(contents)) Ok(tauri::ipc::Response::new(contents))
} }
// TODO, remove in v3, rely on `read_file` command instead
#[tauri::command] #[tauri::command]
pub async fn read_text_file<R: Runtime>( pub async fn read_text_file<R: Runtime>(
webview: Webview<R>, webview: Webview<R>,
@ -376,33 +377,8 @@ pub async fn read_text_file<R: Runtime>(
command_scope: CommandScope<Entry>, command_scope: CommandScope<Entry>,
path: SafeFilePath, path: SafeFilePath,
options: Option<BaseOptions>, options: Option<BaseOptions>,
) -> CommandResult<String> { ) -> CommandResult<tauri::ipc::Response> {
let (mut file, path) = resolve_file( read_file(webview, global_scope, command_scope, path, options).await
&webview,
&global_scope,
&command_scope,
path,
OpenOptions {
base: BaseOptions {
base_dir: options.as_ref().and_then(|o| o.base_dir),
},
options: crate::OpenOptions {
read: true,
..Default::default()
},
},
)?;
let mut contents = String::new();
file.read_to_string(&mut contents).map_err(|e| {
format!(
"failed to read file as text at path: {} with error: {e}",
path.display()
)
})?;
Ok(contents)
} }
#[tauri::command] #[tauri::command]
@ -802,10 +778,11 @@ fn default_create_value() -> bool {
true true
} }
fn write_file_inner<R: Runtime>( #[tauri::command]
pub async fn write_file<R: Runtime>(
webview: Webview<R>, webview: Webview<R>,
global_scope: &GlobalScope<Entry>, global_scope: GlobalScope<Entry>,
command_scope: &CommandScope<Entry>, command_scope: CommandScope<Entry>,
request: tauri::ipc::Request<'_>, request: tauri::ipc::Request<'_>,
) -> CommandResult<()> { ) -> CommandResult<()> {
let data = match request.body() { let data = match request.body() {
@ -836,8 +813,8 @@ fn write_file_inner<R: Runtime>(
let (mut file, path) = resolve_file( let (mut file, path) = resolve_file(
&webview, &webview,
global_scope, &global_scope,
command_scope, &command_scope,
path, path,
if let Some(opts) = options { if let Some(opts) = options {
OpenOptions { OpenOptions {
@ -880,17 +857,7 @@ fn write_file_inner<R: Runtime>(
.map_err(Into::into) .map_err(Into::into)
} }
#[tauri::command] // TODO, remove in v3, rely on `write_file` command instead
pub async fn write_file<R: Runtime>(
webview: Webview<R>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
request: tauri::ipc::Request<'_>,
) -> CommandResult<()> {
write_file_inner(webview, &global_scope, &command_scope, request)
}
// TODO, in v3, remove this command and rely on `write_file` command only
#[tauri::command] #[tauri::command]
pub async fn write_text_file<R: Runtime>( pub async fn write_text_file<R: Runtime>(
webview: Webview<R>, webview: Webview<R>,
@ -898,7 +865,7 @@ pub async fn write_text_file<R: Runtime>(
command_scope: CommandScope<Entry>, command_scope: CommandScope<Entry>,
request: tauri::ipc::Request<'_>, request: tauri::ipc::Request<'_>,
) -> CommandResult<()> { ) -> CommandResult<()> {
write_file_inner(webview, &global_scope, &command_scope, request) write_file(webview, global_scope, command_scope, request).await
} }
#[tauri::command] #[tauri::command]

Loading…
Cancel
Save