<commit_before>class CollaborativeWorkspace extends Workspace

  init: ->
    @nickname    = KD.nick()
    @sessionData = []
    @users       = {}
    @createRemoteInstance()
    @createLoader()
    @fetchUsers()
    @createUserListContainer()
    @createChat()  if @getOptions().enableChat
    @bindRemoteEvents()

  createChat: ->
    @container.addSubView @chatView = new ChatPane delegate: this
    @chatView.hide()

  createRemoteInstance: ->
    instanceName  = @getOptions().firebaseInstance

    unless instanceName
      return warn "CollaborativeWorkspace requires a Firebase instance."

    @firepadRef   = new Firebase "https://#{instanceName}.firebaseIO.com/"
    @sessionKey   = @getOptions().sessionKey or @createSessionKey()
    @workspaceRef = @firepadRef.child @sessionKey
    @broadcastRef = @workspaceRef.child "broadcast"

  bindRemoteEvents: ->
    @workspaceRef.once "value", (snapshot) =>
      if @getOptions().sessionKey
        unless snapshot.val()?.keys
          @showNotActiveView()
          return false

      isOldSession = keys = snapshot.val()?.keys

      if isOldSession
        @sessionData  = keys
        @createPanel()
        @userRef = @workspaceRef.child("users").child @nickname
        @userRef.set "online"
        @userRef.onDisconnect().set "offline"
      else
        @createPanel()
        @workspaceRef.set "keys": @sessionData
        @userRef = @workspaceRef.child("users").child @nickname
        @userRef.set "online"
        @userRef.onDisconnect().set "offline"

      if @amIHost()
        @workspaceRef.onDisconnect().remove()
        @userRef.onDisconnect().remove()

      @loader.destroy()
      @chatView?.show()

      @emit "WorkspaceSyncedWithRemote"

      @broadcastRef.on "value", (snapshot) =>
        message = snapshot.val()
        return if not message or not message.data or message.data.sender is @nickname
        @displayBroadcastMessage message.data

    @workspaceRef.child("users").on "child_added", (snapshot) =>
      @fetchUsers()

    @workspaceRef.child("users").on "child_changed", (snapshot) =>
      name = snapshot.name()
      if @amIHost() and snapshot.val() is "offline"
        @broadcastMessage
          title     : "#{name} has left the session"
          cssClass  : "error"
          sender    : name
<commit_msg>Workspace: Minor refactor for dry.<commit_after>class CollaborativeWorkspace extends Workspace

  init: ->
    @nickname    = KD.nick()
    @sessionData = []
    @users       = {}
    @createRemoteInstance()
    @createLoader()
    @fetchUsers()
    @createUserListContainer()
    @createChat()  if @getOptions().enableChat
    @bindRemoteEvents()

  createChat: ->
    @container.addSubView @chatView = new ChatPane delegate: this
    @chatView.hide()

  createRemoteInstance: ->
    instanceName  = @getOptions().firebaseInstance

    unless instanceName
      return warn "CollaborativeWorkspace requires a Firebase instance."

    @firepadRef   = new Firebase "https://#{instanceName}.firebaseIO.com/"
    @sessionKey   = @getOptions().sessionKey or @createSessionKey()
    @workspaceRef = @firepadRef.child @sessionKey
    @broadcastRef = @workspaceRef.child "broadcast"

  bindRemoteEvents: ->
    @workspaceRef.once "value", (snapshot) =>
      if @getOptions().sessionKey
        unless snapshot.val()?.keys
          @showNotActiveView()
          return false

      isOldSession = keys = snapshot.val()?.keys

      if isOldSession
        @sessionData  = keys
        @createPanel()
      else
        @createPanel()
        @workspaceRef.set "keys": @sessionData

      @userRef = @workspaceRef.child("users").child @nickname
      @userRef.set "online"
      @userRef.onDisconnect().set "offline"

      if @amIHost()
        @workspaceRef.onDisconnect().remove()
        @userRef.onDisconnect().remove()

      @loader.destroy()
      @chatView?.show()

      @emit "WorkspaceSyncedWithRemote"

      @broadcastRef.on "value", (snapshot) =>
        message = snapshot.val()
        return if not message or not message.data or message.data.sender is @nickname
        @displayBroadcastMessage message.data

    @workspaceRef.child("users").on "child_added", (snapshot) =>
      @fetchUsers()

    @workspaceRef.child("users").on "child_changed", (snapshot) =>
      name = snapshot.name()
      if @amIHost() and snapshot.val() is "offline"
        @broadcastMessage
          title     : "#{name} has left the session"
          cssClass  : "error"
          sender    : name

{'fraction_non_alphanumeric': 0.12267080745341614, 'fraction_numerical': 0.0, 'mean_word_length': 2.7890756302521007, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7588693', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1301, 'n_words': 279}