{"action":"created","comment":{"url":"https://api.github.com/repos/mafintosh/signalhub/pulls/comments/68146208","id":68146208,"diff_hunk":"@@ -1,78 +1,96 @@\n+var events = require('events')\n var ess = require('event-source-stream')\n var nets = require('nets')\n var pump = require('pump')\n var through = require('through2')\n+var inherits = require('inherits')\n \n-var noop = function () {}\n+module.exports = SignalHub\n \n-module.exports = function (app, urls) {\n+function SignalHub (app, urls) {\n+  if (!(this instanceof SignalHub)) return new SignalHub(app, urls)\n+\n+  events.EventEmitter.call(this)\n+  \n   if (!app) throw new Error('app name required')\n   if (!urls || !urls.length) throw new Error('signalhub url(s) required')\n \n-  var that = {}\n   if (!Array.isArray(urls)) urls = [urls]\n \n-  urls = urls.map(function (url) {\n+  this.app = app\n+  this.urls = urls.map(function (url) {\n     url = url.replace(/\\/$/, '')\n     return url.indexOf('://') === -1 ? 'http://' + url : url\n   })\n+}\n \n-  that.subscribe = function (channel) {\n-    var endpoint\n-\n-    if (Array.isArray(channel)) {\n-      endpoint = channel.join(',')\n-    } else {\n-      endpoint = channel\n-    }\n-\n-    var all = through.obj()\n-    var streams = urls.map(function (url) {\n-      return ess(url + '/v1/' + app + '/' + endpoint, {json: true})\n-    })\n+inherits(SignalHub, events.EventEmitter)\n \n-    if (streams.length === 1) return streams[0]\n+SignalHub.prototype.subscribe = function (channel) {\n+  var endpoint\n+  if (Array.isArray(channel)) {\n+    endpoint = channel.join(',')\n+  } else {\n+    endpoint = channel\n+  }\n \n-    var all = through.obj()\n+  var streams = this.urls.map(function (url) {\n+    return ess(url + '/v1/' + this.app + '/' + endpoint, {json: true})\n+  })\n \n+  var all\n+  if (streams.length === 1) {\n+    all = streams[0]\n+  } else {\n+    all = through.obj()\n     all.setMaxListeners(0)\n     streams.forEach(function (stream) {\n       stream.on('open', function () {\n         all.emit('open')\n       })\n       pump(stream, all)\n     })\n-\n-    return all\n   }\n \n-  var broadcast = function (url, channel, message, cb) {\n-    nets({\n-      method: 'POST',\n-      json: message,\n-      url: url + '/v1/' + app + '/' + channel\n-    }, function (err, res) {\n-      if (err) return cb(err)\n-      if (res.statusCode !== 200) return cb(new Error('Bad status: ' + res.statusCode))\n-      cb()\n-    })\n-  }\n+  this.on('close', function (cb) {\n+    all.destroy()\n+  })\n \n-  that.broadcast = function (channel, message, cb) {\n-    if (!cb) cb = noop\n+  return all\n+}\n \n-    var pending = urls.length\n-    var errors = 0\n+SignalHub.prototype.broadcast = function (channel, message, cb) {\n+  if (!cb) cb = noop\n \n-    urls.forEach(function (url) {\n-      broadcast(url, channel, message, function (err) {\n-        if (err) errors++\n-        if (--pending) return\n-        if (errors === urls.length) return cb(err)\n-        cb()\n-      })\n+  var pending = this.urls.length\n+  var errors = 0\n+\n+  var self = this\n+  return this.urls.forEach(function (url) {","path":"index.js","position":112,"original_position":112,"commit_id":"d9402d9127738023302fe7bb1a829333d11f0836","original_commit_id":"d9402d9127738023302fe7bb1a829333d11f0836","user":{"login":"mafintosh","id":376661,"avatar_url":"https://avatars.githubusercontent.com/u/376661?v=3","gravatar_id":"","url":"https://api.github.com/users/mafintosh","html_url":"https://github.com/mafintosh","followers_url":"https://api.github.com/users/mafintosh/followers","following_url":"https://api.github.com/users/mafintosh/following{/other_user}","gists_url":"https://api.github.com/users/mafintosh/gists{/gist_id}","starred_url":"https://api.github.com/users/mafintosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mafintosh/subscriptions","organizations_url":"https://api.github.com/users/mafintosh/orgs","repos_url":"https://api.github.com/users/mafintosh/repos","events_url":"https://api.github.com/users/mafintosh/events{/privacy}","received_events_url":"https://api.github.com/users/mafintosh/received_events","type":"User","site_admin":false},"body":"why is there a return statement here?","created_at":"2016-06-22T22:21:30Z","updated_at":"2016-06-22T22:21:30Z","html_url":"https://github.com/mafintosh/signalhub/pull/22#discussion_r68146208","pull_request_url":"https://api.github.com/repos/mafintosh/signalhub/pulls/22","_links":{"self":{"href":"https://api.github.com/repos/mafintosh/signalhub/pulls/comments/68146208"},"html":{"href":"https://github.com/mafintosh/signalhub/pull/22#discussion_r68146208"},"pull_request":{"href":"https://api.github.com/repos/mafintosh/signalhub/pulls/22"}}},"pull_request":{"url":"https://api.github.com/repos/mafintosh/signalhub/pulls/22","id":74856377,"html_url":"https://github.com/mafintosh/signalhub/pull/22","diff_url":"https://github.com/mafintosh/signalhub/pull/22.diff","patch_url":"https://github.com/mafintosh/signalhub/pull/22.patch","issue_url":"https://api.github.com/repos/mafintosh/signalhub/issues/22","number":22,"state":"open","locked":false,"title":"Refactored + added close method","user":{"login":"lukeburns","id":189068,"avatar_url":"https://avatars.githubusercontent.com/u/189068?v=3","gravatar_id":"","url":"https://api.github.com/users/lukeburns","html_url":"https://github.com/lukeburns","followers_url":"https://api.github.com/users/lukeburns/followers","following_url":"https://api.github.com/users/lukeburns/following{/other_user}","gists_url":"https://api.github.com/users/lukeburns/gists{/gist_id}","starred_url":"https://api.github.com/users/lukeburns/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukeburns/subscriptions","organizations_url":"https://api.github.com/users/lukeburns/orgs","repos_url":"https://api.github.com/users/lukeburns/repos","events_url":"https://api.github.com/users/lukeburns/events{/privacy}","received_events_url":"https://api.github.com/users/lukeburns/received_events","type":"User","site_admin":false},"body":"Added close method for https://github.com/karissa/hyperdrive-archive-swarm/issues/2","created_at":"2016-06-22T20:59:25Z","updated_at":"2016-06-22T22:21:31Z","closed_at":null,"merged_at":null,"merge_commit_sha":"25b2e6f297a3a81ebd340023d3ee009b81e2f71c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mafintosh/signalhub/pulls/22/commits","review_comments_url":"https://api.github.com/repos/mafintosh/signalhub/pulls/22/comments","review_comment_url":"https://api.github.com/repos/mafintosh/signalhub/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mafintosh/signalhub/issues/22/comments","statuses_url":"https://api.github.com/repos/mafintosh/signalhub/statuses/d9402d9127738023302fe7bb1a829333d11f0836","head":{"label":"lukeburns:master","ref":"master","sha":"d9402d9127738023302fe7bb1a829333d11f0836","user":{"login":"lukeburns","id":189068,"avatar_url":"https://avatars.githubusercontent.com/u/189068?v=3","gravatar_id":"","url":"https://api.github.com/users/lukeburns","html_url":"https://github.com/lukeburns","followers_url":"https://api.github.com/users/lukeburns/followers","following_url":"https://api.github.com/users/lukeburns/following{/other_user}","gists_url":"https://api.github.com/users/lukeburns/gists{/gist_id}","starred_url":"https://api.github.com/users/lukeburns/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukeburns/subscriptions","organizations_url":"https://api.github.com/users/lukeburns/orgs","repos_url":"https://api.github.com/users/lukeburns/repos","events_url":"https://api.github.com/users/lukeburns/events{/privacy}","received_events_url":"https://api.github.com/users/lukeburns/received_events","type":"User","site_admin":false},"repo":{"id":61747573,"name":"signalhub","full_name":"lukeburns/signalhub","owner":{"login":"lukeburns","id":189068,"avatar_url":"https://avatars.githubusercontent.com/u/189068?v=3","gravatar_id":"","url":"https://api.github.com/users/lukeburns","html_url":"https://github.com/lukeburns","followers_url":"https://api.github.com/users/lukeburns/followers","following_url":"https://api.github.com/users/lukeburns/following{/other_user}","gists_url":"https://api.github.com/users/lukeburns/gists{/gist_id}","starred_url":"https://api.github.com/users/lukeburns/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukeburns/subscriptions","organizations_url":"https://api.github.com/users/lukeburns/orgs","repos_url":"https://api.github.com/users/lukeburns/repos","events_url":"https://api.github.com/users/lukeburns/events{/privacy}","received_events_url":"https://api.github.com/users/lukeburns/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lukeburns/signalhub","description":"Simple signalling server that can be used to coordinate handshaking with webrtc or other fun stuff.","fork":true,"url":"https://api.github.com/repos/lukeburns/signalhub","forks_url":"https://api.github.com/repos/lukeburns/signalhub/forks","keys_url":"https://api.github.com/repos/lukeburns/signalhub/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lukeburns/signalhub/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lukeburns/signalhub/teams","hooks_url":"https://api.github.com/repos/lukeburns/signalhub/hooks","issue_events_url":"https://api.github.com/repos/lukeburns/signalhub/issues/events{/number}","events_url":"https://api.github.com/repos/lukeburns/signalhub/events","assignees_url":"https://api.github.com/repos/lukeburns/signalhub/assignees{/user}","branches_url":"https://api.github.com/repos/lukeburns/signalhub/branches{/branch}","tags_url":"https://api.github.com/repos/lukeburns/signalhub/tags","blobs_url":"https://api.github.com/repos/lukeburns/signalhub/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lukeburns/signalhub/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lukeburns/signalhub/git/refs{/sha}","trees_url":"https://api.github.com/repos/lukeburns/signalhub/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lukeburns/signalhub/statuses/{sha}","languages_url":"https://api.github.com/repos/lukeburns/signalhub/languages","stargazers_url":"https://api.github.com/repos/lukeburns/signalhub/stargazers","contributors_url":"https://api.github.com/repos/lukeburns/signalhub/contributors","subscribers_url":"https://api.github.com/repos/lukeburns/signalhub/subscribers","subscription_url":"https://api.github.com/repos/lukeburns/signalhub/subscription","commits_url":"https://api.github.com/repos/lukeburns/signalhub/commits{/sha}","git_commits_url":"https://api.github.com/repos/lukeburns/signalhub/git/commits{/sha}","comments_url":"https://api.github.com/repos/lukeburns/signalhub/comments{/number}","issue_comment_url":"https://api.github.com/repos/lukeburns/signalhub/issues/comments{/number}","contents_url":"https://api.github.com/repos/lukeburns/signalhub/contents/{+path}","compare_url":"https://api.github.com/repos/lukeburns/signalhub/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lukeburns/signalhub/merges","archive_url":"https://api.github.com/repos/lukeburns/signalhub/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lukeburns/signalhub/downloads","issues_url":"https://api.github.com/repos/lukeburns/signalhub/issues{/number}","pulls_url":"https://api.github.com/repos/lukeburns/signalhub/pulls{/number}","milestones_url":"https://api.github.com/repos/lukeburns/signalhub/milestones{/number}","notifications_url":"https://api.github.com/repos/lukeburns/signalhub/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lukeburns/signalhub/labels{/name}","releases_url":"https://api.github.com/repos/lukeburns/signalhub/releases{/id}","deployments_url":"https://api.github.com/repos/lukeburns/signalhub/deployments","created_at":"2016-06-22T19:57:50Z","updated_at":"2016-06-22T19:57:51Z","pushed_at":"2016-06-22T21:03:43Z","git_url":"git://github.com/lukeburns/signalhub.git","ssh_url":"git@github.com:lukeburns/signalhub.git","clone_url":"https://github.com/lukeburns/signalhub.git","svn_url":"https://github.com/lukeburns/signalhub","homepage":null,"size":47,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","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":"mafintosh:master","ref":"master","sha":"fdb6b533d5348db2e4a23e997257897584710538","user":{"login":"mafintosh","id":376661,"avatar_url":"https://avatars.githubusercontent.com/u/376661?v=3","gravatar_id":"","url":"https://api.github.com/users/mafintosh","html_url":"https://github.com/mafintosh","followers_url":"https://api.github.com/users/mafintosh/followers","following_url":"https://api.github.com/users/mafintosh/following{/other_user}","gists_url":"https://api.github.com/users/mafintosh/gists{/gist_id}","starred_url":"https://api.github.com/users/mafintosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mafintosh/subscriptions","organizations_url":"https://api.github.com/users/mafintosh/orgs","repos_url":"https://api.github.com/users/mafintosh/repos","events_url":"https://api.github.com/users/mafintosh/events{/privacy}","received_events_url":"https://api.github.com/users/mafintosh/received_events","type":"User","site_admin":false},"repo":{"id":34084952,"name":"signalhub","full_name":"mafintosh/signalhub","owner":{"login":"mafintosh","id":376661,"avatar_url":"https://avatars.githubusercontent.com/u/376661?v=3","gravatar_id":"","url":"https://api.github.com/users/mafintosh","html_url":"https://github.com/mafintosh","followers_url":"https://api.github.com/users/mafintosh/followers","following_url":"https://api.github.com/users/mafintosh/following{/other_user}","gists_url":"https://api.github.com/users/mafintosh/gists{/gist_id}","starred_url":"https://api.github.com/users/mafintosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mafintosh/subscriptions","organizations_url":"https://api.github.com/users/mafintosh/orgs","repos_url":"https://api.github.com/users/mafintosh/repos","events_url":"https://api.github.com/users/mafintosh/events{/privacy}","received_events_url":"https://api.github.com/users/mafintosh/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mafintosh/signalhub","description":"Simple signalling server that can be used to coordinate handshaking with webrtc or other fun stuff.","fork":false,"url":"https://api.github.com/repos/mafintosh/signalhub","forks_url":"https://api.github.com/repos/mafintosh/signalhub/forks","keys_url":"https://api.github.com/repos/mafintosh/signalhub/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mafintosh/signalhub/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mafintosh/signalhub/teams","hooks_url":"https://api.github.com/repos/mafintosh/signalhub/hooks","issue_events_url":"https://api.github.com/repos/mafintosh/signalhub/issues/events{/number}","events_url":"https://api.github.com/repos/mafintosh/signalhub/events","assignees_url":"https://api.github.com/repos/mafintosh/signalhub/assignees{/user}","branches_url":"https://api.github.com/repos/mafintosh/signalhub/branches{/branch}","tags_url":"https://api.github.com/repos/mafintosh/signalhub/tags","blobs_url":"https://api.github.com/repos/mafintosh/signalhub/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mafintosh/signalhub/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mafintosh/signalhub/git/refs{/sha}","trees_url":"https://api.github.com/repos/mafintosh/signalhub/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mafintosh/signalhub/statuses/{sha}","languages_url":"https://api.github.com/repos/mafintosh/signalhub/languages","stargazers_url":"https://api.github.com/repos/mafintosh/signalhub/stargazers","contributors_url":"https://api.github.com/repos/mafintosh/signalhub/contributors","subscribers_url":"https://api.github.com/repos/mafintosh/signalhub/subscribers","subscription_url":"https://api.github.com/repos/mafintosh/signalhub/subscription","commits_url":"https://api.github.com/repos/mafintosh/signalhub/commits{/sha}","git_commits_url":"https://api.github.com/repos/mafintosh/signalhub/git/commits{/sha}","comments_url":"https://api.github.com/repos/mafintosh/signalhub/comments{/number}","issue_comment_url":"https://api.github.com/repos/mafintosh/signalhub/issues/comments{/number}","contents_url":"https://api.github.com/repos/mafintosh/signalhub/contents/{+path}","compare_url":"https://api.github.com/repos/mafintosh/signalhub/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mafintosh/signalhub/merges","archive_url":"https://api.github.com/repos/mafintosh/signalhub/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mafintosh/signalhub/downloads","issues_url":"https://api.github.com/repos/mafintosh/signalhub/issues{/number}","pulls_url":"https://api.github.com/repos/mafintosh/signalhub/pulls{/number}","milestones_url":"https://api.github.com/repos/mafintosh/signalhub/milestones{/number}","notifications_url":"https://api.github.com/repos/mafintosh/signalhub/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mafintosh/signalhub/labels{/name}","releases_url":"https://api.github.com/repos/mafintosh/signalhub/releases{/id}","deployments_url":"https://api.github.com/repos/mafintosh/signalhub/deployments","created_at":"2015-04-16T22:58:07Z","updated_at":"2016-06-22T07:18:22Z","pushed_at":"2016-06-22T21:03:43Z","git_url":"git://github.com/mafintosh/signalhub.git","ssh_url":"git@github.com:mafintosh/signalhub.git","clone_url":"https://github.com/mafintosh/signalhub.git","svn_url":"https://github.com/mafintosh/signalhub","homepage":null,"size":46,"stargazers_count":204,"watchers_count":204,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":23,"mirror_url":null,"open_issues_count":1,"forks":23,"open_issues":1,"watchers":204,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mafintosh/signalhub/pulls/22"},"html":{"href":"https://github.com/mafintosh/signalhub/pull/22"},"issue":{"href":"https://api.github.com/repos/mafintosh/signalhub/issues/22"},"comments":{"href":"https://api.github.com/repos/mafintosh/signalhub/issues/22/comments"},"review_comments":{"href":"https://api.github.com/repos/mafintosh/signalhub/pulls/22/comments"},"review_comment":{"href":"https://api.github.com/repos/mafintosh/signalhub/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mafintosh/signalhub/pulls/22/commits"},"statuses":{"href":"https://api.github.com/repos/mafintosh/signalhub/statuses/d9402d9127738023302fe7bb1a829333d11f0836"}}}}
{"actor":{"display_login":"mafintosh"}}