Skip to content

Commit

Permalink
types: optimize webrtc type
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-void0 committed Jan 17, 2024
1 parent 8a894cd commit dc489fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
type KeysStatus,
type ReportArg,
} from './type'
// import { } from 't2t-tools'

const ClassMap = new Map<keyof CONFIG, ClassType>([
['navigator', NavigatorHandle],
Expand Down
30 changes: 19 additions & 11 deletions src/lib/webRTC.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type MutableKeys } from 't2t-tools'
import { type AbstractBaseFunc, Base } from './base'

export interface WebRTCOpts {
Expand Down Expand Up @@ -62,31 +63,38 @@ export class WebRTCHandle extends Base<WebRTCOpts, WebRTCReport> 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<RTCPeerConnection['addEventListener']>
) {
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<typeof RTCPeerConnection>
) {
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<RTCPeerConnection>, value: any) => {
if (!value) return true
if (key === 'onicecandidate') {
target[key] = (event: Event) => {
Expand Down
6 changes: 3 additions & 3 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}))

0 comments on commit dc489fe

Please sign in to comment.