Files
tauri-plugins-workspace/plugins/websocket/api-iife.js
T
Lucas Fernandes Nogueira ae3c808eb6 fix: global API script should reuse window.__TAURI__ types (#3602)
currently each script inlines what it needs from @tauri-apps/api, so type checks like `image instanceof Image` all fail
2026-09-22 06:04:28 -03:00

2 lines
1.0 KiB
JavaScript

if("__TAURI__"in window){var __TAURI_PLUGIN_WEBSOCKET__=function(e){"use strict";class t{constructor(e,t){this.id=e,this.listeners=t}static async connect(n,s){const i=new Set,a=new e.Channel;return a.onmessage=e=>{i.forEach(t=>{t(e)})},s?.headers&&(s.headers=Array.from(new Headers(s.headers).entries())),await e.invoke("plugin:websocket|connect",{url:n,onMessage:a,config:s}).then(e=>new t(e,i))}addListener(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}async send(t){let n;if("string"==typeof t)n={type:"Text",data:t};else if("object"==typeof t&&"type"in t)n=t;else{if(!Array.isArray(t))throw new Error("invalid `message` type, expected a `{ type: string, data: any }` object, a string or a numeric array");n={type:"Binary",data:t}}await e.invoke("plugin:websocket|send",{id:this.id,message:n})}async disconnect(){await this.send({type:"Close",data:{code:1e3,reason:"Disconnected by client"}})}}return t}(window.__TAURI__.core);Object.defineProperty(window.__TAURI__,"websocket",{value:__TAURI_PLUGIN_WEBSOCKET__})}