use channel on fs-watch to remove recursive deps

pull/354/head
Lucas Nogueira 2 years ago
parent 20fa4ad10a
commit 8f0f6b8ac5
No known key found for this signature in database
GPG Key ID: 7C32FCA95C8C95D7

@ -15,3 +15,11 @@ edition = "2021"
authors = [ "Tauri Programme within The Commons Conservancy" ]
license = "Apache-2.0 OR MIT"
rust-version = "1.65"
# default to small, optimized release binaries
[profile.release]
panic = "abort"
codegen-units = 1
lto = true
incremental = false
opt-level = "s"

@ -2,20 +2,20 @@
| | | Win | Mac | Lin | iOS | And |
| ------------------------------------------ | --------------------------------------------------------- | --- | --- | --- | --- | --- |
| [authenticator](plugins/authenticator) | Interface with hardware security keys. | ✅ | ✅ | ✅ | ? | ? |
| [autostart](plugins/autostart) | Automatically launch your app at system startup. | ✅ | ✅ | ✅ | ? | ? |
| [fs-extra](plugins/fs-extra) | File system methods that aren't included in the core API. | ✅ | ✅ | ✅ | ? | ? |
| [fs-watch](plugins/fs-watch) | Watch the filesystem for changes. | ✅ | ✅ | ✅ | ? | ? |
| [localhost](plugins/localhost) | Use a localhost server in production apps. | ✅ | ✅ | ✅ | ? | ? |
| [log](plugins/log) | Configurable logging. | ✅ | ✅ | ✅ | ✅ | ✅ |
| [persisted-scope](plugins/persisted-scope) | Persist runtime scope changes on the filesystem. | ✅ | ✅ | ✅ | ? | ? |
| [positioner](plugins/positioner) | Move windows to common locations. | ✅ | ✅ | ✅ | ? | ? |
| [single-instance](plugins/single-instance) | Ensure a single instance of your tauri app is running. | ✅ | ? | ✅ | ? | ? |
| [sql](plugins/sql) | Interface with SQL databases. | ✅ | ✅ | ✅ | ? | ? |
| [store](plugins/store) | Persistent key value storage. | ✅ | ✅ | ✅ | ? | ? |
| [stronghold](plugins/stronghold) | Encrypted, secure database. | ✅ | ✅ | ✅ | ? | ? |
| [upload](plugins/upload) | Tauri plugin for file uploads through HTTP. | ✅ | ✅ | ✅ | ? | ? |
| [websocket](plugins/websocket) | Open a WebSocket connection using a Rust client in JS. | ✅ | ✅ | ✅ | ? | ? |
| [window-state](plugins/window-state) | Persist window sizes and positions. | ✅ | ✅ | ✅ | ? | ? |
| [authenticator](plugins/authenticator) | Interface with hardware security keys. | ✅ | ✅ | ✅ | ? | ? |
| [autostart](plugins/autostart) | Automatically launch your app at system startup. | ✅ | ✅ | ✅ | ? | ? |
| [fs-extra](plugins/fs-extra) | File system methods that aren't included in the core API. | ✅ | ✅ | ✅ | ? | ? |
| [fs-watch](plugins/fs-watch) | Watch the filesystem for changes. | ✅ | ✅ | ✅ | ? | ? |
| [localhost](plugins/localhost) | Use a localhost server in production apps. | ✅ | ✅ | ✅ | ? | ? |
| [log](plugins/log) | Configurable logging. | ✅ | ✅ | ✅ | ✅ | ✅ |
| [persisted-scope](plugins/persisted-scope) | Persist runtime scope changes on the filesystem. | ✅ | ✅ | ✅ | ? | ? |
| [positioner](plugins/positioner) | Move windows to common locations. | ✅ | ✅ | ✅ | ? | ? |
| [single-instance](plugins/single-instance) | Ensure a single instance of your tauri app is running. | ✅ | ? | ✅ | ? | ? |
| [sql](plugins/sql) | Interface with SQL databases. | ✅ | ✅ | ✅ | ? | ? |
| [store](plugins/store) | Persistent key value storage. | ✅ | ✅ | ✅ | ? | ? |
| [stronghold](plugins/stronghold) | Encrypted, secure database. | ✅ | ✅ | ✅ | ? | ? |
| [upload](plugins/upload) | Tauri plugin for file uploads through HTTP. | ✅ | ✅ | ✅ | ? | ? |
| [websocket](plugins/websocket) | Open a WebSocket connection using a Rust client in JS. | ✅ | ✅ | ✅ | ? | ? |
| [window-state](plugins/window-state) | Persist window sizes and positions. | ✅ | ✅ | ✅ | ? | ? |
_This repo and all plugins require a Rust version of at least **1.65**_

@ -51,11 +51,3 @@ window-shadows = "0.2"
[features]
custom-protocol = [ "tauri/custom-protocol" ]
# default to small, optimized release binaries
[profile.release]
panic = "abort"
codegen-units = 1
lto = true
incremental = false
opt-level = "s"

@ -1,8 +1,4 @@
import { invoke } from "@tauri-apps/api/tauri";
import { UnlistenFn } from "@tauri-apps/api/event";
import { appWindow, WebviewWindow } from "tauri-plugin-window-api";
const w: WebviewWindow = appWindow;
import { invoke, transformCallback } from "@tauri-apps/api/tauri";
export interface WatchOptions {
recursive?: boolean;
@ -42,11 +38,39 @@ async function unwatch(id: number): Promise<void> {
await invoke("plugin:fs-watch|unwatch", { id });
}
// TODO: use channel from @tauri-apps/api on v2
class Channel<T = unknown> {
id: number;
// @ts-expect-error field used by the IPC serializer
private readonly __TAURI_CHANNEL_MARKER__ = true;
#onmessage: (response: T) => void = () => {
// no-op
};
constructor() {
this.id = transformCallback((response: T) => {
this.#onmessage(response);
});
}
set onmessage(handler: (response: T) => void) {
this.#onmessage = handler;
}
get onmessage(): (response: T) => void {
return this.#onmessage;
}
toJSON(): string {
return `__CHANNEL__:${this.id}`;
}
}
export async function watch(
paths: string | string[],
cb: (event: DebouncedEvent) => void,
options: DebouncedWatchOptions = {}
): Promise<UnlistenFn> {
): Promise<() => void> {
const opts = {
recursive: false,
delayMs: 2000,
@ -61,22 +85,18 @@ export async function watch(
const id = window.crypto.getRandomValues(new Uint32Array(1))[0];
const onEvent = new Channel<DebouncedEvent>();
onEvent.onmessage = cb;
await invoke("plugin:fs-watch|watch", {
id,
paths: watchPaths,
options: opts,
onEvent,
});
const unlisten = await w.listen<DebouncedEvent>(
`watcher://debounced-event/${id}`,
(event) => {
cb(event.payload);
}
);
return () => {
void unwatch(id);
unlisten();
};
}
@ -84,7 +104,7 @@ export async function watchImmediate(
paths: string | string[],
cb: (event: RawEvent) => void,
options: WatchOptions = {}
): Promise<UnlistenFn> {
): Promise<() => void> {
const opts = {
recursive: false,
...options,
@ -99,21 +119,17 @@ export async function watchImmediate(
const id = window.crypto.getRandomValues(new Uint32Array(1))[0];
const onEvent = new Channel<RawEvent>();
onEvent.onmessage = cb;
await invoke("plugin:fs-watch|watch", {
id,
paths: watchPaths,
options: opts,
onEvent,
});
const unlisten = await w.listen<RawEvent>(
`watcher://raw-event/${id}`,
(event) => {
cb(event.payload);
}
);
return () => {
void unwatch(id);
unlisten();
};
}

@ -28,7 +28,6 @@
"tslib": "^2.5.0"
},
"dependencies": {
"@tauri-apps/api": "^1.2.0",
"tauri-plugin-window-api": "0.0.0"
"@tauri-apps/api": "^1.2.0"
}
}

@ -2,9 +2,10 @@ use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use notify_debouncer_mini::{new_debouncer, DebounceEventResult, Debouncer};
use serde::{ser::Serializer, Deserialize, Serialize};
use tauri::{
api::ipc::Channel,
command,
plugin::{Builder as PluginBuilder, TauriPlugin},
Manager, Runtime, State, Window,
Manager, Runtime, State,
};
use std::{
@ -44,25 +45,23 @@ enum WatcherKind {
Watcher(RecommendedWatcher),
}
fn watch_raw<R: Runtime>(window: Window<R>, rx: Receiver<notify::Result<Event>>, id: Id) {
fn watch_raw<R: Runtime>(on_event: Channel<R>, rx: Receiver<notify::Result<Event>>) {
spawn(move || {
let event_name = format!("watcher://raw-event/{id}");
while let Ok(event) = rx.recv() {
if let Ok(event) = event {
// TODO: Should errors be emitted too?
let _ = window.emit(&event_name, event);
let _ = on_event.send(&event);
}
}
});
}
fn watch_debounced<R: Runtime>(window: Window<R>, rx: Receiver<DebounceEventResult>, id: Id) {
fn watch_debounced<R: Runtime>(on_event: Channel<R>, rx: Receiver<DebounceEventResult>) {
spawn(move || {
let event_name = format!("watcher://debounced-event/{id}");
while let Ok(event) = rx.recv() {
if let Ok(event) = event {
// TODO: Should errors be emitted too?
let _ = window.emit(&event_name, event);
let _ = on_event.send(&event);
}
}
});
@ -77,11 +76,11 @@ struct WatchOptions {
#[command]
async fn watch<R: Runtime>(
window: Window<R>,
watchers: State<'_, WatcherCollection>,
id: Id,
paths: Vec<PathBuf>,
options: WatchOptions,
on_event: Channel<R>,
) -> Result<()> {
let mode = if options.recursive {
RecursiveMode::Recursive
@ -96,7 +95,7 @@ async fn watch<R: Runtime>(
for path in &paths {
watcher.watch(path, mode)?;
}
watch_debounced(window, rx, id);
watch_debounced(on_event, rx);
WatcherKind::Debouncer(debouncer)
} else {
let (tx, rx) = channel();
@ -104,7 +103,7 @@ async fn watch<R: Runtime>(
for path in &paths {
watcher.watch(path, mode)?;
}
watch_raw(window, rx, id);
watch_raw(on_event, rx);
WatcherKind::Watcher(watcher)
};

@ -28,7 +28,6 @@
"tslib": "^2.5.0"
},
"dependencies": {
"@tauri-apps/api": "^1.2.0",
"tauri-plugin-window-api": "0.0.0"
"@tauri-apps/api": "^1.2.0"
}
}

@ -1,5 +1,17 @@
import { invoke } from "@tauri-apps/api/tauri";
import { WindowLabel, getCurrent } from "tauri-plugin-window-api";
interface WindowDef {
label: string;
}
declare global {
interface Window {
__TAURI_METADATA__: {
__windows: WindowDef[];
__currentWindow: WindowDef;
};
}
}
export enum StateFlags {
SIZE = 1 << 0,
@ -21,7 +33,7 @@ async function saveWindowState(flags: StateFlags) {
/**
* Restore the state for the specified window from disk.
*/
async function restoreState(label: WindowLabel, flags: StateFlags) {
async function restoreState(label: string, flags: StateFlags) {
invoke("plugin:window-state|restore_state", { label, flags });
}
@ -29,7 +41,7 @@ async function restoreState(label: WindowLabel, flags: StateFlags) {
* Restore the state for the current window from disk.
*/
async function restoreStateCurrent(flags: StateFlags) {
restoreState(getCurrent().label, flags);
restoreState(window.__TAURI_METADATA__.__currentWindow.label, flags);
}
export { restoreState, restoreStateCurrent, saveWindowState };

@ -28,7 +28,6 @@
"tslib": "^2.5.0"
},
"dependencies": {
"@tauri-apps/api": "^1.2.0",
"tauri-plugin-window-api": "0.0.0"
"@tauri-apps/api": "^1.2.0"
}
}

@ -198,9 +198,6 @@ importers:
'@tauri-apps/api':
specifier: ^1.2.0
version: 1.2.0
tauri-plugin-window-api:
specifier: 0.0.0
version: link:../window
devDependencies:
tslib:
specifier: ^2.5.0
@ -337,9 +334,6 @@ importers:
'@tauri-apps/api':
specifier: ^1.2.0
version: 1.2.0
tauri-plugin-window-api:
specifier: 0.0.0
version: link:../window
devDependencies:
tslib:
specifier: ^2.5.0
@ -401,9 +395,6 @@ importers:
'@tauri-apps/api':
specifier: ^1.2.0
version: 1.2.0
tauri-plugin-window-api:
specifier: 0.0.0
version: link:../window
devDependencies:
tslib:
specifier: ^2.5.0

Loading…
Cancel
Save