code refactoring, switch to rxjs websocket wrapper

This commit is contained in:
2023-04-19 14:14:15 +02:00
parent 621164589f
commit fa7cd1a691
12 changed files with 82 additions and 66 deletions

View File

@@ -1,13 +1,14 @@
import type { RPCRequest, RPCResponse, DLMetadata } from "../../types"
import type { DLMetadata, RPCRequest, RPCResponse } from '../../types'
import { getHttpRPCEndpoint } from '../../utils'
import { webSocket } from 'rxjs/webSocket'
import { getHttpRPCEndpoint, getWebSocketEndpoint } from '../../utils'
export const socket$ = webSocket<any>(getWebSocketEndpoint())
export class RPCClient {
private socket: WebSocket
private seq: number
constructor(socket: WebSocket) {
this.socket = socket
constructor() {
this.seq = 0
}
@@ -16,7 +17,10 @@ export class RPCClient {
}
private send(req: RPCRequest) {
this.socket.send(JSON.stringify(req))
socket$.next({
...req,
id: this.incrementSeq(),
})
}
private async sendHTTP<T>(req: RPCRequest) {
@@ -35,7 +39,6 @@ export class RPCClient {
public download(url: string, args: string, pathOverride = '', renameTo = '') {
if (url) {
this.send({
id: this.incrementSeq(),
method: 'Service.Exec',
params: [{
URL: url.split("?list").at(0)!,
@@ -50,7 +53,6 @@ export class RPCClient {
public formats(url: string) {
if (url) {
return this.sendHTTP<DLMetadata>({
id: this.incrementSeq(),
method: 'Service.Formats',
params: [{
URL: url.split("?list").at(0)!,
@@ -61,7 +63,6 @@ export class RPCClient {
public running() {
this.send({
id: this.incrementSeq(),
method: 'Service.Running',
params: [],
})
@@ -101,8 +102,4 @@ export class RPCClient {
params: []
})
}
public decode(data: any): RPCResponse<any> {
return JSON.parse(data)
}
}