Skip to content

Commit

Permalink
fix: add tests, refactor code to exclude potential bugs, add doc
Browse files Browse the repository at this point in the history
The API changed: WebChannel#open function returns a Promise instead of key. WebRTCService uses
WebRTC Promise based API instead of callbacks (thats why webrtc-adapter dependecy was added. Because
Chrome does not support yet WebRTC Promise based API). Some tests where refactored to be more simple
and verbose. Many docs where added.
  • Loading branch information
kalitine committed Apr 29, 2016
1 parent ed78a47 commit ca5f31b
Show file tree
Hide file tree
Showing 21 changed files with 3,854 additions and 1,458 deletions.
1,877 changes: 1,498 additions & 379 deletions dist/netflux.es2015.js

Large diffs are not rendered by default.

2,036 changes: 1,590 additions & 446 deletions dist/netflux.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion dist/netflux.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ module.exports = function(config) {
'/src/': '/base/src/',
'/dist/': '/base/dist/',
'/test/': '/base/test/',
'/jspm_packages/': '/base/jspm_packages/'
'/jspm_packages/': '/base/jspm_packages/',
'/node_modules/': '/base/node_modules/'
},


// list of files / patterns to load in the browser
files: [
'dist/netflux.es2015.js',
'node_modules/babel-polyfill/dist/polyfill.js'
'node_modules/webrtc-adapter/out/adapter_no_edge_no_global.js'
],


Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@
"jasmine": "^2.4.1",
"jasmine-core": "^2.4.1",
"jsdoc": "^3.4.0",
"jspm": "^0.16.19",
"jspm": "^0.16.33",
"karma": "^0.13.15",
"karma-babel-preprocessor": "^6.0.1",
"karma-chrome-launcher": "^0.2.2",
"karma-coverage": "douglasduteil/karma-coverage#next",
"karma-firefox-launcher": "^0.1.7",
"karma-jasmine": "^0.3.6",
"karma-jspm": "^2.0.2",
"karma-opera-launcher": "^0.3.0",
"karma-sourcemap-loader": "^0.3.6",
"rollup": "^0.26.0",
"semantic-release": "^4.3.5",
Expand All @@ -79,6 +78,7 @@
"webpack": "^1.12.11"
},
"jspm": {
"dependencies": {},
"devDependencies": {
"babel": "npm:babel-core@^5.8.24",
"babel-runtime": "npm:babel-runtime@^5.8.24",
Expand All @@ -97,7 +97,12 @@
"afterEach",
"beforeAll",
"afterAll",
"DOMException"
"DOMException",
"RTCPeerConnection",
"RTCSessionDescription",
"RTCDataChannel",
"RTCIceCandidate",
"WebSocket"
],
"ignore": [
"dist/*",
Expand Down
54 changes: 53 additions & 1 deletion src/JoiningPeer.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,72 @@
/**
* This class represents a temporary state of a peer, while he is about to join
* the web channel. During the joining process every peer in the web channel
* and the joining peer have an instance of this class with the same `id` and
* `intermediaryId` attribute values. After the joining process has been finished
* regardless of success, these instances will be deleted.
*/
class JoiningPeer {
constructor (id, intermediaryId) {
/**
* The joining peer id.
*
* @type {string}
*/
this.id = id

/**
* The id of the peer who invited the joining peer to the web channel. It is
* a member of the web channel and called an intermediary peer between the
* joining peer and the web channel. The same value for all instances.
*
* @type {string}
*/
this.intermediaryId = intermediaryId

/**
* The channel between the joining peer and intermediary peer. It is null
* for every peer, but the joining and intermediary peers.
*
* @type {ChannelInterface}
*/
this.intermediaryChannel = null

/**
* This attribute is proper to each peer. Array of channels which will be
* added to the current peer once the joining peer become the member of the
* web channel.
*
* @type {Array[ChannelInterface]}
*/
this.channelsToAdd = []

/**
* This attribute is proper to each peer. Array of channels which will be
* closed with the current peer once the joining peer become the member of the
* web channel.
*
* @type {Array[ChannelInterface]}
*/
this.channelsToRemove = []
}

/**
* Add channel to `channelsToAdd` array.
*
* @param {ChannelInterface} channel - Channel to add.
*/
toAddList (channel) {
this.channelsToAdd[this.channelsToAdd.length] = channel
}

/**
* Add channel to `channelsToRemove` array
*
* @param {ChannelInterface} channel - Channel to add.
*/
toRemoveList (channel) {
this.channelsToAdd[this.channelsToAdd.length] = channel
}

}

export default JoiningPeer
100 changes: 41 additions & 59 deletions src/WebChannel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as serviceProvider from './serviceProvider'
import { JOIN_NEW_MEMBER, LEAVE, USER_DATA, JOIN_INIT, JOIN_FINILIZE, REMOVE_NEW_MEMBER, SERVICE_DATA } from './service/channelProxy/channelProxy'
import { JOIN_NEW_MEMBER, LEAVE, USER_DATA, JOIN_INIT, JOIN_FINILIZE, REMOVE_NEW_MEMBER, SERVICE_DATA, INIT_CHANNEL_PONG } from './service/channelProxy/channelProxy'
import JoiningPeer from './JoiningPeer'

/**
Expand Down Expand Up @@ -69,8 +69,6 @@ class WebChannel {
this.joiningPeers = new Set()
/** @private */
this.connectWithRequests = new Map()
/** @private */
this.connections = new Map()

/** @private */
this.proxy = serviceProvider.get(serviceProvider.CHANNEL_PROXY)
Expand Down Expand Up @@ -144,39 +142,33 @@ class WebChannel {

let cBuilder = serviceProvider.get(settings.connector, settings)
let key = this.id + this.myId
try {
let data = cBuilder.open(this, key, (channel) => {
// this.initChannel(channel)
return cBuilder.open(key, (channel) => {
this.initChannel(channel, false).then((channel) => {
let jp = new JoiningPeer(channel.peerId, this.myId)
jp.intermediaryChannel = channel
this.joiningPeers.add(jp)
console.log('send JOIN_INIT his new id: ' + channel.peerId)
console.log('New channel: ' + channel.readyState)
channel.send(this.proxy.msg(JOIN_INIT,
{manager: this.settings.topology,
id: channel.peerId,
intermediaryId: this.myId}
))
this.manager.broadcast(this, this.proxy.msg(JOIN_NEW_MEMBER,
{id: channel.peerId, intermediaryId: this.myId}
))
channel.send(this.proxy.msg(JOIN_INIT, {
manager: this.settings.topology,
id: channel.peerId,
intermediaryId: this.myId})
)
this.manager.broadcast(this, this.proxy.msg(JOIN_NEW_MEMBER, {
id: channel.peerId,
intermediaryId: this.myId})
)
this.manager.add(channel)
.then(() => {
channel.send(this.proxy.msg(JOIN_FINILIZE))
})
.then(() => channel.send(this.proxy.msg(JOIN_FINILIZE)))
.catch((msg) => {
console.log(`Adding peer ${channel.peerId} failed: ${msg}`)
this.manager.broadcast(this, this.proxy.msg(REMOVE_NEW_MEMBER,
{id: channel.peerId}
))
this.manager.broadcast(this, this.proxy.msg(REMOVE_NEW_MEMBER, {
id: channel.peerId})
)
this.removeJoiningPeer(jp.id)
})
})
}).then((data) => {
this.webRTCOpen = data.socket
return data.key
} catch (e) {
console.log('WebChannel open error: ', e)
}
return {key: data.key, url: data.url}
})
}

/**
Expand All @@ -200,16 +192,12 @@ class WebChannel {

let cBuilder = serviceProvider.get(settings.connector, settings)
return new Promise((resolve, reject) => {
cBuilder
.join(this, key)
cBuilder.join(key)
.then((channel) => {
// this.initChannel(channel)
console.log('JOIN channel established')
this.onJoin = () => {
resolve(this)
}
this.onJoin = () => resolve(this)
return this.initChannel(channel, true)
})
.catch((reason) => reject(reason))
.catch(reject)
})
}

Expand Down Expand Up @@ -286,31 +274,25 @@ class WebChannel {
return this.settings.topology
}

/**
* initChannel - description
*
* @private
* @param {type} channel description
* @param {type} id = '' description
* @return {type} description
*/
initChannel (channel, id = '') {
channel.webChannel = this
channel.onmessage = this.proxy.onMsg
channel.onerror = this.proxy.onError
channel.onclose = this.proxy.onClose
if (id !== '') {
channel.peerId = id
} else {
channel.peerId = this.generateId()
}
channel.connection.oniceconnectionstatechange = () => {
console.log('STATE FOR ' + channel.peerId + ' CHANGED TO: ', channel.connection.iceConnectionState)
if (channel.connection.iceConnectionState === 'disconnected') {
this.channels.delete(channel)
this.onLeaving(channel.peerId)
initChannel (channel, isInitiator, id = '') {
return new Promise((resolve, reject) => {
channel.webChannel = this
channel.peerId = (id !== '') ? id : this.generateId()
// TODO: treat the case when the 'ping' or 'pong' message has not been received
if (isInitiator) {
this.proxy.configChannel(channel)
channel.onPong = () => resolve(channel)
channel.send('ping')
} else {
channel.onmessage = (msgEvt) => {
if (msgEvt.data === 'ping') {
this.proxy.configChannel(channel)
channel.send(this.proxy.msg(INIT_CHANNEL_PONG))
resolve(channel)
}
}
}
}
})
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* @see {@link https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Error}
*/

import '../node_modules/webrtc-adapter/out/adapter_no_edge_no_global'
import WebChannel from './WebChannel'
import * as service from './serviceProvider'

const WEBRTC = service.WEBRTC
const FULLY_CONNECTED = service.FULLY_CONNECTED

export { WEBRTC, FULLY_CONNECTED, WebChannel }
export * from '../node_modules/webrtc-adapter/out/adapter_no_edge_no_global'
Loading

0 comments on commit ca5f31b

Please sign in to comment.