refactor(fs): reduce overhead of `watch`

pull/2613/head
Tony 3 months ago
parent 2445e103f2
commit cd8691831b
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

@ -0,0 +1,6 @@
---
fs: minor
fs-js: minor
---
Reduce the overhead of `watch` and `unwatch`

@ -2,16 +2,18 @@
import * as fs from "@tauri-apps/plugin-fs";
import { convertFileSrc } from "@tauri-apps/api/core";
import { arrayBufferToBase64 } from "../lib/utils";
import { onDestroy } from "svelte";
export let onMessage;
export let insecureRenderHtml;
let path = "";
let img;
/** @type {fs.FileHandle} */
let file;
let renameTo;
let watchPath = "";
let watchDebounceDelay = 0;
let watchDebounceDelay = "0";
let watchRecursive = false;
let unwatchFn;
let unwatchPath = "";
@ -118,7 +120,7 @@
.getElementById("file-save")
.addEventListener("click", function () {
fs.writeTextFile(path, fileInput.value, {
dir: getDir(),
baseDir: getDir(),
}).catch(onMessage);
});
});
@ -170,6 +172,15 @@
unwatchFn = undefined;
unwatchPath = undefined;
}
onDestroy(() => {
if (file) {
file.close();
}
if (unwatchFn) {
unwatchFn();
}
})
</script>
<div class="flex flex-col">

File diff suppressed because one or more lines are too long

@ -101,6 +101,7 @@ const COMMANDS: &[(&str, &[&str])] = &[
("fstat", &[]),
("exists", &[]),
("watch", &[]),
// TODO: Remove this in v3
("unwatch", &[]),
("size", &[]),
];

@ -1245,15 +1245,23 @@ type WatchEventKindRemove =
| { kind: 'folder' }
| { kind: 'other' }
// TODO: Remove this in v3, return `Watcher` instead
/**
* @since 2.0.0
*/
type UnwatchFn = () => void
async function unwatch(rid: number): Promise<void> {
await invoke('plugin:fs|unwatch', { rid })
class Watcher extends Resource {
constructor(rid: number) {
super(rid)
}
unwatch = () => {
void this.close()
}
}
// TODO: Return `Watcher` instead in v3
/**
* Watch changes (after a delay) on files or directories.
*
@ -1287,11 +1295,12 @@ async function watch(
onEvent
})
return () => {
void unwatch(rid)
}
const watcher = new Watcher(rid)
return watcher.unwatch
}
// TODO: Return `Watcher` instead in v3
/**
* Watch changes on files or directories.
*
@ -1325,9 +1334,9 @@ async function watchImmediate(
onEvent
})
return () => {
void unwatch(rid)
}
const watcher = new Watcher(rid)
return watcher.unwatch
}
/**

@ -420,8 +420,6 @@ pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
commands::size,
#[cfg(feature = "watch")]
watcher::watch,
#[cfg(feature = "watch")]
watcher::unwatch
])
.setup(|app, api| {
let scope = Scope {

@ -2,8 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use notify_debouncer_full::{new_debouncer, DebounceEventResult, Debouncer, RecommendedCache};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use notify_debouncer_full::{new_debouncer, DebouncedEvent, Debouncer, RecommendedCache};
use serde::Deserialize;
use tauri::{
ipc::{Channel, CommandScope, GlobalScope},
@ -11,15 +11,7 @@ use tauri::{
Manager, Resource, ResourceId, Runtime, Webview,
};
use std::{
path::PathBuf,
sync::{
mpsc::{channel, Receiver},
Mutex,
},
thread::spawn,
time::Duration,
};
use std::time::Duration;
use crate::{
commands::{resolve_path, CommandResult},
@ -27,51 +19,13 @@ use crate::{
SafeFilePath,
};
struct InnerWatcher {
pub kind: WatcherKind,
paths: Vec<PathBuf>,
}
pub struct WatcherResource(Mutex<InnerWatcher>);
impl WatcherResource {
fn new(kind: WatcherKind, paths: Vec<PathBuf>) -> Self {
Self(Mutex::new(InnerWatcher { kind, paths }))
}
fn with_lock<R, F: FnMut(&mut InnerWatcher) -> R>(&self, mut f: F) -> R {
let mut watcher = self.0.lock().unwrap();
f(&mut watcher)
}
}
impl Resource for WatcherResource {}
#[allow(unused)]
enum WatcherKind {
Debouncer(Debouncer<RecommendedWatcher, RecommendedCache>),
Watcher(RecommendedWatcher),
}
fn watch_raw(on_event: Channel<Event>, rx: Receiver<notify::Result<Event>>) {
spawn(move || {
while let Ok(event) = rx.recv() {
if let Ok(event) = event {
// TODO: Should errors be emitted too?
let _ = on_event.send(event);
}
}
});
}
fn watch_debounced(on_event: Channel<Event>, rx: Receiver<DebounceEventResult>) {
spawn(move || {
while let Ok(Ok(events)) = rx.recv() {
for event in events {
// TODO: Should errors be emitted too?
let _ = on_event.send(event.event);
}
}
});
}
impl Resource for WatcherKind {}
#[derive(Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
@ -86,7 +40,7 @@ pub async fn watch<R: Runtime>(
webview: Webview<R>,
paths: Vec<SafeFilePath>,
options: WatchOptions,
on_event: Channel<Event>,
on_event: Channel<notify::Event>,
global_scope: GlobalScope<Entry>,
command_scope: CommandScope<Entry>,
) -> CommandResult<ResourceId> {
@ -107,52 +61,40 @@ pub async fn watch<R: Runtime>(
RecursiveMode::NonRecursive
};
let kind = if let Some(delay) = options.delay_ms {
let (tx, rx) = channel();
let mut debouncer = new_debouncer(Duration::from_millis(delay), None, tx)?;
let watcher_kind = if let Some(delay) = options.delay_ms {
let mut debouncer = new_debouncer(
Duration::from_millis(delay),
None,
move |events: Result<Vec<DebouncedEvent>, Vec<notify::Error>>| {
if let Ok(events) = events {
for event in events {
// TODO: Should errors be emitted too?
let _ = on_event.send(event.event);
}
}
},
)?;
for path in &resolved_paths {
debouncer.watch(path, recursive_mode)?;
}
watch_debounced(on_event, rx);
WatcherKind::Debouncer(debouncer)
} else {
let (tx, rx) = channel();
let mut watcher = RecommendedWatcher::new(tx, Config::default())?;
let mut watcher = RecommendedWatcher::new(
move |event| {
if let Ok(event) = event {
// TODO: Should errors be emitted too?
let _ = on_event.send(event);
}
},
Config::default(),
)?;
for path in &resolved_paths {
watcher.watch(path, recursive_mode)?;
}
watch_raw(on_event, rx);
WatcherKind::Watcher(watcher)
};
let rid = webview
.resources_table()
.add(WatcherResource::new(kind, resolved_paths));
let rid = webview.resources_table().add(watcher_kind);
Ok(rid)
}
#[tauri::command]
pub async fn unwatch<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> CommandResult<()> {
let watcher = webview.resources_table().take::<WatcherResource>(rid)?;
WatcherResource::with_lock(&watcher, |watcher| {
match &mut watcher.kind {
WatcherKind::Debouncer(ref mut debouncer) => {
for path in &watcher.paths {
debouncer.unwatch(path).map_err(|e| {
format!("failed to unwatch path: {} with error: {e}", path.display())
})?;
}
}
WatcherKind::Watcher(ref mut w) => {
for path in &watcher.paths {
w.unwatch(path).map_err(|e| {
format!("failed to unwatch path: {} with error: {e}", path.display())
})?;
}
}
}
Ok(())
})
}

Loading…
Cancel
Save