Files
renovate[bot] 16a24211e5 chore(deps): update dependency rollup to v4.34.6 (v1) (#2400)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/13205128094

Co-authored-by: FabianLars <FabianLars@users.noreply.github.com>
2025-02-07 17:43:43 +00:00

57 lines
1.7 KiB
JavaScript

import { invoke, transformCallback } from '@tauri-apps/api/tauri';
class WebSocket {
constructor(id, listeners) {
this.id = id;
this.listeners = listeners;
}
static async connect(url, config) {
const listeners = [];
const handler = (message) => {
listeners.forEach((l) => l(message));
};
if (config === null || config === void 0 ? void 0 : config.headers) {
config.headers = Array.from(new Headers(config.headers).entries());
}
return await invoke("plugin:websocket|connect", {
url,
callbackFunction: transformCallback(handler),
config,
}).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 invoke("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.mjs.map