From 9a715453036038acf40328af80cd02b68bb0e7a0 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Fri, 24 Jun 2016 23:25:11 +0800 Subject: [PATCH] improve piece map performance --- src/scripts/directives/pieceMap.js | 37 ++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/scripts/directives/pieceMap.js b/src/scripts/directives/pieceMap.js index 52f9777..0505c2b 100644 --- a/src/scripts/directives/pieceMap.js +++ b/src/scripts/directives/pieceMap.js @@ -11,19 +11,46 @@ pieceCount: '=' }, link: function (scope, element) { + var pieces = []; + var currentPieceStatus = []; + var redraw = function () { - var pieces = aria2TaskService.getPieceStatus(scope.bitField, scope.pieceCount); + currentPieceStatus = aria2TaskService.getPieceStatus(scope.bitField, scope.pieceCount); + pieces.length = 0; element.empty(); - var pieceCount = Math.max(1, pieces.length); - + var pieceCount = Math.max(1, currentPieceStatus.length); + for (var i = 0; i < pieceCount; i++) { - element.append('
'); + var piece = angular.element('
'); + pieces.push(piece); + element.append(piece); } }; + var refresh = function () { + var newPieceStatus = aria2TaskService.getPieceStatus(scope.bitField, scope.pieceCount); + + if (!currentPieceStatus || !newPieceStatus || currentPieceStatus.length != newPieceStatus.length || newPieceStatus.length != pieces.length) { + redraw(); + return; + } + + for (var i = 0; i < pieces.length; i++) { + if (currentPieceStatus[i] != newPieceStatus[i]) { + if (newPieceStatus[i]) { + pieces[i].addClass('piece-completed'); + } else { + pieces[i].removeClass('piece-completed'); + } + } + } + + currentPieceStatus = newPieceStatus; + }; + scope.$watch('bitField', function () { - redraw(); + refresh(); }); scope.$watch('pieceNumber', function () {