Struct tauri::InvokeResolver
source · Expand description
Resolver of a invoke message.
Implementations§
source§impl<R: Runtime> InvokeResolver<R>
impl<R: Runtime> InvokeResolver<R>
sourcepub fn respond_async<T, F>(self, task: F)where
T: Serialize,
F: Future<Output = Result<T, InvokeError>> + Send + 'static,
pub fn respond_async<T, F>(self, task: F)where
T: Serialize,
F: Future<Output = Result<T, InvokeError>> + Send + 'static,
Reply to the invoke promise with an async task.
Examples found in repository?
src/endpoints.rs (lines 114-119)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
fn run<R: Runtime>(
self,
window: Window<R>,
resolver: InvokeResolver<R>,
config: Arc<Config>,
package_info: PackageInfo,
) {
let context = InvokeContext {
window,
config,
package_info,
};
match self {
Self::App(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(process_any)]
Self::Process(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(fs_any)]
Self::Fs(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(path_any)]
Self::Path(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(os_any)]
Self::Os(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
Self::Window(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.await
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(shell_any)]
Self::Shell(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
Self::Event(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(dialog_any)]
Self::Dialog(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(cli)]
Self::Cli(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
Self::Notification(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(http_any)]
Self::Http(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.await
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(global_shortcut_any)]
Self::GlobalShortcut(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
#[cfg(clipboard_any)]
Self::Clipboard(cmd) => resolver.respond_async(async move {
cmd
.run(context)
.and_then(|r| r.json)
.map_err(InvokeError::from_anyhow)
}),
}
}
sourcepub fn respond_async_serialized<F>(self, task: F)where
F: Future<Output = Result<JsonValue, InvokeError>> + Send + 'static,
pub fn respond_async_serialized<F>(self, task: F)where
F: Future<Output = Result<JsonValue, InvokeError>> + Send + 'static,
Reply to the invoke promise with an async task which is already serialized.
sourcepub fn respond<T: Serialize>(self, value: Result<T, InvokeError>)
pub fn respond<T: Serialize>(self, value: Result<T, InvokeError>)
Reply to the invoke promise with a serializable value.
Examples found in repository?
src/command.rs (line 189)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
pub fn block<R, T>(self, value: T, resolver: InvokeResolver<R>)
where
R: Runtime,
T: Serialize,
{
resolver.respond(Ok(value))
}
#[inline(always)]
pub fn future<T>(self, value: T) -> impl Future<Output = Result<Value, InvokeError>>
where
T: Serialize,
{
std::future::ready(serde_json::to_value(value).map_err(InvokeError::from_serde_json))
}
}
// ===== Result<impl Serialize, impl Into<InvokeError>> =====
pub struct ResultTag;
pub trait ResultKind {
#[inline(always)]
fn blocking_kind(&self) -> ResultTag {
ResultTag
}
#[inline(always)]
fn async_kind(&self) -> ResultTag {
ResultTag
}
}
impl<T: Serialize, E: Into<InvokeError>> ResultKind for Result<T, E> {}
impl ResultTag {
#[inline(always)]
pub fn block<R, T, E>(self, value: Result<T, E>, resolver: InvokeResolver<R>)
where
R: Runtime,
T: Serialize,
E: Into<InvokeError>,
{
resolver.respond(value.map_err(Into::into))
}
sourcepub fn reject<T: Serialize>(self, value: T)
pub fn reject<T: Serialize>(self, value: T)
Reject the invoke promise with a value.
Examples found in repository?
src/plugin.rs (line 591)
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
pub(crate) fn extend_api(&mut self, mut invoke: Invoke<R>) {
let command = invoke.message.command.replace("plugin:", "");
let mut tokens = command.split('|');
// safe to unwrap: split always has a least one item
let target = tokens.next().unwrap();
if let Some(plugin) = self.store.get_mut(target) {
invoke.message.command = tokens
.next()
.map(|c| c.to_string())
.unwrap_or_else(String::new);
plugin.extend_api(invoke);
} else {
invoke
.resolver
.reject(format!("plugin {} not found", target));
}
}
More examples
src/endpoints.rs (lines 240-243)
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
pub(crate) fn handle<R: Runtime>(
module: String,
invoke: Invoke<R>,
config: Arc<Config>,
package_info: &PackageInfo,
) {
let Invoke { message, resolver } = invoke;
let InvokeMessage {
mut payload,
window,
..
} = message;
if let JsonValue::Object(ref mut obj) = payload {
obj.insert("module".to_string(), JsonValue::String(module.clone()));
}
match serde_json::from_value::<Module>(payload) {
Ok(module) => module.run(window, resolver, config, package_info.clone()),
Err(e) => {
let message = e.to_string();
if message.starts_with("unknown variant") {
let mut s = message.split('`');
s.next();
if let Some(unknown_variant_name) = s.next() {
if unknown_variant_name == module {
return resolver.reject(format!(
"The `{}` module is not enabled. You must enable one of its APIs in the allowlist.",
module
));
} else if module == "Window" {
return resolver.reject(window::into_allowlist_error(unknown_variant_name).to_string());
}
}
}
resolver.reject(message);
}
}
}
sourcepub fn invoke_error(self, error: InvokeError)
pub fn invoke_error(self, error: InvokeError)
Reject the invoke promise with an InvokeError
.
sourcepub async fn return_task<T, F>(
window: Window<R>,
task: F,
success_callback: CallbackFn,
error_callback: CallbackFn
)where
T: Serialize,
F: Future<Output = Result<T, InvokeError>> + Send + 'static,
pub async fn return_task<T, F>(
window: Window<R>,
task: F,
success_callback: CallbackFn,
error_callback: CallbackFn
)where
T: Serialize,
F: Future<Output = Result<T, InvokeError>> + Send + 'static,
Asynchronously executes the given task
and evaluates its Result to the JS promise described by the success_callback
and error_callback
function names.
If the Result is_ok()
, the callback will be the success_callback
function name and the argument will be the Ok value.
If the Result is_err()
, the callback will be the error_callback
function name and the argument will be the Err value.