"use strict";

(function () {
    angular
        .module("argo")
        .factory("quotesService", quotesService);

    quotesService.$inject = ["accountsService"];
    function quotesService(accountsService) {
        var quotes = {},
            service = {
                getQuotes: getQuotes,
                updateTick: updateTick,
                reset: reset
            };

        return service;

        function getQuotes() {
            return quotes;
        }

        function updateTick(tick) {
            var account = accountsService.getAccount(),
                streamingInstruments = account.streamingInstruments,
                pips = account.pips,
                instrument = tick.instrument;

            quotes[instrument] = {
                time: tick.time,
                ask: tick.ask,
                bid: tick.bid,
                spread: ((tick.ask - tick.bid) / pips[instrument]).toFixed(1)
            };

            orderByInstrument(streamingInstruments);
        }

        function reset() {
            var key;

            for (key in quotes) {
                if (quotes.hasOwnProperty(key)) {
                    delete quotes[key];
                }
            }
        }

        function orderByInstrument(streamingInstruments) {
            if (!angular.equals(streamingInstruments, Object.keys(quotes))) {
                streamingInstruments.forEach(function (key) {
                    var tempKey = key + "__tmp";

                    renameKey(quotes, key, tempKey);
                    renameKey(quotes, tempKey, key);
                });
            }
        }

        function renameKey(o, oldKey, newKey) {
            if (oldKey !== newKey) {
                Object.defineProperty(o, newKey,
                    Object.getOwnPropertyDescriptor(o, oldKey));
                delete o[oldKey];
            }
        }
    }

}());

@@ -932,32 +932,33 @@
             %7D;%0A%0A
+%0A
             orde
@@ -953,33 +953,35 @@
         
-orderByInstrument
+if (!angular.equals
 (streami
@@ -993,18 +993,41 @@
 truments
-);
+, Object.keys(quotes))) %7B
 %0A       
@@ -1023,35 +1023,32 @@
 ) %7B%0A        
-%7D%0A%0A
         
 function res
@@ -1027,32 +1027,61 @@
                 
+streamingInstruments.forEach(
 function reset()
@@ -1077,14 +1077,14 @@
 ion 
-reset(
+(instr
 ) %7B%0A
@@ -1099,52 +1099,31 @@
     
-var key;%0A%0A            for (key in quotes) %7B%0A
+        var temp;%0A%0A    
     
@@ -1160,19 +1160,21 @@
 roperty(
-key
+instr
 )) %7B%0A   
@@ -1194,22 +1194,26 @@
     
-delete
+    temp =
  quotes%5B
 key%5D
@@ -1208,19 +1208,21 @@
  quotes%5B
-key
+instr
 %5D;%0A     
@@ -1232,26 +1232,24 @@
         
-%7D%0A
         
     %7D%0A  
@@ -1244,325 +1244,74 @@
     
-    %7D%0A        %7D%0A%0A        function orderByInstrument(streamingInstruments) %7B%0A            if (!angular.equals(streamingInstruments, Object.keys(quotes))) %7B%0A                streamingInstruments.forEach(function (key) %7B%0A                    var tempKey = key + %22__tmp%22;%0A%0A                    renameKey(quotes, key,
+delete quotes%5Binstr%5D;%0A                        quotes%5Binstr%5D =
  temp
-Key)
 ;%0A  
@@ -1332,40 +1332,9 @@
     
-renameKey(quotes, tempKey, key);
+%7D
 %0A   
@@ -1398,186 +1398,160 @@
 n re
-nameKey(o, oldKey, newKey) %7B%0A            if (oldKey !== newKey) %7B%0A                Object.defineProperty(o, newKey,%0A                    Object.getOwnPropertyDescriptor(o, oldKey))
+set() %7B%0A            var key;%0A%0A            for (key in quotes) %7B%0A                if (quotes.hasOwnProperty(key)) %7B%0A                    delete quotes%5Bkey%5D
 ;%0A  
@@ -1568,25 +1568,9 @@
     
-delete o%5BoldKey%5D;
+%7D
 %0A   
@@ -1586,24 +1586,25 @@
 %7D%0A        %7D%0A
+%0A
     %7D%0A%0A%7D());

improve instruments list reorder