{"action":"created","comment":{"url":"https://api.github.com/repos/ensime/ensime-node/pulls/comments/57244493","id":57244493,"diff_hunk":"@@ -1,163 +1,158 @@\n net = require('net')\n-Swank = require './lisp/swank-protocol'\n fs = require 'fs-extra'\n path = require 'path'\n log = require('loglevel').getLogger('ensime.client')\n temp = require 'temp'\n+WebSocket = require(\"ws\")\n \n+temp.track()\n tempDir = temp.mkdirSync()\n getTempDir = -> tempDir\n \n-module.exports =\n-class Client\n-  constructor: (port, @httpPort, generalMsgHandler, @serverPid = undefined) ->\n-    @ensimeMessageCounter = 1\n-    @callbackMap = {}\n-\n-    @parser = new Swank.SwankParser( (env) =>\n-      log.trace(\"incoming: #{env}\")\n+module.exports = createClient = (port, httpPort, generalMsgHandler, serverPid = undefined) ->\n+  new Promise (resolve, reject) ->\n+    \n+    callbackMap = {}\n+\n+    ensimeMessageCounter = 1\n+      \n+    websocket = new WebSocket(\"ws://localhost:\" + httpPort + \"/jerky\")\n+\n+    websocket.on \"open\", ->\n+      log.trace \"connecting websocket…\"\n+      resolve(publicApi()) # FIXME\n+\n+    websocket.on \"message\", (msg) ->\n+      log.trace(\"incoming: #{msg}\")\n+      handleIncoming(msg)\n+\n+    websocket.on \"error\", (error) ->\n+      log.error error\n+\n+    websocket.on \"close\", ->\n+      log.trace \"websocket closed from server\"\n+\n+    publicApi = -> {\n+      post,\n+      destroy,\n+      getCompletions,\n+      getSymbolAtPoint,\n+      typecheckBuffer,\n+      typecheckFile,\n+      symbolByName,\n+      formatSourceFile\n+    }\n+      \n+    \n+    handleIncoming = (env) ->\n       json = JSON.parse(env)\n       callId = json.callId\n       # If RpcResponse - lookup in map, otherwise use some general function for handling general msgs\n \n       if(callId)\n         try\n-          @callbackMap[callId](json.payload)\n+          callbackMap[callId](json.payload)\n         catch error\n           log.trace(\"error in callback: #{error}\")\n         finally\n-          delete @callbackMap[callId]\n+          delete callbackMap[callId]\n       else\n         generalMsgHandler(json.payload)\n-    )\n-\n-    @openSocket(port)\n-\n-  # Kills server if it was spawned from here.\n-  destroy: ->\n-    @socket.destroy()\n-    @serverPid?.kill()\n-\n-  openSocket: (port) ->\n-    log.trace('connecting on port: ' + port)\n-    @socket = net.connect({port: port, allowHalfOpen: true} , ->\n-      log.trace('client connected')\n-    )\n-\n-    @socket.on('data', (data) =>\n-      @parser.execute(data)\n-    )\n-\n-    @socket.on('end', ->\n-      log.trace(\"Ensime server disconnected\")\n-    )\n-\n-    @socket.on('close', (data) ->\n-      log.trace(\"Ensime server close event: \" + data)\n-    )\n-\n-    @socket.on('error', (data) ->\n-      if (data.code == 'ECONNREFUSED')\n-        log.error(\"Connection refused connecting to ensime, it is probably not running. Remove .ensime_cache/port and .ensime_cache/http and try again.\")\n-      else if (data.code == 'EADDRNOTAVAIL')\n-        log.trace(data)\n-        # happens when connecting too soon I think\n-      else\n-        log.trace(\"Ensime server error event: \" + data)\n-    )\n-\n-    @socket.on('timeout', ->\n-      log.trace(\"Ensime server timeout event\")\n-    )\n-\n-  postString: (msg, callback) =>\n-    swankMsg = Swank.buildMessage \"\"\"{\"req\": #{msg}, \"callId\": #{@ensimeMessageCounter}}\"\"\"\n-    @callbackMap[@ensimeMessageCounter++] = callback\n-    log.trace(\"outgoing: \" + swankMsg)\n-    @socket.write(swankMsg, \"UTF8\")\n-\n-  # Public:\n-  post: (msg, callback) ->\n-    @postString(JSON.stringify(msg), callback)\n-\n-\n-  getCompletions: (filePath, bufferText, offset, noOfAutocompleteSuggestions, callback) =>\n-    tempFilePath = getTempDir() + filePath\n-    fs.outputFile(tempFilePath, bufferText, (err) =>\n-      if (err)\n-        throw err\n-      else\n-        msg =\n-          typehint: \"CompletionsReq\"\n-          fileInfo:\n-            file: filePath\n-            contentsIn: tempFilePath\n-          point: offset\n-          maxResults: noOfAutocompleteSuggestions\n-          caseSens: false\n-          reload: true\n-        @post(msg, callback)\n-    )\n-    \n-    \n- \n-  getSymbolAtPoint: (path, offset, callback) ->\n-    req =\n-      typehint: \"SymbolAtPointReq\"\n-      file: path\n-      point: offset\n-    @post(req, (msg) ->\n-      if msg.typehint == 'SymbolInfo'\n-        callback(msg)\n-      else\n-        # if msg.typehint == 'FalseResponse'\n-        # do nothing\n-    )\n     \n \n-  typecheckBuffer: (path, text, callback = ->) =>\n-    tempFilePath = getTempDir() + path\n-    fs.outputFile(tempFilePath, text, (err) =>\n-      if (err)\n-        throw err\n-      else\n-        msg =\n-          typehint: \"TypecheckFileReq\"\n-          fileInfo:\n-            file: path\n-            contentsIn: tempFilePath\n-        @post(msg, callback)\n-    )\n-\n-\n-  \n-  typecheckFile: (path) =>\n-    msg =\n-      typehint: \"TypecheckFileReq\"\n-      fileInfo:\n-        file: path\n-    @post(msg, (result) ->)\n+    # Kills server if it was spawned from here.\n+    destroy = ->\n+      websocket.terminate()\n+      serverPid?.kill()\n \n-\n-  symbolByName: (qualifiedName, callback) ->\n-    msg =\n-      typehint: 'SymbolByNameReq'\n-      typeFullName: qualifiedName\n-    @post(msg, callback)\n     \n+    postString = (msg, callback) ->\n+      msg = \"\"\"{\"req\": #{msg}, \"callId\": #{ensimeMessageCounter}}\"\"\"\n+      callbackMap[ensimeMessageCounter++] = callback","path":"src/client.coffee","position":194,"original_position":194,"commit_id":"448f59d4a4533ff735be423ae54c7a8ee9f1dcbc","original_commit_id":"448f59d4a4533ff735be423ae54c7a8ee9f1dcbc","user":{"login":"d6y","id":102661,"avatar_url":"https://avatars.githubusercontent.com/u/102661?v=3","gravatar_id":"","url":"https://api.github.com/users/d6y","html_url":"https://github.com/d6y","followers_url":"https://api.github.com/users/d6y/followers","following_url":"https://api.github.com/users/d6y/following{/other_user}","gists_url":"https://api.github.com/users/d6y/gists{/gist_id}","starred_url":"https://api.github.com/users/d6y/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/d6y/subscriptions","organizations_url":"https://api.github.com/users/d6y/orgs","repos_url":"https://api.github.com/users/d6y/repos","events_url":"https://api.github.com/users/d6y/events{/privacy}","received_events_url":"https://api.github.com/users/d6y/received_events","type":"User","site_admin":false},"body":"I'm worried about the `++` increment.  If we do one call  a second we're gong to run into [MAX_SAFE_INTEGER](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER) in just 285 million years.\r\n\r\nOh, come on. That was a good joke.","created_at":"2016-03-23T21:57:41Z","updated_at":"2016-03-23T21:57:41Z","html_url":"https://github.com/ensime/ensime-node/pull/5#discussion_r57244493","pull_request_url":"https://api.github.com/repos/ensime/ensime-node/pulls/5","_links":{"self":{"href":"https://api.github.com/repos/ensime/ensime-node/pulls/comments/57244493"},"html":{"href":"https://github.com/ensime/ensime-node/pull/5#discussion_r57244493"},"pull_request":{"href":"https://api.github.com/repos/ensime/ensime-node/pulls/5"}}},"pull_request":{"url":"https://api.github.com/repos/ensime/ensime-node/pulls/5","id":63950870,"html_url":"https://github.com/ensime/ensime-node/pull/5","diff_url":"https://github.com/ensime/ensime-node/pull/5.diff","patch_url":"https://github.com/ensime/ensime-node/pull/5.patch","issue_url":"https://api.github.com/repos/ensime/ensime-node/issues/5","number":5,"state":"open","locked":false,"title":"Feature/websockets","user":{"login":"hedefalk","id":116520,"avatar_url":"https://avatars.githubusercontent.com/u/116520?v=3","gravatar_id":"","url":"https://api.github.com/users/hedefalk","html_url":"https://github.com/hedefalk","followers_url":"https://api.github.com/users/hedefalk/followers","following_url":"https://api.github.com/users/hedefalk/following{/other_user}","gists_url":"https://api.github.com/users/hedefalk/gists{/gist_id}","starred_url":"https://api.github.com/users/hedefalk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hedefalk/subscriptions","organizations_url":"https://api.github.com/users/hedefalk/orgs","repos_url":"https://api.github.com/users/hedefalk/repos","events_url":"https://api.github.com/users/hedefalk/events{/privacy}","received_events_url":"https://api.github.com/users/hedefalk/received_events","type":"User","site_admin":false},"body":"Replaced the old swank envelopes with websocket.","created_at":"2016-03-23T20:51:50Z","updated_at":"2016-03-23T21:57:41Z","closed_at":null,"merged_at":null,"merge_commit_sha":"0357ed637425911f187ba2dfc510d385d3ecaeed","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ensime/ensime-node/pulls/5/commits","review_comments_url":"https://api.github.com/repos/ensime/ensime-node/pulls/5/comments","review_comment_url":"https://api.github.com/repos/ensime/ensime-node/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ensime/ensime-node/issues/5/comments","statuses_url":"https://api.github.com/repos/ensime/ensime-node/statuses/448f59d4a4533ff735be423ae54c7a8ee9f1dcbc","head":{"label":"hedefalk:feature/websockets","ref":"feature/websockets","sha":"448f59d4a4533ff735be423ae54c7a8ee9f1dcbc","user":{"login":"hedefalk","id":116520,"avatar_url":"https://avatars.githubusercontent.com/u/116520?v=3","gravatar_id":"","url":"https://api.github.com/users/hedefalk","html_url":"https://github.com/hedefalk","followers_url":"https://api.github.com/users/hedefalk/followers","following_url":"https://api.github.com/users/hedefalk/following{/other_user}","gists_url":"https://api.github.com/users/hedefalk/gists{/gist_id}","starred_url":"https://api.github.com/users/hedefalk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hedefalk/subscriptions","organizations_url":"https://api.github.com/users/hedefalk/orgs","repos_url":"https://api.github.com/users/hedefalk/repos","events_url":"https://api.github.com/users/hedefalk/events{/privacy}","received_events_url":"https://api.github.com/users/hedefalk/received_events","type":"User","site_admin":false},"repo":{"id":53394077,"name":"ensime-node","full_name":"hedefalk/ensime-node","owner":{"login":"hedefalk","id":116520,"avatar_url":"https://avatars.githubusercontent.com/u/116520?v=3","gravatar_id":"","url":"https://api.github.com/users/hedefalk","html_url":"https://github.com/hedefalk","followers_url":"https://api.github.com/users/hedefalk/followers","following_url":"https://api.github.com/users/hedefalk/following{/other_user}","gists_url":"https://api.github.com/users/hedefalk/gists{/gist_id}","starred_url":"https://api.github.com/users/hedefalk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hedefalk/subscriptions","organizations_url":"https://api.github.com/users/hedefalk/orgs","repos_url":"https://api.github.com/users/hedefalk/repos","events_url":"https://api.github.com/users/hedefalk/events{/privacy}","received_events_url":"https://api.github.com/users/hedefalk/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hedefalk/ensime-node","description":"Shared NodeJS ENSIME library","fork":true,"url":"https://api.github.com/repos/hedefalk/ensime-node","forks_url":"https://api.github.com/repos/hedefalk/ensime-node/forks","keys_url":"https://api.github.com/repos/hedefalk/ensime-node/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hedefalk/ensime-node/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hedefalk/ensime-node/teams","hooks_url":"https://api.github.com/repos/hedefalk/ensime-node/hooks","issue_events_url":"https://api.github.com/repos/hedefalk/ensime-node/issues/events{/number}","events_url":"https://api.github.com/repos/hedefalk/ensime-node/events","assignees_url":"https://api.github.com/repos/hedefalk/ensime-node/assignees{/user}","branches_url":"https://api.github.com/repos/hedefalk/ensime-node/branches{/branch}","tags_url":"https://api.github.com/repos/hedefalk/ensime-node/tags","blobs_url":"https://api.github.com/repos/hedefalk/ensime-node/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hedefalk/ensime-node/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hedefalk/ensime-node/git/refs{/sha}","trees_url":"https://api.github.com/repos/hedefalk/ensime-node/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hedefalk/ensime-node/statuses/{sha}","languages_url":"https://api.github.com/repos/hedefalk/ensime-node/languages","stargazers_url":"https://api.github.com/repos/hedefalk/ensime-node/stargazers","contributors_url":"https://api.github.com/repos/hedefalk/ensime-node/contributors","subscribers_url":"https://api.github.com/repos/hedefalk/ensime-node/subscribers","subscription_url":"https://api.github.com/repos/hedefalk/ensime-node/subscription","commits_url":"https://api.github.com/repos/hedefalk/ensime-node/commits{/sha}","git_commits_url":"https://api.github.com/repos/hedefalk/ensime-node/git/commits{/sha}","comments_url":"https://api.github.com/repos/hedefalk/ensime-node/comments{/number}","issue_comment_url":"https://api.github.com/repos/hedefalk/ensime-node/issues/comments{/number}","contents_url":"https://api.github.com/repos/hedefalk/ensime-node/contents/{+path}","compare_url":"https://api.github.com/repos/hedefalk/ensime-node/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hedefalk/ensime-node/merges","archive_url":"https://api.github.com/repos/hedefalk/ensime-node/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hedefalk/ensime-node/downloads","issues_url":"https://api.github.com/repos/hedefalk/ensime-node/issues{/number}","pulls_url":"https://api.github.com/repos/hedefalk/ensime-node/pulls{/number}","milestones_url":"https://api.github.com/repos/hedefalk/ensime-node/milestones{/number}","notifications_url":"https://api.github.com/repos/hedefalk/ensime-node/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hedefalk/ensime-node/labels{/name}","releases_url":"https://api.github.com/repos/hedefalk/ensime-node/releases{/id}","deployments_url":"https://api.github.com/repos/hedefalk/ensime-node/deployments","created_at":"2016-03-08T08:05:32Z","updated_at":"2016-03-08T08:05:33Z","pushed_at":"2016-03-23T20:50:32Z","git_url":"git://github.com/hedefalk/ensime-node.git","ssh_url":"git@github.com:hedefalk/ensime-node.git","clone_url":"https://github.com/hedefalk/ensime-node.git","svn_url":"https://github.com/hedefalk/ensime-node","homepage":null,"size":250,"stargazers_count":0,"watchers_count":0,"language":"CoffeeScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"ensime:master","ref":"master","sha":"63d97c595e62baf51d2919ee0d98ff7e5b5fd3aa","user":{"login":"ensime","id":5089042,"avatar_url":"https://avatars.githubusercontent.com/u/5089042?v=3","gravatar_id":"","url":"https://api.github.com/users/ensime","html_url":"https://github.com/ensime","followers_url":"https://api.github.com/users/ensime/followers","following_url":"https://api.github.com/users/ensime/following{/other_user}","gists_url":"https://api.github.com/users/ensime/gists{/gist_id}","starred_url":"https://api.github.com/users/ensime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ensime/subscriptions","organizations_url":"https://api.github.com/users/ensime/orgs","repos_url":"https://api.github.com/users/ensime/repos","events_url":"https://api.github.com/users/ensime/events{/privacy}","received_events_url":"https://api.github.com/users/ensime/received_events","type":"Organization","site_admin":false},"repo":{"id":52395230,"name":"ensime-node","full_name":"ensime/ensime-node","owner":{"login":"ensime","id":5089042,"avatar_url":"https://avatars.githubusercontent.com/u/5089042?v=3","gravatar_id":"","url":"https://api.github.com/users/ensime","html_url":"https://github.com/ensime","followers_url":"https://api.github.com/users/ensime/followers","following_url":"https://api.github.com/users/ensime/following{/other_user}","gists_url":"https://api.github.com/users/ensime/gists{/gist_id}","starred_url":"https://api.github.com/users/ensime/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ensime/subscriptions","organizations_url":"https://api.github.com/users/ensime/orgs","repos_url":"https://api.github.com/users/ensime/repos","events_url":"https://api.github.com/users/ensime/events{/privacy}","received_events_url":"https://api.github.com/users/ensime/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ensime/ensime-node","description":"Shared NodeJS ENSIME library","fork":false,"url":"https://api.github.com/repos/ensime/ensime-node","forks_url":"https://api.github.com/repos/ensime/ensime-node/forks","keys_url":"https://api.github.com/repos/ensime/ensime-node/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ensime/ensime-node/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ensime/ensime-node/teams","hooks_url":"https://api.github.com/repos/ensime/ensime-node/hooks","issue_events_url":"https://api.github.com/repos/ensime/ensime-node/issues/events{/number}","events_url":"https://api.github.com/repos/ensime/ensime-node/events","assignees_url":"https://api.github.com/repos/ensime/ensime-node/assignees{/user}","branches_url":"https://api.github.com/repos/ensime/ensime-node/branches{/branch}","tags_url":"https://api.github.com/repos/ensime/ensime-node/tags","blobs_url":"https://api.github.com/repos/ensime/ensime-node/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ensime/ensime-node/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ensime/ensime-node/git/refs{/sha}","trees_url":"https://api.github.com/repos/ensime/ensime-node/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ensime/ensime-node/statuses/{sha}","languages_url":"https://api.github.com/repos/ensime/ensime-node/languages","stargazers_url":"https://api.github.com/repos/ensime/ensime-node/stargazers","contributors_url":"https://api.github.com/repos/ensime/ensime-node/contributors","subscribers_url":"https://api.github.com/repos/ensime/ensime-node/subscribers","subscription_url":"https://api.github.com/repos/ensime/ensime-node/subscription","commits_url":"https://api.github.com/repos/ensime/ensime-node/commits{/sha}","git_commits_url":"https://api.github.com/repos/ensime/ensime-node/git/commits{/sha}","comments_url":"https://api.github.com/repos/ensime/ensime-node/comments{/number}","issue_comment_url":"https://api.github.com/repos/ensime/ensime-node/issues/comments{/number}","contents_url":"https://api.github.com/repos/ensime/ensime-node/contents/{+path}","compare_url":"https://api.github.com/repos/ensime/ensime-node/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ensime/ensime-node/merges","archive_url":"https://api.github.com/repos/ensime/ensime-node/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ensime/ensime-node/downloads","issues_url":"https://api.github.com/repos/ensime/ensime-node/issues{/number}","pulls_url":"https://api.github.com/repos/ensime/ensime-node/pulls{/number}","milestones_url":"https://api.github.com/repos/ensime/ensime-node/milestones{/number}","notifications_url":"https://api.github.com/repos/ensime/ensime-node/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ensime/ensime-node/labels{/name}","releases_url":"https://api.github.com/repos/ensime/ensime-node/releases{/id}","deployments_url":"https://api.github.com/repos/ensime/ensime-node/deployments","created_at":"2016-02-23T22:10:34Z","updated_at":"2016-03-23T21:31:47Z","pushed_at":"2016-03-23T20:51:50Z","git_url":"git://github.com/ensime/ensime-node.git","ssh_url":"git@github.com:ensime/ensime-node.git","clone_url":"https://github.com/ensime/ensime-node.git","svn_url":"https://github.com/ensime/ensime-node","homepage":null,"size":245,"stargazers_count":1,"watchers_count":1,"language":"CoffeeScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":1,"forks":3,"open_issues":1,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ensime/ensime-node/pulls/5"},"html":{"href":"https://github.com/ensime/ensime-node/pull/5"},"issue":{"href":"https://api.github.com/repos/ensime/ensime-node/issues/5"},"comments":{"href":"https://api.github.com/repos/ensime/ensime-node/issues/5/comments"},"review_comments":{"href":"https://api.github.com/repos/ensime/ensime-node/pulls/5/comments"},"review_comment":{"href":"https://api.github.com/repos/ensime/ensime-node/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ensime/ensime-node/pulls/5/commits"},"statuses":{"href":"https://api.github.com/repos/ensime/ensime-node/statuses/448f59d4a4533ff735be423ae54c7a8ee9f1dcbc"}}}}