Skip to content

Commit 7ffebdd

Browse files
authored
Merge pull request #58 from millicast/develop
Release v0.1.3
2 parents 95ec28f + 04cac83 commit 7ffebdd

File tree

8 files changed

+170
-180
lines changed

8 files changed

+170
-180
lines changed

packages/millicast-sdk/package-lock.json

Lines changed: 119 additions & 117 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/millicast-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@millicast/sdk",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "SDK for building a realtime broadcaster using the Millicast platform.",
55
"keywords": [
66
"sdk",

packages/millicast-sdk/src/Publish.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export default class Publish extends BaseWebRTC {
8080
}
8181
) {
8282
logger.debug('Broadcast option values: ', options)
83+
this.options = options
8384
if (!options.mediaStream) {
8485
logger.error('Error while broadcasting. MediaStream required')
8586
throw new Error('MediaStream required')
@@ -89,7 +90,6 @@ export default class Publish extends BaseWebRTC {
8990
throw new Error('Broadcast currently working')
9091
}
9192
let publisherData
92-
this.options = options
9393
try {
9494
publisherData = await this.tokenGenerator()
9595
} catch (error) {

packages/millicast-sdk/src/View.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ export default class View extends BaseWebRTC {
9090
}
9191
) {
9292
logger.debug('Viewer connect options values: ', options)
93+
this.options = options
9394
if (this.isActive()) {
9495
logger.warn('Viewer currently subscribed')
9596
throw new Error('Viewer currently subscribed')
9697
}
9798
let subscriberData
98-
this.options = options
9999
try {
100100
subscriberData = await this.tokenGenerator()
101101
} catch (error) {

packages/millicast-sdk/src/utils/BaseWebRTC.js

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PeerConnection, { webRTCEvents } from '../PeerConnection'
33
import { signalingEvents } from '../Signaling'
44
let logger
55
const maxReconnectionInterval = 32000
6+
const baseInterval = 1000
67

78
/**
89
* Callback invoke when a new connection path is needed.
@@ -40,7 +41,7 @@ export default class BaseWebRTC extends EventEmitter {
4041
this.signaling = null
4142
this.streamName = streamName
4243
this.autoReconnect = autoReconnect
43-
this.reconnectionInterval = 1000
44+
this.reconnectionInterval = baseInterval
4445
this.alreadyDisconnected = false
4546
this.firstReconnection = true
4647
this.tokenGenerator = tokenGenerator
@@ -98,32 +99,30 @@ export default class BaseWebRTC extends EventEmitter {
9899
* Reconnects to last broadcast.
99100
* @fires BaseWebRTC#reconnect
100101
*/
101-
reconnect () {
102-
/**
103-
* Emits with every reconnection attempt made when an active stream
104-
* stopped unexpectedly.
105-
*
106-
* @event BaseWebRTC#reconnect
107-
* @type {Object}
108-
* @property {Number} timeout - Next retry interval in milliseconds.
109-
*/
110-
this.emit('reconnect', { timeout: this.reconnectionInterval })
111-
112-
setTimeout(async () => {
113-
try {
114-
if (!this.isActive()) {
115-
this.stop()
116-
await this.connect(this.options)
117-
this.alreadyDisconnected = false
118-
this.reconnectionInterval = 1000
119-
this.firstReconnection = true
120-
}
121-
} catch (error) {
122-
this.reconnectionInterval = nextReconnectInterval(this.reconnectionInterval)
123-
logger.error(`Reconnection failed, retrying in ${this.reconnectionInterval}ms. Error was: `, error)
124-
this.reconnect()
102+
async reconnect () {
103+
try {
104+
if (!this.isActive()) {
105+
this.stop()
106+
await this.connect(this.options)
107+
this.alreadyDisconnected = false
108+
this.reconnectionInterval = baseInterval
109+
this.firstReconnection = true
125110
}
126-
}, this.reconnectionInterval)
111+
} catch (error) {
112+
this.reconnectionInterval = nextReconnectInterval(this.reconnectionInterval)
113+
logger.error(`Reconnection failed, retrying in ${this.reconnectionInterval}ms. Error was: `, error)
114+
/**
115+
* Emits with every reconnection attempt made when an active stream
116+
* stopped unexpectedly.
117+
*
118+
* @event BaseWebRTC#reconnect
119+
* @type {Object}
120+
* @property {Number} timeout - Next retry interval in milliseconds.
121+
* @property {Error} error - Error object with cause of failure.
122+
*/
123+
this.emit('reconnect', { timeout: this.reconnectionInterval, error })
124+
setTimeout(() => this.reconnect(), this.reconnectionInterval)
125+
}
127126
}
128127
}
129128

packages/millicast-sdk/tests/features/step-definitions/PublisherReconnect.steps.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ defineFeature(feature, test => {
152152
test('Reconnection interval when peer has an error', ({ given, when, then }) => {
153153
let publisher
154154
const reconnectHandler = jest.fn()
155+
const errorMessage = 'Error has ocurred'
155156

156157
given('an instance of Publish with reconnection enabled and peer with error', async () => {
157158
publisher = new Publish('streamName', mockTokenGenerator, true)
@@ -162,24 +163,24 @@ defineFeature(feature, test => {
162163
})
163164

164165
when('reconnection is called and fails', () => {
165-
jest.spyOn(publisher, 'connect').mockImplementation(() => { throw new Error() })
166+
jest.spyOn(publisher, 'connect').mockImplementation(() => { throw new Error(errorMessage) })
166167
publisher.reconnect()
167168
})
168169

169170
then('reconnection is called again in increments of 2 seconds until 32 seconds', async () => {
170-
let interval = 1000
171-
for (let i = 1; i <= 6; i++) {
171+
let interval = 2000
172+
for (let i = 1; i <= 5; i++) {
172173
expect(setTimeout).toHaveBeenCalledTimes(i)
173174
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), interval)
174175
expect(reconnectHandler).toHaveBeenCalledTimes(i)
175-
expect(reconnectHandler).toHaveBeenLastCalledWith({ timeout: interval })
176+
expect(reconnectHandler).toHaveBeenLastCalledWith({ timeout: interval, error: new Error(errorMessage) })
176177
jest.runOnlyPendingTimers()
177178
interval = interval * 2
178179
}
179-
expect(setTimeout).toHaveBeenCalledTimes(7)
180+
expect(setTimeout).toHaveBeenCalledTimes(6)
180181
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 32000)
181-
expect(reconnectHandler).toHaveBeenCalledTimes(7)
182-
expect(reconnectHandler).toHaveBeenLastCalledWith({ timeout: 32000 })
182+
expect(reconnectHandler).toHaveBeenCalledTimes(6)
183+
expect(reconnectHandler).toHaveBeenLastCalledWith({ timeout: 32000, error: new Error(errorMessage) })
183184
jest.runOnlyPendingTimers()
184185
})
185186
})
@@ -198,8 +199,7 @@ defineFeature(feature, test => {
198199
})
199200

200201
then('reconnection is not called again', async () => {
201-
jest.advanceTimersByTime(62000)
202-
expect(setTimeout).toHaveBeenCalledTimes(1)
202+
expect(setTimeout).not.toHaveBeenCalled()
203203
})
204204
})
205205

@@ -217,13 +217,7 @@ defineFeature(feature, test => {
217217
})
218218

219219
then('peer reconnects and reconnection is not called again', async () => {
220-
expect(setTimeout).toHaveBeenCalledTimes(1)
221-
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 1000)
222-
jest.runOnlyPendingTimers()
223-
jest.spyOn(publisher, 'isActive').mockImplementation(() => { return true })
224-
225-
jest.advanceTimersByTime(62000)
226-
expect(setTimeout).toHaveBeenCalledTimes(1)
220+
expect(setTimeout).not.toHaveBeenCalled()
227221
})
228222
})
229223
})

packages/millicast-sdk/tests/features/step-definitions/ViewerReconnect.steps.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ defineFeature(feature, test => {
149149
test('Reconnection interval when peer has an error', ({ given, when, then }) => {
150150
let viewer
151151
const reconnectHandler = jest.fn()
152+
const errorMessage = 'Error has ocurred'
152153

153154
given('an instance of Viewer with reconnection enabled and peer with error', async () => {
154155
viewer = new View('streamName', mockTokenGenerator, null, true)
@@ -159,24 +160,24 @@ defineFeature(feature, test => {
159160
})
160161

161162
when('reconnection is called and fails', () => {
162-
jest.spyOn(viewer, 'connect').mockImplementation(() => { throw new Error() })
163+
jest.spyOn(viewer, 'connect').mockImplementation(() => { throw new Error(errorMessage) })
163164
viewer.reconnect()
164165
})
165166

166167
then('reconnection is called again in increments of 2 seconds until 32 seconds', async () => {
167-
let interval = 1000
168-
for (let i = 1; i <= 6; i++) {
168+
let interval = 2000
169+
for (let i = 1; i <= 5; i++) {
169170
expect(setTimeout).toHaveBeenCalledTimes(i)
170171
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), interval)
171172
expect(reconnectHandler).toHaveBeenCalledTimes(i)
172-
expect(reconnectHandler).toHaveBeenLastCalledWith({ timeout: interval })
173+
expect(reconnectHandler).toHaveBeenLastCalledWith({ timeout: interval, error: new Error(errorMessage) })
173174
jest.runOnlyPendingTimers()
174175
interval = interval * 2
175176
}
176-
expect(setTimeout).toHaveBeenCalledTimes(7)
177+
expect(setTimeout).toHaveBeenCalledTimes(6)
177178
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 32000)
178-
expect(reconnectHandler).toHaveBeenCalledTimes(7)
179-
expect(reconnectHandler).toHaveBeenLastCalledWith({ timeout: 32000 })
179+
expect(reconnectHandler).toHaveBeenCalledTimes(6)
180+
expect(reconnectHandler).toHaveBeenLastCalledWith({ timeout: 32000, error: new Error(errorMessage) })
180181
jest.runOnlyPendingTimers()
181182
expect(viewer.connect).toBeCalledTimes(7)
182183
})
@@ -196,8 +197,7 @@ defineFeature(feature, test => {
196197
})
197198

198199
then('reconnection is not called again', async () => {
199-
jest.advanceTimersByTime(62000)
200-
expect(setTimeout).toHaveBeenCalledTimes(1)
200+
expect(setTimeout).not.toHaveBeenCalled()
201201
})
202202
})
203203

@@ -216,13 +216,7 @@ defineFeature(feature, test => {
216216
})
217217

218218
then('peer reconnects and reconnection is not called again', async () => {
219-
expect(setTimeout).toHaveBeenCalledTimes(1)
220-
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 1000)
221-
jest.runOnlyPendingTimers()
222-
jest.spyOn(viewer, 'isActive').mockImplementation(() => { return true })
223-
224-
jest.advanceTimersByTime(62000)
225-
expect(setTimeout).toHaveBeenCalledTimes(1)
219+
expect(setTimeout).not.toHaveBeenCalled()
226220
})
227221
})
228222
})

packages/millicast-sdk/tests/functional/ViewTest.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ class MillicastViewTest {
3838
this.millicastView.on('connectionStateChange', (state) => {
3939
console.log('Event from connectionStateChange: ', state)
4040
})
41-
this.millicastView.connect(options)
41+
await this.millicastView.connect(options)
4242
} catch (error) {
43-
console.log('There was an error while trying to connect with the publisher: ', error)
43+
console.log('There was an error while trying to connect with the publisher')
44+
this.millicastView.reconnect()
4445
}
4546
}
4647

0 commit comments

Comments
 (0)