{"action":"created","comment":{"url":"https://api.github.com/repos/ampproject/amphtml/pulls/comments/124827175","pull_request_review_id":47154888,"id":124827175,"diff_hunk":"@@ -0,0 +1,232 @@\n+/**\n+ * Copyright 2017 The AMP HTML Authors. All Rights Reserved.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *      http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS-IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+import {dev} from '../../../src/log';\n+import {AMP_ANALYTICS_3P_MESSAGE_TYPE} from '../../../src/3p-analytics-common';\n+import {throttle} from '../../../src/utils/rate-limit';\n+\n+/** @private @const {string} */\n+const TAG_ = 'amp-analytics.CrossDomainIframeMessageQueue';\n+\n+/** @const {number} */\n+export const MESSAGE_THROTTLE_TIME = 100;\n+\n+/** @private @const {number} */\n+const MAX_QUEUE_SIZE_ = 100;\n+\n+/**\n+ * @visibleForTesting\n+ * @abstract\n+ */\n+class AbstractAmpAnalytics3pMessageQueue {\n+  /**\n+   * Constructor\n+   * @param {!Window} win The window element\n+   * @param {!../../../3p/iframe-messaging-client.IframeMessagingClient}\n+   *   iframeMessagingClient Facilitates cross-frame communication\n+   * @param {!string} messageType The type that will be used to\n+   * contain the queued data (see 3p-analytics-common)\n+   */\n+  constructor(win, iframeMessagingClient, messageType) {\n+    /** @private {!Window} */\n+    this.win_ = win;\n+\n+    /** @private {!../../../3p/iframe-messaging-client.IframeMessagingClient} */\n+    this.iframeMessagingClient_ = iframeMessagingClient;\n+\n+    /** @private {string} */\n+    this.messageType_ = messageType;\n+\n+    /** @private {boolean} */\n+    this.isReady_ = false;\n+\n+    /** @private {!Object<string,!string|!Array<string>>} */\n+    this.creativeToPendingMessages_ = {};\n+\n+    /** @private {function(...*)} */\n+    this.throttledFlushQueue_ = throttle(this.win_, () => {\n+      this.flushQueue_();\n+    }, MESSAGE_THROTTLE_TIME);\n+  }\n+\n+  /**\n+   * Indicate that a cross-domain frame is ready to receive messages, and\n+   * send all messages that were previously queued for it.\n+   */\n+  setIsReady() {\n+    this.isReady_ = true;\n+    this.throttledFlushQueue_();\n+  }\n+\n+  /**\n+   * Send queued data (if there is any) to a cross-domain iframe\n+   * This should never be called directly. Use this.throttledFlushQueue_()\n+   * instead.\n+   * @private\n+   */\n+  flushQueue_() {\n+    if (this.isReady_ && Object.keys(this.creativeToPendingMessages_).length) {\n+      const jsonMsg = /** @type {JsonObject} */ (this.buildMessage());\n+      this.iframeMessagingClient_./*OK*/sendMessage(this.messageType_, jsonMsg);\n+      this.creativeToPendingMessages_ = {};\n+    }\n+  }\n+\n+  /**\n+   * @return {../../../src/3p-analytics-common.AmpAnalytics3pNewCreative|\n+   *          ../../../src/3p-analytics-common.AmpAnalytics3pEvent}\n+   * @abstract\n+   * @VisibleForTesting\n+   */\n+  buildMessage() {\n+  }\n+\n+  /**\n+   * Returns how many senderId -> message(s) mappings there are\n+   * @return {number}\n+   * @VisibleForTesting\n+   */\n+  count() {\n+    return Object.keys(this.creativeToPendingMessages_).length;\n+  }\n+\n+  /**\n+   * Returns whether the queue has been marked as ready yet\n+   * @return {boolean}\n+   * @VisibleForTesting\n+   */\n+  isReady() {\n+    return this.isReady_;\n+  }\n+}\n+\n+export class AmpAnalytics3pNewCreativeMessageQueue extends\n+  AbstractAmpAnalytics3pMessageQueue {\n+\n+  /**\n+   * Constructor\n+   * @param {!Window} win The window element\n+   * @param {!../../../3p/iframe-messaging-client.IframeMessagingClient}\n+   *   iframeMessagingClient Facilitates cross-frame communication\n+   */\n+  constructor(win, iframeMessagingClient) {\n+    super(win, iframeMessagingClient, AMP_ANALYTICS_3P_MESSAGE_TYPE.CREATIVE);\n+  }\n+\n+  /**\n+   * Enqueues an AmpAnalytics3pNewCreative message to be sent to a cross-domain\n+   * iframe.\n+   * @param {!string} senderId Identifies which creative is sending the message\n+   * @param {string=} opt_data The data to be enqueued and then sent to the\n+   * iframe\n+   */\n+  enqueue(senderId, opt_data) {\n+    dev().assert(!this.creativeToPendingMessages_[senderId],\n+        'Replacing existing extra data for: ' + senderId);\n+    this.creativeToPendingMessages_[senderId] = opt_data || '';\n+    this.throttledFlushQueue_();\n+  }\n+\n+  /**\n+   * Builds a message object containing the queued data\n+   * @return {../../../src/3p-analytics-common.AmpAnalytics3pNewCreative}\n+   * @override\n+   * @VisibleForTesting\n+   */\n+  buildMessage() {\n+    const message =\n+      /** @type {../../../src/3p-analytics-common.AmpAnalytics3pNewCreative} */\n+      ({\n+        type: this.messageType_,\n+        data: this.creativeToPendingMessages_,\n+      });\n+    return message;\n+  }\n+\n+  /**\n+   * Test method to see which message (if any) is associated with a given\n+   * senderId\n+   * @param {!string} senderId Identifies which creative is sending the message\n+   * @return {string}\n+   * @VisibleForTesting\n+   */\n+  messageFor(senderId) {\n+    return /** @type {string} */ (","path":"extensions/amp-analytics/0.1/amp-analytics-3p-message-queue.js","position":167,"original_position":167,"commit_id":"8c4a13dca20330e08e90ec4125c101e4304d930c","original_commit_id":"8c4a13dca20330e08e90ec4125c101e4304d930c","user":{"login":"jonkeller","id":2455047,"avatar_url":"https://avatars2.githubusercontent.com/u/2455047?v=3","gravatar_id":"","url":"https://api.github.com/users/jonkeller","html_url":"https://github.com/jonkeller","followers_url":"https://api.github.com/users/jonkeller/followers","following_url":"https://api.github.com/users/jonkeller/following{/other_user}","gists_url":"https://api.github.com/users/jonkeller/gists{/gist_id}","starred_url":"https://api.github.com/users/jonkeller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonkeller/subscriptions","organizations_url":"https://api.github.com/users/jonkeller/orgs","repos_url":"https://api.github.com/users/jonkeller/repos","events_url":"https://api.github.com/users/jonkeller/events{/privacy}","received_events_url":"https://api.github.com/users/jonkeller/received_events","type":"User","site_admin":false},"body":"It's typed has having string-based keys, but we are returning the value, which is typed as string|Array<string>. (String for this subclass, Array<string> for the other.) So without the cast:\r\n```\r\nextensions/amp-analytics/0.1/amp-analytics-3p-message-queue.js:167: ERROR - inconsistent return type\r\nfound   : (Array<string>|string)\r\nrequired: string\r\n    return this.creativeToPendingMessages_[senderId];\r\n```\r\n","created_at":"2017-06-29T15:06:06Z","updated_at":"2017-06-29T15:06:06Z","html_url":"https://github.com/ampproject/amphtml/pull/10185#discussion_r124827175","pull_request_url":"https://api.github.com/repos/ampproject/amphtml/pulls/10185","_links":{"self":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/comments/124827175"},"html":{"href":"https://github.com/ampproject/amphtml/pull/10185#discussion_r124827175"},"pull_request":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/10185"}}},"pull_request":{"url":"https://api.github.com/repos/ampproject/amphtml/pulls/10185","id":128013321,"html_url":"https://github.com/ampproject/amphtml/pull/10185","diff_url":"https://github.com/ampproject/amphtml/pull/10185.diff","patch_url":"https://github.com/ampproject/amphtml/pull/10185.patch","issue_url":"https://api.github.com/repos/ampproject/amphtml/issues/10185","number":10185,"state":"open","locked":false,"title":"Amp analytics 3p vendors, impl side","user":{"login":"jonkeller","id":2455047,"avatar_url":"https://avatars2.githubusercontent.com/u/2455047?v=3","gravatar_id":"","url":"https://api.github.com/users/jonkeller","html_url":"https://github.com/jonkeller","followers_url":"https://api.github.com/users/jonkeller/followers","following_url":"https://api.github.com/users/jonkeller/following{/other_user}","gists_url":"https://api.github.com/users/jonkeller/gists{/gist_id}","starred_url":"https://api.github.com/users/jonkeller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonkeller/subscriptions","organizations_url":"https://api.github.com/users/jonkeller/orgs","repos_url":"https://api.github.com/users/jonkeller/repos","events_url":"https://api.github.com/users/jonkeller/events{/privacy}","received_events_url":"https://api.github.com/users/jonkeller/received_events","type":"User","site_admin":false},"body":"Splits PR 9661 in two. This is the impl side. The other side was PRed separately in https://github.com/ampproject/amphtml/pull/10184\r\n\r\nEnables amp-analytics tag to use third-party vendors. These vendors' code will be run in a non-AMP iframe.\r\n\r\nOne-pager: https://docs.google.com/document/d/1mCW7HbQBWQONTWA2q_rKNJyTEdqGOyhX-x-ces4JHWc/edit#","created_at":"2017-06-28T20:31:48Z","updated_at":"2017-06-29T15:06:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7de5f0fee19972365b54635eb11467bc5f79fa64","assignee":null,"assignees":[],"requested_reviewers":[{"login":"lannka","id":8496897,"avatar_url":"https://avatars0.githubusercontent.com/u/8496897?v=3","gravatar_id":"","url":"https://api.github.com/users/lannka","html_url":"https://github.com/lannka","followers_url":"https://api.github.com/users/lannka/followers","following_url":"https://api.github.com/users/lannka/following{/other_user}","gists_url":"https://api.github.com/users/lannka/gists{/gist_id}","starred_url":"https://api.github.com/users/lannka/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lannka/subscriptions","organizations_url":"https://api.github.com/users/lannka/orgs","repos_url":"https://api.github.com/users/lannka/repos","events_url":"https://api.github.com/users/lannka/events{/privacy}","received_events_url":"https://api.github.com/users/lannka/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/ampproject/amphtml/pulls/10185/commits","review_comments_url":"https://api.github.com/repos/ampproject/amphtml/pulls/10185/comments","review_comment_url":"https://api.github.com/repos/ampproject/amphtml/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ampproject/amphtml/issues/10185/comments","statuses_url":"https://api.github.com/repos/ampproject/amphtml/statuses/8c4a13dca20330e08e90ec4125c101e4304d930c","head":{"label":"google:amp_analytics_3p_impl","ref":"amp_analytics_3p_impl","sha":"8c4a13dca20330e08e90ec4125c101e4304d930c","user":{"login":"google","id":1342004,"avatar_url":"https://avatars2.githubusercontent.com/u/1342004?v=3","gravatar_id":"","url":"https://api.github.com/users/google","html_url":"https://github.com/google","followers_url":"https://api.github.com/users/google/followers","following_url":"https://api.github.com/users/google/following{/other_user}","gists_url":"https://api.github.com/users/google/gists{/gist_id}","starred_url":"https://api.github.com/users/google/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/google/subscriptions","organizations_url":"https://api.github.com/users/google/orgs","repos_url":"https://api.github.com/users/google/repos","events_url":"https://api.github.com/users/google/events{/privacy}","received_events_url":"https://api.github.com/users/google/received_events","type":"Organization","site_admin":false},"repo":{"id":51965089,"name":"amphtml","full_name":"google/amphtml","owner":{"login":"google","id":1342004,"avatar_url":"https://avatars2.githubusercontent.com/u/1342004?v=3","gravatar_id":"","url":"https://api.github.com/users/google","html_url":"https://github.com/google","followers_url":"https://api.github.com/users/google/followers","following_url":"https://api.github.com/users/google/following{/other_user}","gists_url":"https://api.github.com/users/google/gists{/gist_id}","starred_url":"https://api.github.com/users/google/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/google/subscriptions","organizations_url":"https://api.github.com/users/google/orgs","repos_url":"https://api.github.com/users/google/repos","events_url":"https://api.github.com/users/google/events{/privacy}","received_events_url":"https://api.github.com/users/google/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/google/amphtml","description":"AMP HTML source code, samples, and documentation.  See below for more info.","fork":true,"url":"https://api.github.com/repos/google/amphtml","forks_url":"https://api.github.com/repos/google/amphtml/forks","keys_url":"https://api.github.com/repos/google/amphtml/keys{/key_id}","collaborators_url":"https://api.github.com/repos/google/amphtml/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/google/amphtml/teams","hooks_url":"https://api.github.com/repos/google/amphtml/hooks","issue_events_url":"https://api.github.com/repos/google/amphtml/issues/events{/number}","events_url":"https://api.github.com/repos/google/amphtml/events","assignees_url":"https://api.github.com/repos/google/amphtml/assignees{/user}","branches_url":"https://api.github.com/repos/google/amphtml/branches{/branch}","tags_url":"https://api.github.com/repos/google/amphtml/tags","blobs_url":"https://api.github.com/repos/google/amphtml/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/google/amphtml/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/google/amphtml/git/refs{/sha}","trees_url":"https://api.github.com/repos/google/amphtml/git/trees{/sha}","statuses_url":"https://api.github.com/repos/google/amphtml/statuses/{sha}","languages_url":"https://api.github.com/repos/google/amphtml/languages","stargazers_url":"https://api.github.com/repos/google/amphtml/stargazers","contributors_url":"https://api.github.com/repos/google/amphtml/contributors","subscribers_url":"https://api.github.com/repos/google/amphtml/subscribers","subscription_url":"https://api.github.com/repos/google/amphtml/subscription","commits_url":"https://api.github.com/repos/google/amphtml/commits{/sha}","git_commits_url":"https://api.github.com/repos/google/amphtml/git/commits{/sha}","comments_url":"https://api.github.com/repos/google/amphtml/comments{/number}","issue_comment_url":"https://api.github.com/repos/google/amphtml/issues/comments{/number}","contents_url":"https://api.github.com/repos/google/amphtml/contents/{+path}","compare_url":"https://api.github.com/repos/google/amphtml/compare/{base}...{head}","merges_url":"https://api.github.com/repos/google/amphtml/merges","archive_url":"https://api.github.com/repos/google/amphtml/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/google/amphtml/downloads","issues_url":"https://api.github.com/repos/google/amphtml/issues{/number}","pulls_url":"https://api.github.com/repos/google/amphtml/pulls{/number}","milestones_url":"https://api.github.com/repos/google/amphtml/milestones{/number}","notifications_url":"https://api.github.com/repos/google/amphtml/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/google/amphtml/labels{/name}","releases_url":"https://api.github.com/repos/google/amphtml/releases{/id}","deployments_url":"https://api.github.com/repos/google/amphtml/deployments","created_at":"2016-02-18T00:04:16Z","updated_at":"2017-06-23T21:54:24Z","pushed_at":"2017-06-29T14:13:46Z","git_url":"git://github.com/google/amphtml.git","ssh_url":"git@github.com:google/amphtml.git","clone_url":"https://github.com/google/amphtml.git","svn_url":"https://github.com/google/amphtml","homepage":"https://ampproject.org","size":257012,"stargazers_count":104,"watchers_count":104,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":29,"mirror_url":null,"open_issues_count":0,"forks":29,"open_issues":0,"watchers":104,"default_branch":"master"}},"base":{"label":"ampproject:master","ref":"master","sha":"5905f9d86439090ff8ae1637f3b957bfb904b119","user":{"login":"ampproject","id":14114390,"avatar_url":"https://avatars0.githubusercontent.com/u/14114390?v=3","gravatar_id":"","url":"https://api.github.com/users/ampproject","html_url":"https://github.com/ampproject","followers_url":"https://api.github.com/users/ampproject/followers","following_url":"https://api.github.com/users/ampproject/following{/other_user}","gists_url":"https://api.github.com/users/ampproject/gists{/gist_id}","starred_url":"https://api.github.com/users/ampproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ampproject/subscriptions","organizations_url":"https://api.github.com/users/ampproject/orgs","repos_url":"https://api.github.com/users/ampproject/repos","events_url":"https://api.github.com/users/ampproject/events{/privacy}","received_events_url":"https://api.github.com/users/ampproject/received_events","type":"Organization","site_admin":false},"repo":{"id":41766002,"name":"amphtml","full_name":"ampproject/amphtml","owner":{"login":"ampproject","id":14114390,"avatar_url":"https://avatars0.githubusercontent.com/u/14114390?v=3","gravatar_id":"","url":"https://api.github.com/users/ampproject","html_url":"https://github.com/ampproject","followers_url":"https://api.github.com/users/ampproject/followers","following_url":"https://api.github.com/users/ampproject/following{/other_user}","gists_url":"https://api.github.com/users/ampproject/gists{/gist_id}","starred_url":"https://api.github.com/users/ampproject/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ampproject/subscriptions","organizations_url":"https://api.github.com/users/ampproject/orgs","repos_url":"https://api.github.com/users/ampproject/repos","events_url":"https://api.github.com/users/ampproject/events{/privacy}","received_events_url":"https://api.github.com/users/ampproject/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ampproject/amphtml","description":"AMP HTML source code, samples, and documentation.  See below for more info.","fork":false,"url":"https://api.github.com/repos/ampproject/amphtml","forks_url":"https://api.github.com/repos/ampproject/amphtml/forks","keys_url":"https://api.github.com/repos/ampproject/amphtml/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ampproject/amphtml/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ampproject/amphtml/teams","hooks_url":"https://api.github.com/repos/ampproject/amphtml/hooks","issue_events_url":"https://api.github.com/repos/ampproject/amphtml/issues/events{/number}","events_url":"https://api.github.com/repos/ampproject/amphtml/events","assignees_url":"https://api.github.com/repos/ampproject/amphtml/assignees{/user}","branches_url":"https://api.github.com/repos/ampproject/amphtml/branches{/branch}","tags_url":"https://api.github.com/repos/ampproject/amphtml/tags","blobs_url":"https://api.github.com/repos/ampproject/amphtml/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ampproject/amphtml/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ampproject/amphtml/git/refs{/sha}","trees_url":"https://api.github.com/repos/ampproject/amphtml/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ampproject/amphtml/statuses/{sha}","languages_url":"https://api.github.com/repos/ampproject/amphtml/languages","stargazers_url":"https://api.github.com/repos/ampproject/amphtml/stargazers","contributors_url":"https://api.github.com/repos/ampproject/amphtml/contributors","subscribers_url":"https://api.github.com/repos/ampproject/amphtml/subscribers","subscription_url":"https://api.github.com/repos/ampproject/amphtml/subscription","commits_url":"https://api.github.com/repos/ampproject/amphtml/commits{/sha}","git_commits_url":"https://api.github.com/repos/ampproject/amphtml/git/commits{/sha}","comments_url":"https://api.github.com/repos/ampproject/amphtml/comments{/number}","issue_comment_url":"https://api.github.com/repos/ampproject/amphtml/issues/comments{/number}","contents_url":"https://api.github.com/repos/ampproject/amphtml/contents/{+path}","compare_url":"https://api.github.com/repos/ampproject/amphtml/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ampproject/amphtml/merges","archive_url":"https://api.github.com/repos/ampproject/amphtml/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ampproject/amphtml/downloads","issues_url":"https://api.github.com/repos/ampproject/amphtml/issues{/number}","pulls_url":"https://api.github.com/repos/ampproject/amphtml/pulls{/number}","milestones_url":"https://api.github.com/repos/ampproject/amphtml/milestones{/number}","notifications_url":"https://api.github.com/repos/ampproject/amphtml/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ampproject/amphtml/labels{/name}","releases_url":"https://api.github.com/repos/ampproject/amphtml/releases{/id}","deployments_url":"https://api.github.com/repos/ampproject/amphtml/deployments","created_at":"2015-09-01T22:10:53Z","updated_at":"2017-06-29T14:35:04Z","pushed_at":"2017-06-29T15:04:15Z","git_url":"git://github.com/ampproject/amphtml.git","ssh_url":"git@github.com:ampproject/amphtml.git","clone_url":"https://github.com/ampproject/amphtml.git","svn_url":"https://github.com/ampproject/amphtml","homepage":"https://www.ampproject.org","size":255947,"stargazers_count":10244,"watchers_count":10244,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":1816,"mirror_url":null,"open_issues_count":861,"forks":1816,"open_issues":861,"watchers":10244,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/10185"},"html":{"href":"https://github.com/ampproject/amphtml/pull/10185"},"issue":{"href":"https://api.github.com/repos/ampproject/amphtml/issues/10185"},"comments":{"href":"https://api.github.com/repos/ampproject/amphtml/issues/10185/comments"},"review_comments":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/10185/comments"},"review_comment":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ampproject/amphtml/pulls/10185/commits"},"statuses":{"href":"https://api.github.com/repos/ampproject/amphtml/statuses/8c4a13dca20330e08e90ec4125c101e4304d930c"}}}}
{"actor":{"display_login":"jonkeller"}}