Fix addTapListener both on initial load and on navigation.

Initial page load attempted to access a stale actionsApi that was set
at construction time. This wasn't valid until a SHOW_REVISION_ACTIONS
occurred.

Snapshots of actionsApi and restApi were used across page navigation.
These stale references would prevent proper addTapListener registration.

Change-Id: I12ffd0433badccb5370505f3ac391b8d458d9480
diff --git a/web/automerger.ts b/web/automerger.ts
index 96bcab5..fe294a0 100644
--- a/web/automerger.ts
+++ b/web/automerger.ts
@@ -18,8 +18,6 @@
   ActionInfo,
   ChangeInfo,
 } from '@gerritcodereview/typescript-api/rest-api';
-import {RestPluginApi} from '@gerritcodereview/typescript-api/rest';
-import {ChangeActionsPluginApi} from '@gerritcodereview/typescript-api/change-actions';
 import {PopupPluginApi} from '@gerritcodereview/typescript-api/popup';
 import {PluginApi} from '@gerritcodereview/typescript-api/plugin';
 import {RequestPayload} from '@gerritcodereview/typescript-api/rest';
@@ -44,13 +42,10 @@
 
   private downstreamConfigMap: ConfigMap = {};
 
-  readonly restApi: RestPluginApi;
+  readonly plugin: PluginApi;
 
-  readonly actionsApi: ChangeActionsPluginApi;
-
-  constructor(readonly plugin: PluginApi) {
-    this.restApi = plugin.restApi();
-    this.actionsApi = plugin.changeActions();
+  constructor(readonly p: PluginApi) {
+    this.plugin = p;
   }
 
   private callAction(payload: RequestPayload, onSuccess: () => void) {
@@ -149,12 +144,17 @@
   private getDownstreamConfigMap() {
     const change = this.change;
     if (!change) return;
+
     const changeId = change._number;
+    if(changeId === undefined) return;
+
     const revisionId = change.current_revision;
+    if(revisionId === undefined) return;
+
     const url =
       `/changes/${changeId}/revisions/${revisionId}` +
       '/automerger~config-downstream';
-    this.restApi.post<ConfigMap>(url, {subject: change.subject}).then(resp => {
+    this.plugin.restApi().post<ConfigMap>(url, {subject: change.subject}).then(resp => {
       this.downstreamConfigMap = resp;
       this.styleRelatedChanges();
     });
@@ -162,13 +162,19 @@
 
   onShowChange(change: ChangeInfo) {
     this.change = change;
-    this.action = this.actionsApi.getActionDetails(
-      'automerge-change'
-    ) as UIActionInfo;
     this.downstreamConfigMap = {};
     this.getDownstreamConfigMap();
+  }
+
+  onShowRevision() {
+    let actionsApi = this.plugin.changeActions();
+
+    this.action = actionsApi.getActionDetails(
+      'automerge-change'
+    ) as UIActionInfo;
+
     if (this.action) {
-      this.actionsApi.addTapListener(this.action.__key, () => {
+      actionsApi.addTapListener(this.action.__key, () => {
         this.onAutomergeChange();
       });
     }
diff --git a/web/plugin.ts b/web/plugin.ts
index e926a35..03e107b 100644
--- a/web/plugin.ts
+++ b/web/plugin.ts
@@ -24,4 +24,7 @@
   plugin.on(EventType.SHOW_CHANGE, (change: ChangeInfo) =>
     automerger.onShowChange(change)
   );
+  plugin.on(EventType.SHOW_REVISION_ACTIONS, () =>
+    automerger.onShowRevision()
+  );
 });