feat: update to alpha.11 (#555)

This commit is contained in:
Lucas Fernandes Nogueira
2023-08-14 10:51:20 -07:00
committed by GitHub
parent d5a7c77a8d
commit d74fc0a097
67 changed files with 629 additions and 539 deletions
@@ -13,7 +13,7 @@
"devDependencies": {
"@sveltejs/adapter-auto": "2.1.0",
"@sveltejs/kit": "1.22.3",
"@tauri-apps/cli": "2.0.0-alpha.10",
"@tauri-apps/cli": "2.0.0-alpha.11",
"svelte": "4.0.5",
"svelte-check": "3.4.6",
"tslib": "2.6.0",
+10 -17
View File
@@ -2,14 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
declare global {
interface Window {
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
__TAURI__: {
transformCallback: <T>(cb: (payload: T) => void) => number;
};
}
}
import { invoke, Channel } from "@tauri-apps/api/tauri";
export interface MessageKind<T, D> {
type: T;
@@ -39,17 +32,17 @@ export default class WebSocket {
static async connect(url: string, options?: unknown): Promise<WebSocket> {
const listeners: Array<(arg: Message) => void> = [];
const handler = (message: Message): void => {
const onMessage = new Channel<Message>();
onMessage.onmessage = (message: Message): void => {
listeners.forEach((l) => l(message));
};
return await window
.__TAURI_INVOKE__<number>("plugin:websocket|connect", {
url,
callbackFunction: window.__TAURI__.transformCallback(handler),
options,
})
.then((id) => new WebSocket(id, listeners));
return await invoke<number>("plugin:websocket|connect", {
url,
onMessage,
options,
}).then((id) => new WebSocket(id, listeners));
}
addListener(cb: (arg: Message) => void): void {
@@ -69,7 +62,7 @@ export default class WebSocket {
"invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array",
);
}
return await window.__TAURI_INVOKE__("plugin:websocket|send", {
return await invoke("plugin:websocket|send", {
id: this.id,
message: m,
});
+1 -1
View File
@@ -27,6 +27,6 @@
"tslib": "2.6.0"
},
"dependencies": {
"@tauri-apps/api": "2.0.0-alpha.5"
"@tauri-apps/api": "2.0.0-alpha.6"
}
}
+1 -1
View File
@@ -1 +1 @@
if("__TAURI__"in window){var __TAURI_WEBSOCKET__=function(){"use strict";class e{constructor(e,t){this.id=e,this.listeners=t}static async connect(t,n){const i=[];return await window.__TAURI_INVOKE__("plugin:websocket|connect",{url:t,callbackFunction:window.__TAURI__.transformCallback((e=>{i.forEach((t=>t(e)))})),options:n}).then((t=>new e(t,i)))}addListener(e){this.listeners.push(e)}async send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}return await window.__TAURI_INVOKE__("plugin:websocket|send",{id:this.id,message:t})}async disconnect(){return await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return e}();Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_WEBSOCKET__})}
if("__TAURI__"in window){var __TAURI_WEBSOCKET__=function(){"use strict";var e=Object.defineProperty,t=(e,t,n)=>{if(!t.has(e))throw TypeError("Cannot "+n)},n=(e,n,r)=>(t(e,n,"read from private field"),r?r.call(e):n.get(e)),r=(e,n,r,i)=>(t(e,n,"write to private field"),i?i.call(e,r):n.set(e,r),r);function i(e,t=!1){let n=window.crypto.getRandomValues(new Uint32Array(1))[0],r=`_${n}`;return Object.defineProperty(window,r,{value:n=>(t&&Reflect.deleteProperty(window,r),e?.(n)),writable:!1,configurable:!0}),n}((t,n)=>{for(var r in n)e(t,r,{get:n[r],enumerable:!0})})({},{Channel:()=>a,PluginListener:()=>o,addPluginListener:()=>c,convertFileSrc:()=>d,invoke:()=>l,transformCallback:()=>i});var s,a=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,((e,t,n)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,n)})(this,s,(()=>{})),this.id=i((e=>{n(this,s).call(this,e)}))}set onmessage(e){r(this,s,e)}get onmessage(){return n(this,s)}toJSON(){return`__CHANNEL__:${this.id}`}};s=new WeakMap;var o=class{constructor(e,t,n){this.plugin=e,this.event=t,this.channelId=n}async unregister(){return l(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function c(e,t,n){let r=new a;return r.onmessage=n,l(`plugin:${e}|register_listener`,{event:t,handler:r}).then((()=>new o(e,t,r.id)))}async function l(e,t={},n){return new Promise(((r,s)=>{let a=i((e=>{r(e),Reflect.deleteProperty(window,`_${o}`)}),!0),o=i((e=>{s(e),Reflect.deleteProperty(window,`_${a}`)}),!0);window.__TAURI_IPC__({cmd:e,callback:a,error:o,payload:t,options:n})}))}function d(e,t="asset"){return window.__TAURI__.convertFileSrc(e,t)}class _{constructor(e,t){this.id=e,this.listeners=t}static async connect(e,t){const n=[],r=new a;return r.onmessage=e=>{n.forEach((t=>t(e)))},await l("plugin:websocket|connect",{url:e,onMessage:r,options:t}).then((e=>new _(e,n)))}addListener(e){this.listeners.push(e)}async send(e){let t;if("string"==typeof e)t={type:"Text",data:e};else if("object"==typeof e&&"type"in e)t=e;else{if(!Array.isArray(e))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");t={type:"Binary",data:e}}return await l("plugin:websocket|send",{id:this.id,message:t})}async disconnect(){return await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return _}();Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_WEBSOCKET__})}
+5 -5
View File
@@ -14,7 +14,7 @@
use futures_util::{stream::SplitSink, SinkExt, StreamExt};
use serde::{ser::Serializer, Deserialize, Serialize};
use tauri::{
api::ipc::{format_callback, CallbackFn},
ipc::Channel,
plugin::{Builder as PluginBuilder, TauriPlugin},
Manager, Runtime, State, Window,
};
@@ -95,7 +95,7 @@ enum WebSocketMessage {
async fn connect<R: Runtime>(
window: Window<R>,
url: String,
callback_function: CallbackFn,
on_message: Channel,
config: Option<ConnectionConfig>,
) -> Result<Id> {
let id = rand::random();
@@ -107,6 +107,7 @@ async fn connect<R: Runtime>(
manager.0.lock().await.insert(id, write);
read.for_each(move |message| {
let window_ = window.clone();
let on_message_ = on_message.clone();
async move {
if let Ok(Message::Close(_)) = message {
let manager = window_.state::<ConnectionManager>();
@@ -136,9 +137,8 @@ async fn connect<R: Runtime>(
Ok(Message::Frame(_)) => serde_json::Value::Null, // This value can't be recieved.
Err(e) => serde_json::to_value(Error::from(e)).unwrap(),
};
let js = format_callback(callback_function, &response)
.expect("unable to serialize websocket message");
let _ = window_.eval(js.as_str());
let _ = on_message_.send(response);
}
})
.await;