var {Store} = require('flummox');

var {createIceServers} = require('helpers/WebRTCAdapter');
var {getPreferredAudioCodec} = require('helpers/WebRTCConstraints');
var _ = require('lodash');

class WebRTCStore extends Store {
  static deserialize(json) {
    return json;
  }

  static serialize(state) {
    return state;
  }

  constructor(registry) {
    super();

    var webRTCActionIds = registry.getActionIds('webRTC');
    var webRTCActionBindings = [
      // Public facing actions.
      ['fetchTurn', this.handleFetchTurn],
      ['prepareAsHost', this.handlePrepareAsHost],
      ['disconnect', this.handleDisconnect],

      // Internal actions regarding signaling and RTCPeerConnection.
      ['_createPeer', this.handleCreatePeer],
      ['_createPeerLocalStream', this.handleCreatePeerLocalStream],
      ['_createPeerOffer', this.handleCreatePeerLocalDescription],
      ['_createPeerAnswer', this.handleCreatePeerLocalDescription],
      ['_receivePeerIceCandidate', this.handleReceivePeerIceCandidate],
      ['_receivePeerRemoteDescription', this.handleReceivePeerRemoteDescription],
      ['_receivePeerRemoteStream', this.handleReceivePeerRemoteStream]
    ];

    webRTCActionBindings.forEach(([action, handler]) => {
      this.register(webRTCActionIds[action], handler);
    });

    this.registry = registry;
    this.state = {
      // State variables representing mostly immutable server configuration.
      audioReceiveCodec: null,
      audioSendCodec: null,
      mediaConstraints: null,
      offerConstraints: null,
      pcConfig: null,
      pcConstraints: null,
      stereo: null,
      turnUrl: null,

      // State variables representing client status.
      isTurnFetchingComplete: false,
      localStream: null,
      meetingId: null,
      pc: null,
      remoteStream: null
    };
  }

  getPreferredAudioSendCodec(sdp) {
    if (audioSendCodec == '') {
      console.log('No preference on audio send codec.');
      return sdp;
    }
    console.log('Prefer audio send codec: ' + audioSendCodec);
    return getPreferredAudioCodec(sdp, audioSendCodec);
  }

  getPreferredAudioReceiveCodec(sdp) {
    if (audioReceiveCodec == '') {
      console.log('No preference on audio receive codec.');
      return sdp;
    }
    console.log('Prefer audio receive codec: ' + audioReceiveCodec);
    return getPreferredAudioCodec(sdp, audioReceiveCodec);
  }

  handlePrepareAsHost(stream) {
    this.setState({localStream: stream});
  }

  handleDisconnect() {
    var {localStream, pc} = this.state;

    localStream && localStream.stop();
    pc && pc.close();

    this.setState({
      localStream: null,
      meetingId: null,
      pc: null,
      remoteStream: null
    });
  }

  handleFetchTurn(turnServer) {
    if (turnServer === null) {
      this.setState({isTurnFetchingComplete: true});
      return;
    }

    var {uris, username, password} = turnServer;
    var iceServers = createIceServers(uris, username, password);

    if (iceServers !== null) {
      var {pcConfig} = this.state;
      pcConfig.iceServers = pcConfig.iceServers.concat(iceServers);

      this.setState({
        isTurnFetchingComplete: true,
        pcConfig: pcConfig
      });
    }
  }

  handleCreatePeer(pc) {
    this.setState({pc: pc})
  }

  handleCreatePeerLocalStream(stream) {
    this.state.pc.addStream(stream);
  }

  handleCreatePeerLocalDescription(sessionDescription) {
    this.state.pc.setLocalDescription(sessionDescription);
  }

  handleReceivePeerIceCandidate(candidate) {
    this.state.pc.addIceCandidate(candidate);
  }

  handleReceivePeerRemoteDescription(sessionDescription) {
    this.state.pc.setRemoteDescription(sessionDescription);
  }

  handleReceivePeerRemoteStream(stream) {
    this.setState({remoteStream: stream});
  }
}

module.exports = WebRTCStore;
@@ -578,16 +578,124 @@
 sHost%5D,%0A
+      %5B'connectAsHost', %5Bthis.handleConnectBegin%5D%5D,%0A      %5B'connectAsAudience', %5Bthis.handleConnectBegin%5D%5D,%0A
       %5B'
@@ -1341,16 +1341,130 @@
 %5D) =%3E %7B%0A
+      if (_.isArray(handler)) %7B%0A        this.registerAsync(webRTCActionIds%5Baction%5D, ...handler);%0A      %7D else %7B%0A  
       th
@@ -1506,24 +1506,32 @@
 , handler);%0A
+      %7D%0A
     %7D);%0A%0A   
@@ -2689,32 +2689,114 @@
  stream%7D);%0A  %7D%0A%0A
+  handleConnectBegin(meetingId) %7B%0A    this.setState(%7BmeetingId: meetingId%7D);%0A  %7D%0A%0A
   handleDisconne

Make sure we store the meetingId at the start of connect sequence.