From a60320df3a2b89b56bd17699b7ccf00b0678aabe Mon Sep 17 00:00:00 2001 From: Kevin Sawicki & Nathan Sobo Date: Fri, 26 Jul 2013 12:25:15 -0600 Subject: [PATCH] Add Participant class --- .../collaboration/lib/collaboration.coffee | 4 +-- .../collaboration/lib/participant-view.coffee | 6 ++-- .../collaboration/lib/participant.coffee | 15 +++++++++ src/packages/collaboration/lib/session.coffee | 32 +++++++++++++------ .../spec/collaboration-spec.coffee | 2 +- 5 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 src/packages/collaboration/lib/participant.coffee diff --git a/src/packages/collaboration/lib/collaboration.coffee b/src/packages/collaboration/lib/collaboration.coffee index a4446e1cc..0d7d34f0d 100644 --- a/src/packages/collaboration/lib/collaboration.coffee +++ b/src/packages/collaboration/lib/collaboration.coffee @@ -20,10 +20,10 @@ module.exports = session.on 'participant-entered', (participant) => view = @createParticipant(session, participant) - participantViews[participant.id] = view + participantViews[participant.clientId] = view session.on 'participant-exited', (participant) => - view = participantViews[participant.id] + view = participantViews[participant.clientId] view.detach() rootView.eachPane (pane) -> diff --git a/src/packages/collaboration/lib/participant-view.coffee b/src/packages/collaboration/lib/participant-view.coffee index b6c89b2be..940c737bf 100644 --- a/src/packages/collaboration/lib/participant-view.coffee +++ b/src/packages/collaboration/lib/participant-view.coffee @@ -14,8 +14,10 @@ class ParticipantView extends View @div class: 'volume-container', outlet: 'volumeContainer', => @div class: 'volume', outlet: 'volume' - initialize: (@session, {id, email}) -> - @session.waitForStream (stream) => + initialize: (@session, @participant) -> + {id, email} = @participant.getState() + + @session.waitForStream (stream) => @video[0].src = URL.createObjectURL(stream) @video.click => diff --git a/src/packages/collaboration/lib/participant.coffee b/src/packages/collaboration/lib/participant.coffee new file mode 100644 index 000000000..601a346a7 --- /dev/null +++ b/src/packages/collaboration/lib/participant.coffee @@ -0,0 +1,15 @@ +_ = require 'underscore' + +module.exports = +class Participant + constructor: (@state) -> + {@clientId} = @state + + getState: -> @state + + isEqual: (other) -> + if other instanceof @constructor + otherState = other.getState() + else + otherState = other + _.isEqual(@getState(), otherState) diff --git a/src/packages/collaboration/lib/session.coffee b/src/packages/collaboration/lib/session.coffee index 459908801..81b28b089 100644 --- a/src/packages/collaboration/lib/session.coffee +++ b/src/packages/collaboration/lib/session.coffee @@ -7,6 +7,7 @@ patrick = require 'patrick' MediaConnection = require './media-connection' Project = require 'project' WsChannel = require './ws-channel' +Participant = require './participant' module.exports = class Session @@ -39,14 +40,11 @@ class Session @listening = false @trigger 'stopped' - @channel.on 'channel:participant-entered', (participant) => - @participants.push(participant) - @trigger 'participant-entered', participant + @channel.on 'channel:participant-entered', (participantState) => + @trigger 'participant-entered', @addParticipant(participantState) - @channel.on 'channel:participant-exited', (participant) => - @participants = @participants.filter ({clientId}) -> - clientId isnt participant.clientId - @trigger 'participant-exited', participant + @channel.on 'channel:participant-exited', (participantState) => + @trigger 'participant-exited', @removeParticipant(participantState) if @isLeader() @doc = @createDocument() @@ -54,7 +52,8 @@ class Session @mediaConnection.start() @connectDocument() - @channel.one 'channel:subscribed', (@participants) => + @channel.one 'channel:subscribed', (participantStates) => + @setParticipantStates(participantStates) @listening = true @trigger 'started', @getParticipants() @@ -67,7 +66,8 @@ class Session @channel.send 'welcome', welcomePackage else - @channel.one 'channel:subscribed', (@participants) => + @channel.one 'channel:subscribed', (participantStates) => + @setParticipantStates(participantStates) @channel.one 'welcome', ({doc, siteId, repoSnapshot}) => @site = new Site(siteId) @doc = @site.deserializeDocument(doc) @@ -108,6 +108,20 @@ class Session getOtherParticipants: -> @getParticipants().filter ({clientId}) => clientId isnt @clientId + addParticipant: (participantState) -> + participant = new Participant(participantState) + @participants.push(participant) + participant + + removeParticipant: (participantState) -> + clientIdToRemove = participantState.clientId + participant = _.find @participants, ({clientId}) -> clientId is clientIdToRemove + @participants = _.without(@participants, participant) + participant + + setParticipantStates: (participantStates) -> + @participants = participantStates.map (state) -> new Participant(state) + subscribe: (name) -> token = keytar.getPassword('github.com', 'github') channel = new WsChannel({name, @host, @port, @secure, token}) diff --git a/src/packages/collaboration/spec/collaboration-spec.coffee b/src/packages/collaboration/spec/collaboration-spec.coffee index f8ab1fef8..dcd045f18 100644 --- a/src/packages/collaboration/spec/collaboration-spec.coffee +++ b/src/packages/collaboration/spec/collaboration-spec.coffee @@ -9,7 +9,7 @@ ServerHost = 'localhost' ServerPort = 8081 describe "Collaboration", -> - describe "when a host and a guest join a channel", -> + fdescribe "when a host and a guest join a channel", -> [server, leaderSession, guestSession, leaderStartedHandler, guestStartedHandler, guestStoppedHandler, token, userDataByToken] = [] beforeEach ->