parent
66245c943d
commit
a8f198afec
@ -1,71 +0,0 @@
|
||||
import { invoke, transformCallback } from '@tauri-apps/api/tauri'
|
||||
|
||||
export interface MessageKind<T, D> {
|
||||
type: T
|
||||
data: D
|
||||
}
|
||||
|
||||
export interface CloseFrame {
|
||||
code: number
|
||||
reason: string
|
||||
}
|
||||
|
||||
export type Message =
|
||||
MessageKind<'Text', string> |
|
||||
MessageKind<'Binary', number[]> |
|
||||
MessageKind<'Ping', number[]> |
|
||||
MessageKind<'Pong', number[]> |
|
||||
MessageKind<'Close', CloseFrame | null>
|
||||
|
||||
export default class WebSocket {
|
||||
id: number
|
||||
private listeners: Array<(arg: Message) => void>
|
||||
|
||||
constructor(id: number, listeners: Array<(arg: Message) => void>) {
|
||||
this.id = id
|
||||
this.listeners = listeners
|
||||
}
|
||||
|
||||
static async connect(url: string, options?: any): Promise<WebSocket> {
|
||||
const listeners: Array<(arg: Message) => void> = []
|
||||
const handler = (message: Message) => {
|
||||
listeners.forEach(l => l(message))
|
||||
}
|
||||
|
||||
return invoke<number>('plugin:websocket|connect', {
|
||||
url,
|
||||
callbackFunction: transformCallback(handler),
|
||||
options
|
||||
}).then(id => new WebSocket(id, listeners))
|
||||
}
|
||||
|
||||
addListener(cb: (arg: Message) => void) {
|
||||
this.listeners.push(cb)
|
||||
}
|
||||
|
||||
send(message: Message | string | number[]): Promise<void> {
|
||||
let m: Message
|
||||
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 invoke('plugin:websocket|send', {
|
||||
id: this.id,
|
||||
message: m
|
||||
})
|
||||
}
|
||||
|
||||
disconnect(): Promise<void> {
|
||||
return this.send({
|
||||
type: 'Close', data: {
|
||||
code: 1000,
|
||||
reason: 'Disconnected by client'
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
import resolve from "@rollup/plugin-node-resolve";
|
||||
import { terser } from "rollup-plugin-terser";
|
||||
import typescript from "@rollup/plugin-typescript";
|
||||
|
||||
export default [
|
||||
{
|
||||
input: "./webview-src/index.ts",
|
||||
output: {
|
||||
dir: "./webview-dist",
|
||||
entryFileNames: "[name].min.js",
|
||||
format: "esm",
|
||||
},
|
||||
plugins: [
|
||||
resolve(),
|
||||
terser(),
|
||||
typescript({
|
||||
tsconfig: "./webview-src/tsconfig.json",
|
||||
}),
|
||||
],
|
||||
},
|
||||
{
|
||||
input: "./webview-src/index.ts",
|
||||
external: ['@tauri-apps/api/tauri'],
|
||||
output: {
|
||||
dir: "./webview-dist",
|
||||
entryFileNames: "[name].mjs",
|
||||
format: "esm",
|
||||
},
|
||||
plugins: [
|
||||
typescript({
|
||||
tsconfig: "./webview-src/tsconfig.json",
|
||||
}),
|
||||
],
|
||||
},
|
||||
];
|
@ -1,24 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2019",
|
||||
"strict": true,
|
||||
"lib": [
|
||||
"ES2019",
|
||||
"ES2020.Promise",
|
||||
"ES2020.String",
|
||||
"DOM",
|
||||
],
|
||||
"noEmitOnError": true,
|
||||
"isolatedModules": true,
|
||||
// module resolution
|
||||
"esModuleInterop": true,
|
||||
"moduleResolution": "node",
|
||||
// advanced
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"skipLibCheck": true,
|
||||
"declaration": true,
|
||||
"declarationDir": "../webview-dist",
|
||||
"rootDir": "./",
|
||||
},
|
||||
"exclude": ["node_modules"]
|
||||
}
|
Loading…
Reference in new issue