This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-rocketbot-ui.git


The following commit(s) were added to refs/heads/master by this push:
     new 48cbc76  update event-bus type (#231)
48cbc76 is described below

commit 48cbc76a96cf6276800fa2480a4a1a809c992afe
Author: leemove <[email protected]>
AuthorDate: Thu Dec 12 18:36:57 2019 +0800

    update event-bus type (#231)
    
    * Update: update type of Vue.$eventBus
    
    * Update: update event-bus type
---
 src/event-bus.ts   | 31 ++++++++++++++++++-------------
 src/types/vue.d.ts |  3 ++-
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/src/event-bus.ts b/src/event-bus.ts
index 7c6b7ef..a8e0068 100644
--- a/src/event-bus.ts
+++ b/src/event-bus.ts
@@ -1,7 +1,8 @@
-import Vue from 'vue';
+import Vue, { VueConstructor  } from 'vue';
 
 type VueComponentVM = Vue & { _uid: string; };
 
+
 interface Handles {
   [key: string]: any[];
 }
@@ -11,14 +12,14 @@ export class EventBus {
   private readonly eventMapUid: any;
   private handles!: Handles;
 
-  constructor(vue: Vue) {
+  constructor(vue: VueConstructor) {
     if (!this.handles) {
       Object.defineProperty(this, 'handles', {
         value: {},
         enumerable: false,
       });
     }
-    this.Vue = vue;
+    this.Vue = Vue;
     // _uid and event name map
     this.eventMapUid = {};
   }
@@ -62,6 +63,17 @@ export class EventBus {
     delete this.handles[eventName];
   }
 
+  /**
+   * eventBus.$offVmEvent.
+   * @param uid uid of VueComponentVM
+   */
+  public $offVmEvent(uid: string) {
+    const currentEvents = this.eventMapUid[uid] || [];
+    currentEvents.forEach((event: any) => {
+      this.$off(event);
+    });
+  }
+
   private setEventMapUid(uid: string, eventName: string) {
     if (!this.eventMapUid[uid]) {
       this.eventMapUid[uid] = [];
@@ -69,24 +81,17 @@ export class EventBus {
     // Push the name of each _uid subscription to the array to which the 
respective uid belongs.
     this.eventMapUid[uid].push(eventName);
   }
-
-  private $offVmEvent(uid: string) {
-    const currentEvents = this.eventMapUid[uid] || [];
-    currentEvents.forEach((event: any) => {
-      this.$off(event);
-    });
-  }
 }
 
 const $EventBus = {
-  install: (vue: any) => {
+  install: (vue: VueConstructor) => {
     vue.prototype.$eventBus = new EventBus(vue);
     vue.mixin({
       deactivated() {
-        this.$eventBus.$offVmEvent(this._uid);
+        (this as VueComponentVM).$eventBus.$offVmEvent((this as 
VueComponentVM)._uid);
       },
       beforeDestroy() {
-        this.$eventBus.$offVmEvent(this._uid);
+        (this as VueComponentVM).$eventBus.$offVmEvent((this as 
VueComponentVM)._uid);
       },
     });
   },
diff --git a/src/types/vue.d.ts b/src/types/vue.d.ts
index 579e711..136de3e 100644
--- a/src/types/vue.d.ts
+++ b/src/types/vue.d.ts
@@ -1,7 +1,8 @@
 import Vue from 'vue';
+import { EventBus } from '@/event-bus';
 
 declare module 'vue/types/vue' {
   interface Vue {
-    $eventBus: any;
+    $eventBus: EventBus;
   }
 }

Reply via email to