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

feat(fs/watch): migrate to notify-debouncer-full #885

Merged
merged 7 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/fs-replace-notify-debouncer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"fs": "patch"
"fs-js": "patch"
---

Replace `notify-debouncer-mini` with `notify-debouncer-full`. [(plugins-workspace#885)](https://github.com/tauri-apps/plugins-workspace/pull/885)
21 changes: 16 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion examples/api/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ serde = { workspace = true }
tiny_http = "0.11"
log = { workspace = true }
tauri-plugin-log = { path = "../../../plugins/log", version = "2.0.0-alpha.6" }
tauri-plugin-fs = { path = "../../../plugins/fs", version = "2.0.0-alpha.7" }
tauri-plugin-fs = { path = "../../../plugins/fs", version = "2.0.0-alpha.7", features = [ "watch" ] }
tauri-plugin-clipboard-manager = { path = "../../../plugins/clipboard-manager", version = "2.0.0-alpha.6" }
tauri-plugin-dialog = { path = "../../../plugins/dialog", version = "2.0.0-alpha.7" }
tauri-plugin-http = { path = "../../../plugins/http", features = [ "multipart" ], version = "2.0.0-alpha.9" }
Expand Down
67 changes: 67 additions & 0 deletions examples/api/src/views/FileSystem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
let img;
let file;
let renameTo;
let watchPath = "";
let watchDebounceDelay = 0;
let watchRecursive = false;
let unwatchFn;
let unwatchPath = "";

function getDir() {
const dirSelect = document.getElementById("dir");
Expand Down Expand Up @@ -141,6 +146,41 @@
function setSrc() {
img.src = convertFileSrc(path);
}

function watch() {
unwatch();
if (watchPath) {
onMessage(`Watching ${watchPath} for changes`);
let options = {
recursive: watchRecursive,
delayMs: parseInt(watchDebounceDelay),
};
if (options.delayMs === 0) {
fs.watchImmediate(watchPath, onMessage, options)
.then((fn) => {
unwatchFn = fn;
unwatchPath = watchPath;
})
.catch(onMessage);
} else {
fs.watch(watchPath, onMessage, options)
.then((fn) => {
unwatchFn = fn;
unwatchPath = watchPath;
})
.catch(onMessage);
}
}
}

function unwatch() {
if (unwatchFn) {
onMessage(`Stopped watching ${unwatchPath} for changes`);
unwatchFn();
}
unwatchFn = undefined;
unwatchPath = undefined;
}
</script>

<div class="flex flex-col">
Expand Down Expand Up @@ -175,6 +215,33 @@
<button class="btn" on:click={stat}>Stat</button>
</div>
{/if}

<h3>Watch</h3>

<input
class="input grow"
placeholder="Type the path to watch..."
bind:value={watchPath}
/>
<br />
<div>
<label for="watch-debounce-delay">Debounce delay in milliseconds (`0` disables the debouncer)</label>
<input
class="input"
id="watch-debounce-delay"
bind:value={watchDebounceDelay}
/>
</div>
<br />
<div>
<input type="checkbox" id="watch-recursive" bind:checked={watchRecursive} />
<label for="watch-recursive">Recursive</label>
</div>
<br />
<div>
<button class="btn" on:click={watch}>Watch</button>
<button class="btn" on:click={unwatch}>Unwatch</button>
</div>
</div>

<br />
Expand Down
4 changes: 2 additions & 2 deletions plugins/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ anyhow = "1"
uuid = { version = "1", features = [ "v4" ] }
glob = "0.3"
notify = { version = "6", optional = true, features = [ "serde" ] }
notify-debouncer-mini = { version = "0.4", optional = true, features = [ "serde" ] }
notify-debouncer-full = { version = "0.3", optional = true }

[features]
watch = [ "notify", "notify-debouncer-mini" ]
watch = [ "notify", "notify-debouncer-full" ]
84 changes: 60 additions & 24 deletions plugins/fs/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1098,37 +1098,69 @@ interface DebouncedWatchOptions extends WatchOptions {
/**
* @since 2.0.0
*/
type RawEvent = {
type: RawEventKind;
type WatchEvent = {
type: WatchEventKind;
paths: string[];
attrs: unknown;
};

/**
* @since 2.0.0
*/
type RawEventKind =
| "any "
| {
access?: unknown;
}
| {
create?: unknown;
}
| {
modify?: unknown;
}
type WatchEventKind =
| "any"
| { access: WatchEventKindAccess }
| { create: WatchEventKindCreate }
| { modify: WatchEventKindModify }
| { remove: WatchEventKindRemove }
| "other";

/**
* @since 2.0.0
*/
type WatchEventKindAccess =
| { kind: "any" }
| { kind: "close"; mode: "any" | "execute" | "read" | "write" | "other" }
| { kind: "open"; mode: "any" | "execute" | "read" | "write" | "other" }
| { kind: "other" };

/**
* @since 2.0.0
*/
type WatchEventKindCreate =
| { kind: "any" }
| { kind: "file" }
| { kind: "folder" }
| { kind: "other" };

/**
* @since 2.0.0
*/
type WatchEventKindModify =
| { kind: "any" }
| { kind: "data"; mode: "any" | "size" | "content" | "other" }
| {
remove?: unknown;
kind: "metadata";
mode:
| "any"
| "access-time"
| "write-time"
| "permissions"
| "ownership"
| "extended"
| "other";
}
| "other";
| { kind: "name"; mode: "any" | "to" | "from" | "both" | "other" }
| { kind: "other" };

/**
* @since 2.0.0
*/
type DebouncedEvent =
| { kind: "Any"; path: string }[]
| { kind: "AnyContinuous"; path: string }[];
type WatchEventKindRemove =
| { kind: "any" }
| { kind: "file" }
| { kind: "folder" }
| { kind: "other" };

/**
* @since 2.0.0
Expand All @@ -1146,7 +1178,7 @@ async function unwatch(rid: number): Promise<void> {
*/
async function watch(
paths: string | string[] | URL | URL[],
cb: (event: DebouncedEvent) => void,
cb: (event: WatchEvent) => void,
options?: DebouncedWatchOptions,
): Promise<UnwatchFn> {
const opts = {
Expand All @@ -1163,7 +1195,7 @@ async function watch(
}
}

const onEvent = new Channel<DebouncedEvent>();
const onEvent = new Channel<WatchEvent>();
onEvent.onmessage = cb;

const rid: number = await invoke("plugin:fs|watch", {
Expand All @@ -1184,7 +1216,7 @@ async function watch(
*/
async function watchImmediate(
paths: string | string[] | URL | URL[],
cb: (event: RawEvent) => void,
cb: (event: WatchEvent) => void,
options?: WatchOptions,
): Promise<UnwatchFn> {
const opts = {
Expand All @@ -1201,7 +1233,7 @@ async function watchImmediate(
}
}

const onEvent = new Channel<RawEvent>();
const onEvent = new Channel<WatchEvent>();
onEvent.onmessage = cb;

const rid: number = await invoke("plugin:fs|watch", {
Expand Down Expand Up @@ -1232,8 +1264,12 @@ export type {
FileInfo,
WatchOptions,
DebouncedWatchOptions,
DebouncedEvent,
RawEvent,
WatchEvent,
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
WatchEventKind,
WatchEventKindAccess,
WatchEventKindCreate,
WatchEventKindModify,
WatchEventKindRemove,
UnwatchFn,
};

Expand Down
19 changes: 9 additions & 10 deletions plugins/fs/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: MIT

use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use notify_debouncer_mini::{new_debouncer, DebounceEventResult, Debouncer};
use notify_debouncer_full::{new_debouncer, DebounceEventResult, Debouncer, FileIdMap};
use serde::Deserialize;
use tauri::{
ipc::Channel,
Expand Down Expand Up @@ -43,7 +43,7 @@ impl WatcherResource {
impl Resource for WatcherResource {}

enum WatcherKind {
Debouncer(Debouncer<RecommendedWatcher>),
Debouncer(Debouncer<RecommendedWatcher, FileIdMap>),
Watcher(RecommendedWatcher),
}

Expand All @@ -60,10 +60,10 @@ fn watch_raw(on_event: Channel, rx: Receiver<notify::Result<Event>>) {

fn watch_debounced(on_event: Channel, rx: Receiver<DebounceEventResult>) {
spawn(move || {
while let Ok(event) = rx.recv() {
if let Ok(event) = event {
while let Ok(Ok(events)) = rx.recv() {
for event in events {
// TODO: Should errors be emitted too?
let _ = on_event.send(&event);
let _ = on_event.send(&event.event);
}
}
});
Expand Down Expand Up @@ -97,10 +97,9 @@ pub async fn watch<R: Runtime>(

let kind = if let Some(delay) = options.delay_ms {
let (tx, rx) = channel();
let mut debouncer = new_debouncer(Duration::from_millis(delay), tx)?;
let watcher = debouncer.watcher();
let mut debouncer = new_debouncer(Duration::from_millis(delay), None, tx)?;
for path in &resolved_paths {
watcher.watch(path.as_ref(), mode)?;
debouncer.watcher().watch(path.as_ref(), mode)?;
}
watch_debounced(on_event, rx);
WatcherKind::Debouncer(debouncer)
Expand Down Expand Up @@ -130,14 +129,14 @@ pub async fn unwatch<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> CommandR
for path in &watcher.paths {
debouncer.watcher().unwatch(path.as_ref()).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.as_ref()).map_err(|e| {
format!("failed to unwatch path: {} with error: {e}", path.display())
})?
})?;
}
}
}
Expand Down
Loading