瀏覽代碼

Performance fix: Cache avatar preview

Otherwise `bufferToUrl` would get called repeatedly.
Danilo Bargen 7 年之前
父節點
當前提交
81708991f4
共有 1 個文件被更改,包括 15 次插入4 次删除
  1. 15 4
      src/directives/avatar.ts

+ 15 - 4
src/directives/avatar.ts

@@ -15,6 +15,7 @@
  * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  */
 
+import {bufferToUrl, logAdapter} from '../helpers';
 import {isEchoContact, isGatewayContact} from '../receiver_helpers';
 import {WebClientService} from '../services/webclient';
 import {isContactReceiver} from '../typeguards';
@@ -22,11 +23,11 @@ import {isContactReceiver} from '../typeguards';
 export default [
     '$rootScope',
     '$timeout',
-    '$filter',
+    '$log',
     'WebClientService',
     function($rootScope: ng.IRootScopeService,
              $timeout: ng.ITimeoutService,
-             $filter: ng.IFilterService,
+             $log: ng.ILogService,
              webClientService: WebClientService) {
         return {
             restrict: 'E',
@@ -37,6 +38,8 @@ export default [
             },
             controllerAs: 'ctrl',
             controller: [function() {
+                this.logTag = '[Directives.Avatar]';
+
                 this.highResolution = this.resolution === 'high';
                 this.isLoading = this.highResolution;
                 this.backgroundColor = this.receiver.color;
@@ -76,12 +79,20 @@ export default [
                 /**
                  * Convert avatar bytes to an URI.
                  */
+                let avatarUri = null;
                 this.avatarToUri = (data: ArrayBuffer) => {
                     if (data === null || data === undefined) {
                         return '';
                     }
-                    const filter = $filter('bufferToUrl') as (data: ArrayBuffer, mime: string) => string;
-                    return filter(data, webClientService.appCapabilities.imageFormat.avatar);
+                    if (avatarUri === null) {
+                        // Cache avatar image URI
+                        avatarUri = bufferToUrl(
+                            data,
+                            webClientService.appCapabilities.imageFormat.avatar,
+                            logAdapter($log.warn, this.logTag),
+                        );
+                    }
+                    return avatarUri;
                 };
 
                 /**