You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tauri-plugins-workspace/plugins/websocket/guest-js/dist/index.min.js

56 lines
2.2 KiB

var d=Object.defineProperty;var e=(c,a)=>{for(var b in a)d(c,b,{get:a[b],enumerable:!0});};
var f={};e(f,{convertFileSrc:()=>w,invoke:()=>c,transformCallback:()=>s});function u(){return window.crypto.getRandomValues(new Uint32Array(1))[0]}function s(e,r=!1){let n=u(),t=`_${n}`;return Object.defineProperty(window,t,{value:o=>(r&&Reflect.deleteProperty(window,t),e==null?void 0:e(o)),writable:!1,configurable:!0}),n}async function c(e,r={}){return new Promise((n,t)=>{let o=s(i=>{n(i),Reflect.deleteProperty(window,`_${a}`);},!0),a=s(i=>{t(i),Reflect.deleteProperty(window,`_${o}`);},!0);window.__TAURI_IPC__({cmd:e,callback:o,error:a,...r});})}function w(e,r="asset"){let n=encodeURIComponent(e);return navigator.userAgent.includes("Windows")?`https://${r}.localhost/${n}`:`${r}://localhost/${n}`}
class WebSocket {
constructor(id, listeners) {
this.id = id;
this.listeners = listeners;
}
static async connect(url, options) {
const listeners = [];
const handler = (message) => {
listeners.forEach((l) => l(message));
};
return await c("plugin:websocket|connect", {
url,
callbackFunction: s(handler),
options,
}).then((id) => new WebSocket(id, listeners));
}
addListener(cb) {
this.listeners.push(cb);
}
async send(message) {
let m;
if (typeof message === "string") {
m = { type: "Text", data: message };
}
else if (typeof message === "object" && "type" in message) {
m = message;
}
else if (Array.isArray(message)) {
m = { type: "Binary", data: message };
}
else {
throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");
}
return await c("plugin:websocket|send", {
id: this.id,
message: m,
});
}
async disconnect() {
return await this.send({
type: "Close",
data: {
code: 1000,
reason: "Disconnected by client",
},
});
}
}
export { WebSocket as default };
//# sourceMappingURL=index.min.js.map