Kaynağa Gözat

Unsubscribe global broadcast listeners

Silvan Engeler 7 yıl önce
ebeveyn
işleme
8148c4ef2f
1 değiştirilmiş dosya ile 15 ekleme ve 6 silme
  1. 15 6
      src/directives/compose_area.ts

+ 15 - 6
src/directives/compose_area.ts

@@ -763,9 +763,6 @@ export default [
                 composeDiv.on('keyup mouseup', updateCaretPosition);
                 composeDiv.on('selectionchange', updateCaretPosition);
 
-                // When switching chat, send stopTyping message
-                scope.$on('$destroy', stopTyping);
-
                 // Handle paste event
                 composeDiv.on('paste', onPaste);
 
@@ -783,15 +780,27 @@ export default [
 
                 updateView();
 
-                $rootScope.$on('onQuoted', (event: ng.IAngularEvent, args: any) => {
+                // Listen to broadcasts
+                const unsubscribeListeners = [];
+                unsubscribeListeners.push($rootScope.$on('onQuoted', (event: ng.IAngularEvent, args: any) => {
                     composeDiv[0].focus();
-                });
-                $rootScope.$on('onMentionSelected', (event: ng.IAngularEvent, args: any) => {
+                }));
+
+                unsubscribeListeners.push($rootScope.$on('onMentionSelected', (event: ng.IAngularEvent, args: any) => {
                     if (args.query && args.mention) {
                         // Insert resulting HTML
                         insertMention(args.mention, caretPosition ? caretPosition.to - args.query.length : null,
                             caretPosition ?  caretPosition.to : null);
                     }
+                }));
+
+                // When switching chat, send stopTyping message
+                scope.$on('$destroy', () => {
+                    unsubscribeListeners.forEach((u) => {
+                        // Unsubscribe
+                        u();
+                    });
+                    stopTyping();
                 });
             },
             // tslint:disable:max-line-length