@yz-social/webrtc 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/datachannels.js +4 -7
- package/lib/webrtc.js +1 -0
- package/package.json +1 -1
- package/spec/webrtcSpec.js +5 -1
package/lib/datachannels.js
CHANGED
|
@@ -17,11 +17,6 @@ export class WebRTCDataChannels extends WebRTCPeerEvents {
|
|
|
17
17
|
get hasStartedConnecting() {
|
|
18
18
|
return this.channelId > this.constructor.BASE_CHANNEL_ID;
|
|
19
19
|
}
|
|
20
|
-
close(removeConnection = true) {
|
|
21
|
-
this.channelId = this.constructor.BASE_CHANNEL_ID;
|
|
22
|
-
super.close();
|
|
23
|
-
if (removeConnection) this.constructor.connections.delete(this.serviceLabel);
|
|
24
|
-
}
|
|
25
20
|
async ensureDataChannel(channelName, channelOptions = {}, signals = null) { // Return a promise for an open data channel on this connection.
|
|
26
21
|
// Much of this machinery is for ealing with the first data channel on either side in the case that the connection as a whole
|
|
27
22
|
// has not yet been made.
|
|
@@ -132,12 +127,14 @@ export class WebRTCDataChannels extends WebRTCPeerEvents {
|
|
|
132
127
|
});
|
|
133
128
|
return channel;
|
|
134
129
|
}
|
|
135
|
-
close() {
|
|
130
|
+
close(removeConnection = true) {
|
|
131
|
+
this.channelId = this.constructor.BASE_CHANNEL_ID;
|
|
136
132
|
super.close();
|
|
133
|
+
if (removeConnection) this.constructor.connections.delete(this.serviceLabel);
|
|
137
134
|
// If the webrtc implementation closes the data channels before the peer itself, then this.dataChannels will be empty.
|
|
138
135
|
// But if not (e.g., status 'failed' or 'disconnected' on Safari), then let us explicitly close them so that Synchronizers know to clean up.
|
|
139
136
|
for (const channel of this.dataChannels.values()) {
|
|
140
|
-
if (channel.readyState !== 'open') continue;
|
|
137
|
+
if (channel.readyState !== 'open') continue;
|
|
141
138
|
// It appears that in Safari (18.5) for a call to channel.close() with the connection already internally closed, Safari
|
|
142
139
|
// will set channel.readyState to 'closing', but NOT fire the closed or closing event. So we have to dispatch it ourselves.
|
|
143
140
|
//channel.close();
|
package/lib/webrtc.js
CHANGED
|
@@ -16,6 +16,7 @@ export class WebRTC extends WebRTCBase {
|
|
|
16
16
|
|
|
17
17
|
const returning = await responder(this.signals); // this.signals might be an empty list.
|
|
18
18
|
if (!returning?.length) return;
|
|
19
|
+
if (this.peer.iceGatheringState !== 'gathering') return; // All done.
|
|
19
20
|
this.signals = returning;
|
|
20
21
|
await this.connectVia(responder); // Keep "long-polling" the other side until it has nothing left to say.
|
|
21
22
|
}
|
package/package.json
CHANGED
package/spec/webrtcSpec.js
CHANGED
|
@@ -25,7 +25,7 @@ describe("WebRTC", function () {
|
|
|
25
25
|
const channelName = 'test';
|
|
26
26
|
function test(Kind, connect) {
|
|
27
27
|
describe(Kind.name, function () {
|
|
28
|
-
let a = {}, b = {};
|
|
28
|
+
let a = {}, b = {}; let calledCloseA = 0, calledCloseB = 0;
|
|
29
29
|
beforeAll(async function () {
|
|
30
30
|
const debug = false;
|
|
31
31
|
a.connection = Kind.ensure({serviceLabel: 'A'+Kind.name, debug});
|
|
@@ -35,6 +35,8 @@ describe("WebRTC", function () {
|
|
|
35
35
|
|
|
36
36
|
a.testChannel = await a.dataChannelPromise;
|
|
37
37
|
b.testChannel = await b.dataChannelPromise;
|
|
38
|
+
a.testChannel.addEventListener('close', () => { calledCloseA++; });
|
|
39
|
+
b.testChannel.addEventListener('close', () => { calledCloseB++; });
|
|
38
40
|
|
|
39
41
|
await a.connection.reportConnection();
|
|
40
42
|
await b.connection.reportConnection();
|
|
@@ -44,6 +46,8 @@ describe("WebRTC", function () {
|
|
|
44
46
|
expect(a.connection.peer.connectionState).toBe('new');
|
|
45
47
|
await delay(10); // Yield to allow the other side to close.
|
|
46
48
|
expect(b.connection.peer.connectionState).toBe('new');
|
|
49
|
+
expect(calledCloseA).toBe(1);
|
|
50
|
+
expect(calledCloseB).toBe(1);
|
|
47
51
|
});
|
|
48
52
|
it("changes state appropriately.", async function () {
|
|
49
53
|
expect(await a.dataChannelPromise).toBeTruthy();
|