From dc489fe21822a3feb1a0c203a5f3c7d9daba93ad Mon Sep 17 00:00:00 2001 From: yangjingyu Date: Wed, 17 Jan 2024 14:17:54 +0800 Subject: [PATCH] types: optimize webrtc type --- src/index.ts | 1 - src/lib/webRTC.ts | 30 +++++++++++++++++++----------- tsup.config.ts | 6 +++--- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index cb0015c..726df2a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,6 @@ import { type KeysStatus, type ReportArg, } from './type' -// import { } from 't2t-tools' const ClassMap = new Map([ ['navigator', NavigatorHandle], diff --git a/src/lib/webRTC.ts b/src/lib/webRTC.ts index 2c72456..fdca57f 100644 --- a/src/lib/webRTC.ts +++ b/src/lib/webRTC.ts @@ -1,3 +1,4 @@ +import { type MutableKeys } from 't2t-tools' import { type AbstractBaseFunc, Base } from './base' export interface WebRTCOpts { @@ -62,31 +63,38 @@ export class WebRTCHandle extends Base implements Abst proxy(): void { const self = this - RTCPeerConnection.prototype.addEventListener = function () { - if (arguments[0] === 'icecandidate') { - const call = arguments[1] + RTCPeerConnection.prototype.addEventListener = function ( + this: RTCPeerConnection, + ...arg: Parameters + ) { + if (arg[0] === 'icecandidate') { + const call = arg[1] if (call) { - arguments[1] = (event: Event) => { + arg[1] = (event: Event) => { + // @ts-expect-error call(new Proxy(event, { get: self.handler })) } } - return self.oriRTCAddEventListener.apply(this, arguments) + return self.oriRTCAddEventListener.apply(this, arg) } - return self.oriRTCAddEventListener.apply(this, arguments) + return self.oriRTCAddEventListener.apply(this, arg) } - window.RTCPeerConnection = function (this: RTCPeerConnection) { + window.RTCPeerConnection = function ( + this: RTCPeerConnection, + ...arg: ConstructorParameters + ) { const connection = this instanceof self.oriRTCPeerConnection - ? self.oriRTCPeerConnection.apply(this, arguments) - : new self.oriRTCPeerConnection(...arguments) + ? self.oriRTCPeerConnection.apply(this, arg) + : new self.oriRTCPeerConnection(...arg) return new Proxy(connection!, { - get: (target, key) => { + get: (target: RTCPeerConnection, key: keyof RTCPeerConnection) => { const res = target[key] return typeof res === 'function' ? res.bind(target) : res }, - set: (target, key, value) => { + set: (target: RTCPeerConnection, key: keyof MutableKeys, value: any) => { if (!value) return true if (key === 'onicecandidate') { target[key] = (event: Event) => { diff --git a/tsup.config.ts b/tsup.config.ts index 4c3ab02..de533bb 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -2,14 +2,14 @@ import { defineConfig } from 'tsup' export const tsup = defineConfig((option) => ({ entry: ['src/index.ts'], - target: 'node16', + target: 'es2015', dts: true, clean: true, format: ['cjs', 'esm'], - platform: 'node', + platform: 'browser', splitting: false, treeshake: true, - minify: false, + minify: true, sourcemap: !!option.watch, tsconfig: option.watch ? 'tsconfig.dev.json' : 'tsconfig.json', }))