From 8f0f6b8ac5bccc6f72285803a1a9c742927ec880 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 12 May 2023 11:57:43 -0300 Subject: [PATCH] use channel on fs-watch to remove recursive deps --- Cargo.toml | 8 ++++ README.md | 30 ++++++------- examples/api/src-tauri/Cargo.toml | 8 ---- plugins/fs-watch/guest-js/index.ts | 62 ++++++++++++++++---------- plugins/fs-watch/package.json | 3 +- plugins/fs-watch/src/lib.rs | 19 ++++---- plugins/upload/package.json | 3 +- plugins/window-state/guest-js/index.ts | 18 ++++++-- plugins/window-state/package.json | 3 +- pnpm-lock.yaml | 9 ---- 10 files changed, 89 insertions(+), 74 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3cfe36a1..c70e53f1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/README.md b/README.md index e01bd4f9..edeb0ac5 100644 --- a/README.md +++ b/README.md @@ -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**_ diff --git a/examples/api/src-tauri/Cargo.toml b/examples/api/src-tauri/Cargo.toml index 38e9430e..e64d84b8 100644 --- a/examples/api/src-tauri/Cargo.toml +++ b/examples/api/src-tauri/Cargo.toml @@ -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" diff --git a/plugins/fs-watch/guest-js/index.ts b/plugins/fs-watch/guest-js/index.ts index 9d25096f..2aa58b0c 100644 --- a/plugins/fs-watch/guest-js/index.ts +++ b/plugins/fs-watch/guest-js/index.ts @@ -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 { await invoke("plugin:fs-watch|unwatch", { id }); } +// TODO: use channel from @tauri-apps/api on v2 +class Channel { + 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 { +): 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(); + onEvent.onmessage = cb; + await invoke("plugin:fs-watch|watch", { id, paths: watchPaths, options: opts, + onEvent, }); - const unlisten = await w.listen( - `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 { +): 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(); + onEvent.onmessage = cb; + await invoke("plugin:fs-watch|watch", { id, paths: watchPaths, options: opts, + onEvent, }); - const unlisten = await w.listen( - `watcher://raw-event/${id}`, - (event) => { - cb(event.payload); - } - ); - return () => { void unwatch(id); - unlisten(); }; } diff --git a/plugins/fs-watch/package.json b/plugins/fs-watch/package.json index 61c5cf71..4ac45241 100644 --- a/plugins/fs-watch/package.json +++ b/plugins/fs-watch/package.json @@ -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" } } diff --git a/plugins/fs-watch/src/lib.rs b/plugins/fs-watch/src/lib.rs index 3185d148..d0a9f783 100644 --- a/plugins/fs-watch/src/lib.rs +++ b/plugins/fs-watch/src/lib.rs @@ -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(window: Window, rx: Receiver>, id: Id) { +fn watch_raw(on_event: Channel, rx: Receiver>) { 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(window: Window, rx: Receiver, id: Id) { +fn watch_debounced(on_event: Channel, rx: Receiver) { 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( - window: Window, watchers: State<'_, WatcherCollection>, id: Id, paths: Vec, options: WatchOptions, + on_event: Channel, ) -> Result<()> { let mode = if options.recursive { RecursiveMode::Recursive @@ -96,7 +95,7 @@ async fn watch( 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( for path in &paths { watcher.watch(path, mode)?; } - watch_raw(window, rx, id); + watch_raw(on_event, rx); WatcherKind::Watcher(watcher) }; diff --git a/plugins/upload/package.json b/plugins/upload/package.json index e84f3b62..acd01d42 100644 --- a/plugins/upload/package.json +++ b/plugins/upload/package.json @@ -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" } } diff --git a/plugins/window-state/guest-js/index.ts b/plugins/window-state/guest-js/index.ts index a40d8183..2d303112 100644 --- a/plugins/window-state/guest-js/index.ts +++ b/plugins/window-state/guest-js/index.ts @@ -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 }; diff --git a/plugins/window-state/package.json b/plugins/window-state/package.json index d6937f9f..f7938586 100644 --- a/plugins/window-state/package.json +++ b/plugins/window-state/package.json @@ -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" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c43adf01..2305976a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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