@@ -120,10 +120,76 @@ export type PeerEvents = {
120
120
*/
121
121
error : ( error : PeerError ) => void ;
122
122
} ;
123
+
124
+ export interface IPeer {
125
+ /**
126
+ * The brokering ID of this peer
127
+ *
128
+ * If no ID was specified in {@apilink Peer | the constructor},
129
+ * this will be `undefined` until the {@apilink PeerEvents | `open`} event is emitted.
130
+ */
131
+ get id ( ) : string ;
132
+ get open ( ) : boolean ;
133
+ /**
134
+ * A hash of all connections associated with this peer, keyed by the remote peer's ID.
135
+ * @deprecated
136
+ * Return type will change from Object to Map<string,[]>
137
+ */
138
+ get connections ( ) : Object ;
139
+ /**
140
+ * true if this peer and all of its connections can no longer be used.
141
+ */
142
+ get destroyed ( ) : boolean ;
143
+ /**
144
+ * Connects to the remote peer specified by id and returns a data connection.
145
+ * @param peer The brokering ID of the remote peer (their {@apilink Peer.id}).
146
+ * @param options for specifying details about Peer Connection
147
+ */
148
+ connect ( peer : string , options : PeerConnectOption ) : DataConnection ;
149
+ /**
150
+ * Calls the remote peer specified by id and returns a media connection.
151
+ * @param peer The brokering ID of the remote peer (their peer.id).
152
+ * @param stream The caller's media stream
153
+ * @param options Metadata associated with the connection, passed in by whoever initiated the connection.
154
+ */
155
+ call ( peer : string , stream : MediaStream , options : CallOption ) : MediaConnection ;
156
+ /** Retrieve a data/media connection for this peer. */
157
+ getConnection (
158
+ peerId : string ,
159
+ connectionId : string ,
160
+ ) : null | DataConnection | MediaConnection ;
161
+ /**
162
+ * Destroys the Peer: closes all active connections as well as the connection
163
+ * to the server.
164
+ *
165
+ * :::caution
166
+ * This cannot be undone; the respective peer object will no longer be able
167
+ * to create or receive any connections, its ID will be forfeited on the server,
168
+ * and all of its data and media connections will be closed.
169
+ * :::
170
+ */
171
+ destroy ( ) : void ;
172
+ /**
173
+ * Disconnects the Peer's connection to the PeerServer. Does not close any
174
+ * active connections.
175
+ * Warning: The peer can no longer create or accept connections after being
176
+ * disconnected. It also cannot reconnect to the server.
177
+ */
178
+ disconnect ( ) : void ;
179
+ /** Attempts to reconnect with the same ID.
180
+ *
181
+ * Only {@apilink Peer.disconnect | disconnected peers} can be reconnected.
182
+ * Destroyed peers cannot be reconnected.
183
+ * If the connection fails (as an example, if the peer's old ID is now taken),
184
+ * the peer's existing connections will not close, but any associated errors events will fire.
185
+ */
186
+ reconnect ( ) : void ;
187
+ }
188
+
123
189
/**
124
190
* A peer who can initiate connections with other peers.
125
191
*/
126
- export class Peer extends EventEmitter < PeerEvents > {
192
+ export class Peer extends EventEmitter < PeerEvents > implements IPeer {
127
193
private static readonly DEFAULT_KEY = "peerjs" ;
128
194
129
195
private readonly _serializers : SerializerMapping = {
@@ -150,12 +216,11 @@ export class Peer extends EventEmitter<PeerEvents> {
150
216
( DataConnection | MediaConnection ) [ ]
151
217
> = new Map ( ) ; // All connections for this peer.
152
218
private readonly _lostMessages : Map < string , ServerMessage [ ] > = new Map ( ) ; // src => [list of messages]
153
- /**
154
- * The brokering ID of this peer
155
- *
156
- * If no ID was specified in {@apilink Peer | the constructor},
157
- * this will be `undefined` until the {@apilink PeerEvents | `open`} event is emitted.
158
- */
219
+ private then : (
220
+ onfulfilled ?: ( value : IPeer ) => any ,
221
+ onrejected ?: ( reason : PeerError ) => any ,
222
+ ) => void ;
223
+
159
224
get id ( ) {
160
225
return this . _id ;
161
226
}
@@ -175,11 +240,6 @@ export class Peer extends EventEmitter<PeerEvents> {
175
240
return this . _socket ;
176
241
}
177
242
178
- /**
179
- * A hash of all connections associated with this peer, keyed by the remote peer's ID.
180
- * @deprecated
181
- * Return type will change from Object to Map<string,[]>
182
- */
183
243
get connections ( ) : Object {
184
244
const plainConnections = Object . create ( null ) ;
185
245
@@ -190,15 +250,9 @@ export class Peer extends EventEmitter<PeerEvents> {
190
250
return plainConnections ;
191
251
}
192
252
193
- /**
194
- * true if this peer and all of its connections can no longer be used.
195
- */
196
253
get destroyed ( ) {
197
254
return this . _destroyed ;
198
255
}
199
- /**
200
- * false if there is an active connection to the PeerServer.
201
- */
202
256
get disconnected ( ) {
203
257
return this . _disconnected ;
204
258
}
@@ -226,6 +280,15 @@ export class Peer extends EventEmitter<PeerEvents> {
226
280
constructor ( id ?: string | PeerOptions , options ?: PeerOptions ) {
227
281
super ( ) ;
228
282
283
+ this . then = (
284
+ onfulfilled ?: ( value : IPeer ) => any ,
285
+ onrejected ?: ( reason : PeerError ) => any ,
286
+ ) => {
287
+ delete this . then ;
288
+ this . once ( "open" , ( ) => onfulfilled ?.( this ) ) ;
289
+ this . once ( "error" , onrejected ) ;
290
+ } ;
291
+
229
292
let userId : string | undefined ;
230
293
231
294
// Deal with overloading
@@ -495,11 +558,6 @@ export class Peer extends EventEmitter<PeerEvents> {
495
558
return [ ] ;
496
559
}
497
560
498
- /**
499
- * Connects to the remote peer specified by id and returns a data connection.
500
- * @param peer The brokering ID of the remote peer (their {@apilink Peer.id}).
501
- * @param options for specifying details about Peer Connection
502
- */
503
561
connect ( peer : string , options : PeerConnectOption ) : DataConnection {
504
562
options = {
505
563
serialization : "default" ,
@@ -528,12 +586,6 @@ export class Peer extends EventEmitter<PeerEvents> {
528
586
return dataConnection ;
529
587
}
530
588
531
- /**
532
- * Calls the remote peer specified by id and returns a media connection.
533
- * @param peer The brokering ID of the remote peer (their peer.id).
534
- * @param stream The caller's media stream
535
- * @param options Metadata associated with the connection, passed in by whoever initiated the connection.
536
- */
537
589
call (
538
590
peer : string ,
539
591
stream : MediaStream ,
@@ -598,7 +650,6 @@ export class Peer extends EventEmitter<PeerEvents> {
598
650
this . _lostMessages . delete ( connection . connectionId ) ;
599
651
}
600
652
601
- /** Retrieve a data/media connection for this peer. */
602
653
getConnection (
603
654
peerId : string ,
604
655
connectionId : string ,
@@ -647,16 +698,6 @@ export class Peer extends EventEmitter<PeerEvents> {
647
698
this . emit ( "error" , new PeerError ( type , err ) ) ;
648
699
}
649
700
650
- /**
651
- * Destroys the Peer: closes all active connections as well as the connection
652
- * to the server.
653
- *
654
- * :::caution
655
- * This cannot be undone; the respective peer object will no longer be able
656
- * to create or receive any connections, its ID will be forfeited on the server,
657
- * and all of its data and media connections will be closed.
658
- * :::
659
- */
660
701
destroy ( ) : void {
661
702
if ( this . destroyed ) {
662
703
return ;
@@ -693,12 +734,6 @@ export class Peer extends EventEmitter<PeerEvents> {
693
734
}
694
735
}
695
736
696
- /**
697
- * Disconnects the Peer's connection to the PeerServer. Does not close any
698
- * active connections.
699
- * Warning: The peer can no longer create or accept connections after being
700
- * disconnected. It also cannot reconnect to the server.
701
- */
702
737
disconnect ( ) : void {
703
738
if ( this . disconnected ) {
704
739
return ;
@@ -719,13 +754,6 @@ export class Peer extends EventEmitter<PeerEvents> {
719
754
this . emit ( "disconnected" , currentId ) ;
720
755
}
721
756
722
- /** Attempts to reconnect with the same ID.
723
- *
724
- * Only {@apilink Peer.disconnect | disconnected peers} can be reconnected.
725
- * Destroyed peers cannot be reconnected.
726
- * If the connection fails (as an example, if the peer's old ID is now taken),
727
- * the peer's existing connections will not close, but any associated errors events will fire.
728
- */
729
757
reconnect ( ) : void {
730
758
if ( this . disconnected && ! this . destroyed ) {
731
759
logger . log (
0 commit comments