Simon Hunt
Committed by Gerrit Code Review

ONOS-3747: Delayed start refactored into LoadingService.

Change-Id: I07d3c3ffdfe6b207aa21e7b9e470b037a3cffb9b
1 /* 1 /*
2 - * Copyright 2015 Open Networking Laboratory 2 + * Copyright 2015,2016 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -29,14 +29,16 @@ ...@@ -29,14 +29,16 @@
29 var id = 'loading-anim', 29 var id = 'loading-anim',
30 dir = 'data/img/loading/', 30 dir = 'data/img/loading/',
31 pfx = '/load-', 31 pfx = '/load-',
32 - speed = 100; 32 + speed = 100,
33 + waitDelay = 500;
33 34
34 // internal state 35 // internal state
35 var div, 36 var div,
36 img, 37 img,
37 th, 38 th,
38 idx, 39 idx,
39 - task; 40 + task,
41 + wait;
40 42
41 function fname(i) { 43 function fname(i) {
42 var z = i > 9 ? '' : '0'; 44 var z = i > 9 ? '' : '0';
...@@ -50,7 +52,7 @@ ...@@ -50,7 +52,7 @@
50 } 52 }
51 53
52 // start displaying 'loading...' animation (idempotent) 54 // start displaying 'loading...' animation (idempotent)
53 - function start() { 55 + function startAnim() {
54 th = ts.theme(); 56 th = ts.theme();
55 div = d3.select('#'+id); 57 div = d3.select('#'+id);
56 if (div.empty()) { 58 if (div.empty()) {
...@@ -62,7 +64,7 @@ ...@@ -62,7 +64,7 @@
62 } 64 }
63 65
64 // stop displaying 'loading...' animation (idempotent) 66 // stop displaying 'loading...' animation (idempotent)
65 - function stop() { 67 + function stopAnim() {
66 if (task) { 68 if (task) {
67 $timeout.cancel(task); 69 $timeout.cancel(task);
68 task = null; 70 task = null;
...@@ -70,6 +72,25 @@ ...@@ -70,6 +72,25 @@
70 d3.select('#'+id).remove(); 72 d3.select('#'+id).remove();
71 } 73 }
72 74
75 + // schedule function to start animation in the future
76 + function start() {
77 + wait = $timeout(startAnim, waitDelay);
78 + }
79 +
80 + // cancel future start, if any; stop the animation
81 + function stop() {
82 + if (wait) {
83 + $timeout.cancel(wait);
84 + wait = null;
85 + }
86 + stopAnim();
87 + }
88 +
89 + // return true if start() has been called but not stop()
90 + function waiting() {
91 + return !!wait;
92 + }
93 +
73 angular.module('onosLayer') 94 angular.module('onosLayer')
74 .factory('LoadingService', ['$log', '$timeout', 'ThemeService', 95 .factory('LoadingService', ['$log', '$timeout', 'ThemeService',
75 function (_$log_, _$timeout_, _ts_) { 96 function (_$log_, _$timeout_, _ts_) {
...@@ -79,7 +100,8 @@ ...@@ -79,7 +100,8 @@
79 100
80 return { 101 return {
81 start: start, 102 start: start,
82 - stop: stop 103 + stop: stop,
104 + waiting: waiting
83 }; 105 };
84 }]); 106 }]);
85 107
......
1 /* 1 /*
2 - * Copyright 2015 Open Networking Laboratory 2 + * Copyright 2015,2016 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -24,8 +24,7 @@ ...@@ -24,8 +24,7 @@
24 var $log, $interval, $timeout, fs, wss, ls; 24 var $log, $interval, $timeout, fs, wss, ls;
25 25
26 // constants 26 // constants
27 - var refreshInterval = 2000, 27 + var refreshInterval = 2000;
28 - tardyWait = 500;
29 28
30 // example params to buildTable: 29 // example params to buildTable:
31 // { 30 // {
...@@ -49,8 +48,7 @@ ...@@ -49,8 +48,7 @@
49 onResp = fs.isF(o.respCb), 48 onResp = fs.isF(o.respCb),
50 idKey = o.idKey || 'id', 49 idKey = o.idKey || 'id',
51 oldTableData = [], 50 oldTableData = [],
52 - refreshPromise, 51 + refreshPromise;
53 - tardyPromise;
54 52
55 o.scope.tableData = []; 53 o.scope.tableData = [];
56 o.scope.changedData = []; 54 o.scope.changedData = [];
...@@ -61,7 +59,6 @@ ...@@ -61,7 +59,6 @@
61 // === websocket functions -------------------- 59 // === websocket functions --------------------
62 // response 60 // response
63 function respCb(data) { 61 function respCb(data) {
64 - cancelTardy();
65 ls.stop(); 62 ls.stop();
66 o.scope.tableData = data[root]; 63 o.scope.tableData = data[root];
67 o.scope.annots = data.annots; 64 o.scope.annots = data.annots;
...@@ -85,24 +82,12 @@ ...@@ -85,24 +82,12 @@
85 handlers[resp] = respCb; 82 handlers[resp] = respCb;
86 wss.bindHandlers(handlers); 83 wss.bindHandlers(handlers);
87 84
88 - // handle "loading..." animation
89 - function scheduleTardy() {
90 - tardyPromise = $timeout(ls.start, tardyWait);
91 - }
92 -
93 - function cancelTardy() {
94 - if (tardyPromise) {
95 - $timeout.cancel(tardyPromise);
96 - tardyPromise = null;
97 - }
98 - }
99 -
100 // request 85 // request
101 function sortCb(params) { 86 function sortCb(params) {
102 var p = angular.extend({}, params, o.query); 87 var p = angular.extend({}, params, o.query);
103 if (wss.isConnected()) { 88 if (wss.isConnected()) {
104 wss.sendEvent(req, p); 89 wss.sendEvent(req, p);
105 - scheduleTardy(); 90 + ls.start();
106 } 91 }
107 } 92 }
108 o.scope.sortCallback = sortCb; 93 o.scope.sortCallback = sortCb;
...@@ -118,7 +103,7 @@ ...@@ -118,7 +103,7 @@
118 103
119 // === autoRefresh functions ------------------ 104 // === autoRefresh functions ------------------
120 function fetchDataIfNotWaiting() { 105 function fetchDataIfNotWaiting() {
121 - if (!tardyPromise) { 106 + if (!ls.waiting()) {
122 if (fs.debugOn('widget')) { 107 if (fs.debugOn('widget')) {
123 $log.debug('Refreshing ' + root + ' page'); 108 $log.debug('Refreshing ' + root + ' page');
124 } 109 }
...@@ -147,7 +132,7 @@ ...@@ -147,7 +132,7 @@
147 o.scope.$on('$destroy', function () { 132 o.scope.$on('$destroy', function () {
148 wss.unbindHandlers(handlers); 133 wss.unbindHandlers(handlers);
149 stopRefresh(); 134 stopRefresh();
150 - cancelTardy(); 135 + ls.stop();
151 }); 136 });
152 137
153 sortCb(o.scope.sortParams); 138 sortCb(o.scope.sortParams);
......