'use babel'

import app from 'app'
import Menu from 'menu'
import Tray from 'tray'
import clipboard from 'clipboard'
import globalShortcut from 'global-shortcut'
import Discover from 'node-discover'
import fs from 'fs'
import dialog from 'dialog'
import path from 'path'
import mime from 'mime'
import http from 'http'
import Icon from './src/icon'
import buildWebApp from './src/build_web_app'
import fetchData from './src/fetch_data'
import EventEmitter from 'events'
import TextPayload from './src/text_payload'
import FilePayload from './src/file_payload'

class Publisher extends EventEmitter  {
  publish(payload) {
    this.last = payload
    this.emit('publish')
  }
}

var icon = new Icon()

var d = Discover()

var appIcon = null
var httpPort = null

var lastReceive = null

var publisher = new Publisher()
publisher.on('publish', () => {
  var success = d.send('clipboard', {
    httpPort: httpPort,
    payload: publisher.last.serialize()
  })
})

function receive(notice) {
  d.eachNode((node) => {
    if (node.id === notice.obj.iid) {
      fetchData(node.address, notice.data.httpPort, (dataText, dataBytes) => {
        appIcon.setImage(icon.defaultIcon())
        var mime = notice.data.payload.mime
        if (mime === 'text/plain') {
          clipboard.writeText(dataText)
        } else {
          dialog.showSaveDialog({
            title: 'Choose location to store the .' + mime.extension(mime) + ' file'
          }, function(destinationPath){
            if (destinationPath) {
              fs.writeFileSync(destinationPath, dataBytes)
            }
          })
        }
      })
    }
  })
}

d.join("clipboard", function(data, obj){
  appIcon.setImage(icon.dropIcon(data.payload.mime))
  lastReceive = {
    data: data,
    obj: obj
  }
})

app.dock ? app.dock.hide() : false // disable dock icon on OS X

app.on('ready', function(){
  appIcon = new Tray(icon.defaultIcon())
  appIcon.setToolTip('Handover')
  appIcon.on('drop-files', function(e, paths){
    publisher.publish(new FilePayload(paths[0]))
  })
  globalShortcut.register('CmdOrCtrl+shift+c', () => {
    publisher.publish(new TextPayload(clipboard.readText()))
  })
  globalShortcut.register('CmdOrCtrl+shift+v', () => {
    if (lastReceive) {
      receive(lastReceive)
    }
  })
})

app.on('will-quit', function() {
  globalShortcut.unregisterAll()
})

var httpServer = http.createServer(buildWebApp(publisher).callback())
httpServer.listen(0) // random
httpServer.on('listening', function() {
  httpPort = httpServer.address().port
  console.log('listening http://127.0.0.1:'+httpPort)
})

@@ -671,16 +671,131 @@
 %0A  %7D%0A%7D%0A%0A
+class Consumer extends EventEmitter %7B%0A  consume(payload) %7B%0A    this.last = payload%0A    this.emit('consume')%0A  %7D%0A%7D%0A%0A
 var icon
@@ -873,32 +873,8 @@
 ll%0A%0A
-var lastReceive = null%0A%0A
 var 
@@ -1044,16 +1044,148 @@
  %7D)%0A%7D)%0A%0A
+var consumer = new Consumer()%0Aconsumer.on('consume', () =%3E %7B%0A  appIcon.setImage(icon.dropIcon(consumer.last.data.payload.mime))%0A%7D)%0A%0A
 function
@@ -1888,75 +1888,25 @@
 %7B%0A  
-appIcon.setImage(icon.dropIcon(data.payload.mime))%0A  lastReceive = 
+consumer.consume(
 %7B%0A  
@@ -1935,16 +1935,17 @@
  obj%0A  %7D
+)
 %0A%7D)%0A%0Aapp
@@ -2393,27 +2393,29 @@
     if (
-lastReceive
+consumer.last
 ) %7B%0A    
@@ -2428,19 +2428,21 @@
 ive(
-lastReceive
+consumer.last
 )%0A  

Add consumer class