|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) |
| 3 | + |
| 4 | +/// RuntimeBridge — Unified IPC bridge for Lith Studio. |
| 5 | +/// |
| 6 | +/// Detects the available runtime (Gossamer, Tauri, or browser-only) and |
| 7 | +/// dispatches `invoke` calls to the appropriate backend. This allows all |
| 8 | +/// command modules to use a single import instead of binding directly |
| 9 | +/// to `@tauri-apps/api/core`. |
| 10 | +/// |
| 11 | +/// Priority order: |
| 12 | +/// 1. Gossamer (`window.__gossamer_invoke`) — own stack, preferred |
| 13 | +/// 2. Tauri (`window.__TAURI_INTERNALS__`) — legacy, transition |
| 14 | +/// 3. Browser (direct HTTP fetch) — development fallback |
| 15 | +/// |
| 16 | +/// Migration path: replace |
| 17 | +/// `Tauri.invoke("cmd", args)` |
| 18 | +/// with |
| 19 | +/// `RuntimeBridge.invoke("cmd", args)` |
| 20 | + |
| 21 | +// --------------------------------------------------------------------------- |
| 22 | +// Raw external bindings — exactly one of these will be available at runtime |
| 23 | +// --------------------------------------------------------------------------- |
| 24 | + |
| 25 | +/// Gossamer IPC: injected by gossamer_channel_open() into the webview. |
| 26 | +/// Signature: (commandName: string, payload: object) => Promise<any> |
| 27 | +%%raw(` |
| 28 | +function isGossamerRuntime() { |
| 29 | + return typeof window !== 'undefined' |
| 30 | + && typeof window.__gossamer_invoke === 'function'; |
| 31 | +} |
| 32 | +`) |
| 33 | +@val external isGossamerRuntime: unit => bool = "isGossamerRuntime" |
| 34 | + |
| 35 | +%%raw(` |
| 36 | +function gossamerInvoke(cmd, args) { |
| 37 | + return window.__gossamer_invoke(cmd, args); |
| 38 | +} |
| 39 | +`) |
| 40 | +@val external gossamerInvoke: (string, 'a) => promise<'b> = "gossamerInvoke" |
| 41 | + |
| 42 | +/// Tauri IPC: injected by the Tauri runtime into the webview. |
| 43 | +%%raw(` |
| 44 | +function isTauriRuntime() { |
| 45 | + return typeof window !== 'undefined' |
| 46 | + && window.__TAURI_INTERNALS__ != null |
| 47 | + && !window.__TAURI_INTERNALS__.__BROWSER_SHIM__; |
| 48 | +} |
| 49 | +`) |
| 50 | +@val external isTauriRuntime: unit => bool = "isTauriRuntime" |
| 51 | + |
| 52 | +@module("@tauri-apps/api/core") |
| 53 | +external tauriInvoke: (string, 'a) => promise<'b> = "invoke" |
| 54 | + |
| 55 | +// --------------------------------------------------------------------------- |
| 56 | +// Unified invoke — detects runtime and dispatches |
| 57 | +// --------------------------------------------------------------------------- |
| 58 | + |
| 59 | +/// The runtime currently in use. Cached after first detection for performance. |
| 60 | +type runtime = |
| 61 | + | Gossamer |
| 62 | + | Tauri |
| 63 | + | BrowserOnly |
| 64 | + |
| 65 | +%%raw(` |
| 66 | +var _detectedRuntime = null; |
| 67 | +function detectRuntime() { |
| 68 | + if (_detectedRuntime !== null) return _detectedRuntime; |
| 69 | + if (typeof window !== 'undefined' && typeof window.__gossamer_invoke === 'function') { |
| 70 | + _detectedRuntime = 'gossamer'; |
| 71 | + } else if (typeof window !== 'undefined' && window.__TAURI_INTERNALS__ != null && !window.__TAURI_INTERNALS__.__BROWSER_SHIM__) { |
| 72 | + _detectedRuntime = 'tauri'; |
| 73 | + } else { |
| 74 | + _detectedRuntime = 'browser'; |
| 75 | + } |
| 76 | + return _detectedRuntime; |
| 77 | +} |
| 78 | +`) |
| 79 | +@val external detectRuntimeRaw: unit => string = "detectRuntime" |
| 80 | + |
| 81 | +/// Detect and return the current runtime. |
| 82 | +let detectRuntime = (): runtime => { |
| 83 | + switch detectRuntimeRaw() { |
| 84 | + | "gossamer" => Gossamer |
| 85 | + | "tauri" => Tauri |
| 86 | + | _ => BrowserOnly |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Invoke a backend command through whatever runtime is available. |
| 91 | +/// |
| 92 | +/// - On Gossamer: calls `window.__gossamer_invoke(cmd, args)` |
| 93 | +/// - On Tauri: calls `window.__TAURI_INTERNALS__.invoke(cmd, args)` |
| 94 | +/// - On browser: rejects with a descriptive error |
| 95 | +/// |
| 96 | +/// This is the primary function all command modules should use. |
| 97 | +let invoke = (cmd: string, args: 'a): promise<'b> => { |
| 98 | + if isGossamerRuntime() { |
| 99 | + gossamerInvoke(cmd, args) |
| 100 | + } else if isTauriRuntime() { |
| 101 | + tauriInvoke(cmd, args) |
| 102 | + } else { |
| 103 | + Promise.reject( |
| 104 | + JsError.throwWithMessage( |
| 105 | + `No desktop runtime — "${cmd}" requires Gossamer or Tauri`, |
| 106 | + ), |
| 107 | + ) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/// Check whether any desktop runtime is available. |
| 112 | +let hasDesktopRuntime = (): bool => { |
| 113 | + isGossamerRuntime() || isTauriRuntime() |
| 114 | +} |
| 115 | + |
| 116 | +/// Get a human-readable name for the current runtime. |
| 117 | +let runtimeName = (): string => { |
| 118 | + switch detectRuntime() { |
| 119 | + | Gossamer => "Gossamer" |
| 120 | + | Tauri => "Tauri" |
| 121 | + | BrowserOnly => "Browser" |
| 122 | + } |
| 123 | +} |
0 commit comments