Explorar o código

Optimize dndModeSimplified filter

Initially, the dndModeSimplified would get called 100'000 times with my
device just by loading the page once.

This commit replaces the filter with a controller method and uses
ng-init to only calculate the dnd mode once per conversation.
Danilo Bargen %!s(int64=7) %!d(string=hai) anos
pai
achega
7b02df8422

+ 0 - 16
src/filters.ts

@@ -469,22 +469,6 @@ angular.module('3ema.filters', [])
     };
 }])
 
-/**
- * Return a simplified DND mode.
- *
- * This will return either 'on', 'off' or 'mention'.
- * The 'until' mode will be processed depending on the expiration timestamp.
- */
-.filter('dndModeSimplified', ['NotificationService', function(notificationService: NotificationService) {
-    return (conversation: threema.Conversation) => {
-        const simplified = notificationService.getAppNotificationSettings(conversation);
-        if (simplified.dnd.enabled) {
-            return simplified.dnd.mentionOnly ? 'mention' : 'on';
-        }
-        return 'off';
-    };
-}])
-
 /**
  * Mark data as trusted.
  */

+ 4 - 3
src/partials/messenger.navigation.html

@@ -67,6 +67,7 @@
     <p class="empty" ng-if="ctrl.conversations().length === 0" translate>messenger.NO_CONVERSATIONS_FOUND</p>
     <ul>
         <li ng-repeat="conversation in ctrl.conversations() | filter:ctrl.searchConversation"
+            ng-init="dndModeSimplified = ctrl.dndModeSimplified(conversation)"
             ui-sref="messenger.home.conversation({ type: conversation.type, id: conversation.id, initParams: null })"
             class="conversation-wrapper" ng-if="ctrl.isVisible(conversation)">
 
@@ -84,13 +85,13 @@
                     <section class="receiver-box">
                         <span class="title" ng-class="{'disabled': conversation.receiver.disabled === true}" ng-bind-html="conversation.receiver.displayName | escapeHtml | emojify">
                         </span>
-                        <span class="notification-settings" ng-show="(conversation | dndModeSimplified) === 'on'">
+                        <span class="notification-settings" ng-if="dndModeSimplified === 'on'">
                             <img height="16" width="16" src="img/ic_dnd_total_silence.svg" translate translate-attr-title="messenger.MUTED_NONE">
                         </span>
-                        <span class="notification-settings" ng-show="(conversation | dndModeSimplified) === 'mention'">
+                        <span class="notification-settings" ng-if="dndModeSimplified === 'mention'">
                             <img height="16" width="16" src="img/ic_dnd_mention.svg" translate translate-attr-title="messenger.MUTED_MENTION_ONLY">
                         </span>
-                        <span class="notification-settings" ng-show="(conversation | dndModeSimplified) === 'off' && conversation.notifications && conversation.notifications.sound.mode === 'muted'">
+                        <span class="notification-settings" ng-if="dndModeSimplified === 'off' && conversation.notifications && conversation.notifications.sound.mode === 'muted'">
                             <img height="16" width="16" src="img/ic_notifications_off.svg" translate translate-attr-title="messenger.MUTED_SILENT">
                         </span>
                         <span class="badge unread-count" ng-show="conversation.unreadCount > 0">

+ 18 - 2
src/partials/messenger.ts

@@ -848,6 +848,7 @@ class NavigationController {
     private receiverService: ReceiverService;
     private stateService: StateService;
     private trustedKeyStoreService: TrustedKeyStoreService;
+    private notificationService: NotificationService;
 
     private activeTab: 'contacts' | 'conversations' = 'conversations';
     private searchVisible = false;
@@ -859,13 +860,13 @@ class NavigationController {
 
     public static $inject = [
         '$log', '$state', '$mdDialog', '$translate',
-        'WebClientService', 'StateService', 'ReceiverService', 'TrustedKeyStore',
+        'WebClientService', 'StateService', 'ReceiverService', 'NotificationService', 'TrustedKeyStore',
     ];
 
     constructor($log: ng.ILogService, $state: UiStateService,
                 $mdDialog: ng.material.IDialogService, $translate: ng.translate.ITranslateService,
                 webClientService: WebClientService, stateService: StateService,
-                receiverService: ReceiverService,
+                receiverService: ReceiverService, notificationService: NotificationService,
                 trustedKeyStoreService: TrustedKeyStoreService) {
 
         // Redirect to welcome if necessary
@@ -879,6 +880,7 @@ class NavigationController {
         this.receiverService = receiverService;
         this.stateService = stateService;
         this.trustedKeyStoreService = trustedKeyStoreService;
+        this.notificationService = notificationService;
         this.$mdDialog = $mdDialog;
         this.$translate = $translate;
         this.$state = $state;
@@ -1070,6 +1072,20 @@ class NavigationController {
         return this.webClientService.appCapabilities.distributionLists;
     }
 
+    /**
+     * Return a simplified DND mode.
+     *
+     * This will return either 'on', 'off' or 'mention'.
+     * The 'until' mode will be processed depending on the expiration timestamp.
+     */
+    public dndModeSimplified(conversation: threema.Conversation): 'on' | 'mention' | 'off' {
+        const simplified = this.notificationService.getAppNotificationSettings(conversation);
+        if (simplified.dnd.enabled) {
+            return simplified.dnd.mentionOnly ? 'mention' : 'on';
+        }
+        return 'off';
+    }
+
 }
 
 class MessengerController {

+ 1 - 0
src/services/webclient.ts

@@ -3084,6 +3084,7 @@ export class WebClientService {
 
     /**
      * Receive a new incoming decrypted message.
+     * This method runs inside the digest loop.
      */
     private receive(message: threema.WireMessage): void {
         // Dispatch message