{"action":"created","comment":{"url":"https://api.github.com/repos/node-modules/cluster-client/pulls/comments/93087670","pull_request_review_id":13607570,"id":93087670,"diff_hunk":"@@ -0,0 +1,204 @@\n+'use strict';\n+\n+const is = require('is-type-of');\n+const Reflect = require('harmony-reflect');\n+const logger = require('./default_logger');\n+const transcode = require('./default_transcode');\n+const ClusterClient = require('./client');\n+const formatKey = require('./utils').formatKey;\n+\n+const defaultOptions = {\n+  port: 7777,\n+  maxWaitTime: 30000,\n+  responseTimeout: 3000,\n+  heartbeatInterval: 10000,\n+  autoGenerate: true,\n+  isBroadcast: true,\n+  logger,\n+  transcode,\n+  formatKey,\n+};\n+\n+class ClientWrapper {\n+  /**\n+   * Cluster Client Wrapper\n+   *\n+   * @param {Function} clientClass - the client class\n+   * @param {Object} options\n+   *  - {Number} responseTimeout - response timeout, default is 3000\n+   *  - {Boolean} autoGenerate - whether generate delegate rule automatically, default is true\n+   *  - {Boolean} isBroadcast - whether broadcast subscrption result to all followers or just one, default is true\n+   *  - {Logger} logger - log instance\n+   *  - {Transcode} [transcode|JSON.stringify/parse]\n+   *    - {Function} encode - custom serialize method\n+   *    - {Function} decode - custom deserialize method\n+   *  - {Boolean} [isLeader|null] - specify whether current instance is leader\n+   *  - {Number} [maxWaitTime|30000] - leader startup max time (ONLY effective on isLeader is true)\n+   * @constructor\n+   */\n+  constructor(clientClass, options) {\n+    this._clientClass = clientClass;\n+    this._options = Object.assign({\n+      name: clientClass.prototype.constructor.name,\n+    }, defaultOptions, options);\n+\n+    // wrapper descptions\n+    this._descriptors = new Map();\n+  }\n+\n+  /**\n+   * override the property\n+   *\n+   * @param {String} name - property name\n+   * @param {Object} value - property value\n+   * @return {ClientWrapper} self\n+   */\n+  override(name, value) {\n+    this._descriptors.set(name, {\n+      type: 'override',\n+      value,\n+    });\n+    return this;\n+  }\n+\n+  /**\n+   * delegate methods\n+   *\n+   * @param {String} from - method name\n+   * @param {String} to - delegate to subscribe|publish|invoke\n+   * @return {ClientWrapper} self\n+   */\n+  delegate(from, to) {\n+    to = to || 'invoke';\n+    this._descriptors.set(from, {\n+      type: 'delegate',\n+      to,\n+    });\n+    return this;\n+  }\n+\n+  /**\n+   * create cluster client instance\n+   *\n+   * @return {Object} instance\n+   */\n+  create() {\n+    const args = Array.from(arguments);\n+    const clientClass = this._clientClass;\n+    const proto = clientClass.prototype;\n+    const descriptors = this._descriptors;\n+\n+    // auto generate description\n+    if (this._options.autoGenerate) {\n+      this._generateDescriptors();\n+    }\n+\n+    function createRealClient() {\n+      return Reflect.construct(clientClass, args);\n+    }\n+\n+    const client = new ClusterClient(Object.assign({\n+      createRealClient,\n+      descriptors: this._descriptors,\n+    }, this._options));\n+\n+    for (const name of descriptors.keys()) {\n+      let value;\n+      const descriptor = descriptors.get(name);\n+      switch (descriptor.type) {\n+        case 'override':\n+          value = descriptor.value;\n+          break;\n+        case 'delegate':\n+          if (descriptor.to === 'invoke') {\n+            if (is.generatorFunction(proto[name])) {\n+              value = function* () {\n+                const args = Array.from(arguments);\n+                return yield callback => client[Symbol.for(`ClusterClient#${descriptor.to}`)](name, args, callback);","path":"lib/index.js","position":117,"original_position":117,"commit_id":"003eefcc56a97732dc365c711d8ac06419f69784","original_commit_id":"003eefcc56a97732dc365c711d8ac06419f69784","user":{"login":"popomore","id":360661,"avatar_url":"https://avatars.githubusercontent.com/u/360661?v=3","gravatar_id":"","url":"https://api.github.com/users/popomore","html_url":"https://github.com/popomore","followers_url":"https://api.github.com/users/popomore/followers","following_url":"https://api.github.com/users/popomore/following{/other_user}","gists_url":"https://api.github.com/users/popomore/gists{/gist_id}","starred_url":"https://api.github.com/users/popomore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/popomore/subscriptions","organizations_url":"https://api.github.com/users/popomore/orgs","repos_url":"https://api.github.com/users/popomore/repos","events_url":"https://api.github.com/users/popomore/events{/privacy}","received_events_url":"https://api.github.com/users/popomore/received_events","type":"User","site_admin":false},"body":"内部的就别用 symbol.for 吧","created_at":"2016-12-19T18:15:45Z","updated_at":"2016-12-19T18:15:45Z","html_url":"https://github.com/node-modules/cluster-client/pull/1#discussion_r93087670","pull_request_url":"https://api.github.com/repos/node-modules/cluster-client/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/node-modules/cluster-client/pulls/comments/93087670"},"html":{"href":"https://github.com/node-modules/cluster-client/pull/1#discussion_r93087670"},"pull_request":{"href":"https://api.github.com/repos/node-modules/cluster-client/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/node-modules/cluster-client/pulls/1","id":98535174,"html_url":"https://github.com/node-modules/cluster-client/pull/1","diff_url":"https://github.com/node-modules/cluster-client/pull/1.diff","patch_url":"https://github.com/node-modules/cluster-client/pull/1.patch","issue_url":"https://api.github.com/repos/node-modules/cluster-client/issues/1","number":1,"state":"open","locked":false,"title":"feat: implement cluster client","user":{"login":"gxcsoccer","id":1207064,"avatar_url":"https://avatars.githubusercontent.com/u/1207064?v=3","gravatar_id":"","url":"https://api.github.com/users/gxcsoccer","html_url":"https://github.com/gxcsoccer","followers_url":"https://api.github.com/users/gxcsoccer/followers","following_url":"https://api.github.com/users/gxcsoccer/following{/other_user}","gists_url":"https://api.github.com/users/gxcsoccer/gists{/gist_id}","starred_url":"https://api.github.com/users/gxcsoccer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gxcsoccer/subscriptions","organizations_url":"https://api.github.com/users/gxcsoccer/orgs","repos_url":"https://api.github.com/users/gxcsoccer/repos","events_url":"https://api.github.com/users/gxcsoccer/events{/privacy}","received_events_url":"https://api.github.com/users/gxcsoccer/received_events","type":"User","site_admin":false},"body":"1. Master/Slave => Leader/Follower\r\n2. 协议优化，将 body 拆分成 conn 和 app 两部分，便于 app 部分的自定义\r\n3. 订阅支持选择一个 client 分发和广播两种","created_at":"2016-12-19T09:53:30Z","updated_at":"2016-12-19T18:32:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3412d61908528dcff69b8ea5c9a26c1fd0c40e2a","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/node-modules/cluster-client/pulls/1/commits","review_comments_url":"https://api.github.com/repos/node-modules/cluster-client/pulls/1/comments","review_comment_url":"https://api.github.com/repos/node-modules/cluster-client/pulls/comments{/number}","comments_url":"https://api.github.com/repos/node-modules/cluster-client/issues/1/comments","statuses_url":"https://api.github.com/repos/node-modules/cluster-client/statuses/003eefcc56a97732dc365c711d8ac06419f69784","head":{"label":"node-modules:first","ref":"first","sha":"003eefcc56a97732dc365c711d8ac06419f69784","user":{"login":"node-modules","id":7125868,"avatar_url":"https://avatars.githubusercontent.com/u/7125868?v=3","gravatar_id":"","url":"https://api.github.com/users/node-modules","html_url":"https://github.com/node-modules","followers_url":"https://api.github.com/users/node-modules/followers","following_url":"https://api.github.com/users/node-modules/following{/other_user}","gists_url":"https://api.github.com/users/node-modules/gists{/gist_id}","starred_url":"https://api.github.com/users/node-modules/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/node-modules/subscriptions","organizations_url":"https://api.github.com/users/node-modules/orgs","repos_url":"https://api.github.com/users/node-modules/repos","events_url":"https://api.github.com/users/node-modules/events{/privacy}","received_events_url":"https://api.github.com/users/node-modules/received_events","type":"Organization","site_admin":false},"repo":{"id":76217388,"name":"cluster-client","full_name":"node-modules/cluster-client","owner":{"login":"node-modules","id":7125868,"avatar_url":"https://avatars.githubusercontent.com/u/7125868?v=3","gravatar_id":"","url":"https://api.github.com/users/node-modules","html_url":"https://github.com/node-modules","followers_url":"https://api.github.com/users/node-modules/followers","following_url":"https://api.github.com/users/node-modules/following{/other_user}","gists_url":"https://api.github.com/users/node-modules/gists{/gist_id}","starred_url":"https://api.github.com/users/node-modules/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/node-modules/subscriptions","organizations_url":"https://api.github.com/users/node-modules/orgs","repos_url":"https://api.github.com/users/node-modules/repos","events_url":"https://api.github.com/users/node-modules/events{/privacy}","received_events_url":"https://api.github.com/users/node-modules/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/node-modules/cluster-client","description":"Sharing Connection among Multi-Process Nodejs","fork":false,"url":"https://api.github.com/repos/node-modules/cluster-client","forks_url":"https://api.github.com/repos/node-modules/cluster-client/forks","keys_url":"https://api.github.com/repos/node-modules/cluster-client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/node-modules/cluster-client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/node-modules/cluster-client/teams","hooks_url":"https://api.github.com/repos/node-modules/cluster-client/hooks","issue_events_url":"https://api.github.com/repos/node-modules/cluster-client/issues/events{/number}","events_url":"https://api.github.com/repos/node-modules/cluster-client/events","assignees_url":"https://api.github.com/repos/node-modules/cluster-client/assignees{/user}","branches_url":"https://api.github.com/repos/node-modules/cluster-client/branches{/branch}","tags_url":"https://api.github.com/repos/node-modules/cluster-client/tags","blobs_url":"https://api.github.com/repos/node-modules/cluster-client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/node-modules/cluster-client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/node-modules/cluster-client/git/refs{/sha}","trees_url":"https://api.github.com/repos/node-modules/cluster-client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/node-modules/cluster-client/statuses/{sha}","languages_url":"https://api.github.com/repos/node-modules/cluster-client/languages","stargazers_url":"https://api.github.com/repos/node-modules/cluster-client/stargazers","contributors_url":"https://api.github.com/repos/node-modules/cluster-client/contributors","subscribers_url":"https://api.github.com/repos/node-modules/cluster-client/subscribers","subscription_url":"https://api.github.com/repos/node-modules/cluster-client/subscription","commits_url":"https://api.github.com/repos/node-modules/cluster-client/commits{/sha}","git_commits_url":"https://api.github.com/repos/node-modules/cluster-client/git/commits{/sha}","comments_url":"https://api.github.com/repos/node-modules/cluster-client/comments{/number}","issue_comment_url":"https://api.github.com/repos/node-modules/cluster-client/issues/comments{/number}","contents_url":"https://api.github.com/repos/node-modules/cluster-client/contents/{+path}","compare_url":"https://api.github.com/repos/node-modules/cluster-client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/node-modules/cluster-client/merges","archive_url":"https://api.github.com/repos/node-modules/cluster-client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/node-modules/cluster-client/downloads","issues_url":"https://api.github.com/repos/node-modules/cluster-client/issues{/number}","pulls_url":"https://api.github.com/repos/node-modules/cluster-client/pulls{/number}","milestones_url":"https://api.github.com/repos/node-modules/cluster-client/milestones{/number}","notifications_url":"https://api.github.com/repos/node-modules/cluster-client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/node-modules/cluster-client/labels{/name}","releases_url":"https://api.github.com/repos/node-modules/cluster-client/releases{/id}","deployments_url":"https://api.github.com/repos/node-modules/cluster-client/deployments","created_at":"2016-12-12T03:03:39Z","updated_at":"2016-12-12T03:03:39Z","pushed_at":"2016-12-19T18:06:49Z","git_url":"git://github.com/node-modules/cluster-client.git","ssh_url":"git@github.com:node-modules/cluster-client.git","clone_url":"https://github.com/node-modules/cluster-client.git","svn_url":"https://github.com/node-modules/cluster-client","homepage":null,"size":25,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"node-modules:master","ref":"master","sha":"1fd47d3d4f368d8166360715a9067cf8f157e7cf","user":{"login":"node-modules","id":7125868,"avatar_url":"https://avatars.githubusercontent.com/u/7125868?v=3","gravatar_id":"","url":"https://api.github.com/users/node-modules","html_url":"https://github.com/node-modules","followers_url":"https://api.github.com/users/node-modules/followers","following_url":"https://api.github.com/users/node-modules/following{/other_user}","gists_url":"https://api.github.com/users/node-modules/gists{/gist_id}","starred_url":"https://api.github.com/users/node-modules/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/node-modules/subscriptions","organizations_url":"https://api.github.com/users/node-modules/orgs","repos_url":"https://api.github.com/users/node-modules/repos","events_url":"https://api.github.com/users/node-modules/events{/privacy}","received_events_url":"https://api.github.com/users/node-modules/received_events","type":"Organization","site_admin":false},"repo":{"id":76217388,"name":"cluster-client","full_name":"node-modules/cluster-client","owner":{"login":"node-modules","id":7125868,"avatar_url":"https://avatars.githubusercontent.com/u/7125868?v=3","gravatar_id":"","url":"https://api.github.com/users/node-modules","html_url":"https://github.com/node-modules","followers_url":"https://api.github.com/users/node-modules/followers","following_url":"https://api.github.com/users/node-modules/following{/other_user}","gists_url":"https://api.github.com/users/node-modules/gists{/gist_id}","starred_url":"https://api.github.com/users/node-modules/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/node-modules/subscriptions","organizations_url":"https://api.github.com/users/node-modules/orgs","repos_url":"https://api.github.com/users/node-modules/repos","events_url":"https://api.github.com/users/node-modules/events{/privacy}","received_events_url":"https://api.github.com/users/node-modules/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/node-modules/cluster-client","description":"Sharing Connection among Multi-Process Nodejs","fork":false,"url":"https://api.github.com/repos/node-modules/cluster-client","forks_url":"https://api.github.com/repos/node-modules/cluster-client/forks","keys_url":"https://api.github.com/repos/node-modules/cluster-client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/node-modules/cluster-client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/node-modules/cluster-client/teams","hooks_url":"https://api.github.com/repos/node-modules/cluster-client/hooks","issue_events_url":"https://api.github.com/repos/node-modules/cluster-client/issues/events{/number}","events_url":"https://api.github.com/repos/node-modules/cluster-client/events","assignees_url":"https://api.github.com/repos/node-modules/cluster-client/assignees{/user}","branches_url":"https://api.github.com/repos/node-modules/cluster-client/branches{/branch}","tags_url":"https://api.github.com/repos/node-modules/cluster-client/tags","blobs_url":"https://api.github.com/repos/node-modules/cluster-client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/node-modules/cluster-client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/node-modules/cluster-client/git/refs{/sha}","trees_url":"https://api.github.com/repos/node-modules/cluster-client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/node-modules/cluster-client/statuses/{sha}","languages_url":"https://api.github.com/repos/node-modules/cluster-client/languages","stargazers_url":"https://api.github.com/repos/node-modules/cluster-client/stargazers","contributors_url":"https://api.github.com/repos/node-modules/cluster-client/contributors","subscribers_url":"https://api.github.com/repos/node-modules/cluster-client/subscribers","subscription_url":"https://api.github.com/repos/node-modules/cluster-client/subscription","commits_url":"https://api.github.com/repos/node-modules/cluster-client/commits{/sha}","git_commits_url":"https://api.github.com/repos/node-modules/cluster-client/git/commits{/sha}","comments_url":"https://api.github.com/repos/node-modules/cluster-client/comments{/number}","issue_comment_url":"https://api.github.com/repos/node-modules/cluster-client/issues/comments{/number}","contents_url":"https://api.github.com/repos/node-modules/cluster-client/contents/{+path}","compare_url":"https://api.github.com/repos/node-modules/cluster-client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/node-modules/cluster-client/merges","archive_url":"https://api.github.com/repos/node-modules/cluster-client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/node-modules/cluster-client/downloads","issues_url":"https://api.github.com/repos/node-modules/cluster-client/issues{/number}","pulls_url":"https://api.github.com/repos/node-modules/cluster-client/pulls{/number}","milestones_url":"https://api.github.com/repos/node-modules/cluster-client/milestones{/number}","notifications_url":"https://api.github.com/repos/node-modules/cluster-client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/node-modules/cluster-client/labels{/name}","releases_url":"https://api.github.com/repos/node-modules/cluster-client/releases{/id}","deployments_url":"https://api.github.com/repos/node-modules/cluster-client/deployments","created_at":"2016-12-12T03:03:39Z","updated_at":"2016-12-12T03:03:39Z","pushed_at":"2016-12-19T18:06:49Z","git_url":"git://github.com/node-modules/cluster-client.git","ssh_url":"git@github.com:node-modules/cluster-client.git","clone_url":"https://github.com/node-modules/cluster-client.git","svn_url":"https://github.com/node-modules/cluster-client","homepage":null,"size":25,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/node-modules/cluster-client/pulls/1"},"html":{"href":"https://github.com/node-modules/cluster-client/pull/1"},"issue":{"href":"https://api.github.com/repos/node-modules/cluster-client/issues/1"},"comments":{"href":"https://api.github.com/repos/node-modules/cluster-client/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/node-modules/cluster-client/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/node-modules/cluster-client/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/node-modules/cluster-client/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/node-modules/cluster-client/statuses/003eefcc56a97732dc365c711d8ac06419f69784"}}}}
{"actor":{"display_login":"popomore"}}