add process module

This commit is contained in:
Jonas Kruckenberg 2022-11-03 18:52:40 +01:00
parent ebf3c07e79
commit bc8818e695
6 changed files with 3664 additions and 5 deletions

3572
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -18,15 +18,20 @@ semver = { version = "1.0.14", optional = true }
[dev-dependencies] [dev-dependencies]
wasm-bindgen-test = "0.3.33" wasm-bindgen-test = "0.3.33"
tauri-sys = { path = ".", features = ["all"] } tauri-sys = { path = ".", features = ["all"] }
tauri = "1.1.1"
[package.metadata.docs.rs] [package.metadata.docs.rs]
all-features = true all-features = true
[features] [features]
all = ["app", "clipboard", "event", "mocks", "tauri", "window"] all = ["app", "clipboard", "event", "mocks", "tauri", "window", "process"]
app = ["dep:semver"] app = ["dep:semver"]
clipboard = [] clipboard = []
event = [] event = []
mocks = [] mocks = []
tauri = ["dep:url"] tauri = ["dep:url"]
window = [] window = []
process = []
[workspace]
members = ["examples/test", "examples/test/src-tauri"]

View file

@ -11,7 +11,8 @@ fn main() {
"tauri/tooling/api/src/tauri.ts", "tauri/tooling/api/src/tauri.ts",
"tauri/tooling/api/src/event.ts", "tauri/tooling/api/src/event.ts",
"tauri/tooling/api/src/mocks.ts", "tauri/tooling/api/src/mocks.ts",
"tauri/tooling/api/src/window.ts" "tauri/tooling/api/src/window.ts",
"tauri/tooling/api/src/process.ts"
]; ];
if cfg!(windows) { if cfg!(windows) {

65
dist/process.js vendored Normal file
View file

@ -0,0 +1,65 @@
// tauri/tooling/api/src/tauri.ts
function uid() {
return window.crypto.getRandomValues(new Uint32Array(1))[0];
}
function transformCallback(callback, once = false) {
const identifier = uid();
const prop = `_${identifier}`;
Object.defineProperty(window, prop, {
value: (result) => {
if (once) {
Reflect.deleteProperty(window, prop);
}
return callback?.(result);
},
writable: false,
configurable: true
});
return identifier;
}
async function invoke(cmd, args = {}) {
return new Promise((resolve, reject) => {
const callback = transformCallback((e) => {
resolve(e);
Reflect.deleteProperty(window, `_${error}`);
}, true);
const error = transformCallback((e) => {
reject(e);
Reflect.deleteProperty(window, `_${callback}`);
}, true);
window.__TAURI_IPC__({
cmd,
callback,
error,
...args
});
});
}
// tauri/tooling/api/src/helpers/tauri.ts
async function invokeTauriCommand(command) {
return invoke("tauri", command);
}
// tauri/tooling/api/src/process.ts
async function exit(exitCode = 0) {
return invokeTauriCommand({
__tauriModule: "Process",
message: {
cmd: "exit",
exitCode
}
});
}
async function relaunch() {
return invokeTauriCommand({
__tauriModule: "Process",
message: {
cmd: "relaunch"
}
});
}
export {
exit,
relaunch
};

View file

@ -12,6 +12,8 @@ pub mod mocks;
pub mod tauri; pub mod tauri;
#[cfg(feature = "window")] #[cfg(feature = "window")]
pub mod window; pub mod window;
#[cfg(feature = "process")]
pub mod process;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {

18
src/process.rs Normal file
View file

@ -0,0 +1,18 @@
pub fn exit(exit_code: u32) -> ! {
inner::exit(exit_code);
unreachable!()
}
pub fn relaunch() {
inner::relaunch();
}
mod inner {
use wasm_bindgen::prelude::wasm_bindgen;
#[wasm_bindgen(module = "/dist/process.js")]
extern "C" {
pub fn exit(exitCode: u32);
pub fn relaunch();
}
}