From 1c9a430d5ee8dd0bf506941f915b968b399c925c Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 16 Apr 2023 03:41:50 -0400 Subject: [PATCH] * Refactor handlePeer function to use async/await and emit "peer" event, and make Proxy class extend EventEmitter. --- src/proxies/multiSocket.ts | 12 +++++++++--- src/proxy.ts | 4 +++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/proxies/multiSocket.ts b/src/proxies/multiSocket.ts index eb4cfa6..d4665b1 100644 --- a/src/proxies/multiSocket.ts +++ b/src/proxies/multiSocket.ts @@ -70,14 +70,20 @@ const errorSocketEncoding = { const nextSocketId = idFactory(1); export default class MultiSocketProxy extends Proxy { - handlePeer({ peer, muxer, ...options }: DataSocketOptions & PeerOptions) { - new Peer({ + async handlePeer({ + peer, + muxer, + ...options + }: DataSocketOptions & PeerOptions) { + const conn = new Peer({ ...this.socketOptions, proxy: this, peer, muxer, ...options, - }).init(); + }); + await conn.init(); + this.emit("peer", conn); } private socketClass: any; private _peers: Map = new Map(); diff --git a/src/proxy.ts b/src/proxy.ts index 39cfbad..fbbb15a 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -1,5 +1,6 @@ import Protomux from "protomux"; import { DataSocketOptions, PeerOptions } from "./peer.js"; +import EventEmitter from "events"; export interface ProxyOptions extends DataSocketOptions { swarm: any; @@ -8,7 +9,7 @@ export interface ProxyOptions extends DataSocketOptions { autostart?: boolean; } -export default abstract class Proxy { +export default abstract class Proxy extends EventEmitter { protected _listen: any; protected _autostart: boolean; @@ -24,6 +25,7 @@ export default abstract class Proxy { autostart = false, emulateWebsocket = false, }: ProxyOptions) { + super(); this._swarm = swarm; this._protocol = protocol; this._listen = listen;