
var populate = fetch('/content')
  .then(function(res){ return res.text()})
  .then(function(text){
    console.log("content:" + text)
    code.setValue(text)
  })


// don't listen if we're the master (other checks too)
if(document.location.hash != '#master') {

  fetch('/pusher/config')
    .then(function(res){return res.json()})
    .then(function(config){
      var pusher = new Pusher(config.key, config.options)

      var channel = pusher.subscribe('codealong')

      channel.bind('update', function(action){
        console.log("update", action)

        code.setOption('readOnly', action.readOnly ? 'nocursor' : false)

        if(typeof(action.body) !== 'undefined') {
          code.setValue(action.body)
        }

      })

    })

} else {

  console.log("Master!")

  populate
    .then(function(){

      code.on('change', debounce(function(){

        console.log("putting")

        fetch('/content', {
          credentials: 'same-origin',
          method: 'PUT',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            value: code.getValue()
          })
        })

      },750))

      function debounce(fn, millis, timer){
        return function(){
          clearTimeout(timer)
          timer = setTimeout(fn,millis,arguments)
        }
      }

    })

}

@@ -730,24 +730,380 @@
 %0A%0A      %7D)%0A%0A
+      // Pusher.logToConsole = true%0A%0A      var channel = pusher.subscribe('presence-codealong')%0A      channel.bind('pusher:subscription_succeeded', function(e)%7B%0A        var element = document.getElementById('connection-state')%0A        element.style.display = 'block'%0A        element.className = 'connected'%0A        element.innerText = '_'+e.myID%0A      %7D)%0A%0A
     %7D)%0A%0A%7D el

connect to presence channel