Simon Hunt
Committed by Gerrit Code Review

ONOS-3747: Delayed start refactored into LoadingService.

Change-Id: I07d3c3ffdfe6b207aa21e7b9e470b037a3cffb9b
/*
* Copyright 2015 Open Networking Laboratory
* Copyright 2015,2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -29,14 +29,16 @@
var id = 'loading-anim',
dir = 'data/img/loading/',
pfx = '/load-',
speed = 100;
speed = 100,
waitDelay = 500;
// internal state
var div,
img,
th,
idx,
task;
task,
wait;
function fname(i) {
var z = i > 9 ? '' : '0';
......@@ -50,7 +52,7 @@
}
// start displaying 'loading...' animation (idempotent)
function start() {
function startAnim() {
th = ts.theme();
div = d3.select('#'+id);
if (div.empty()) {
......@@ -62,7 +64,7 @@
}
// stop displaying 'loading...' animation (idempotent)
function stop() {
function stopAnim() {
if (task) {
$timeout.cancel(task);
task = null;
......@@ -70,6 +72,25 @@
d3.select('#'+id).remove();
}
// schedule function to start animation in the future
function start() {
wait = $timeout(startAnim, waitDelay);
}
// cancel future start, if any; stop the animation
function stop() {
if (wait) {
$timeout.cancel(wait);
wait = null;
}
stopAnim();
}
// return true if start() has been called but not stop()
function waiting() {
return !!wait;
}
angular.module('onosLayer')
.factory('LoadingService', ['$log', '$timeout', 'ThemeService',
function (_$log_, _$timeout_, _ts_) {
......@@ -79,7 +100,8 @@
return {
start: start,
stop: stop
stop: stop,
waiting: waiting
};
}]);
......
/*
* Copyright 2015 Open Networking Laboratory
* Copyright 2015,2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -24,8 +24,7 @@
var $log, $interval, $timeout, fs, wss, ls;
// constants
var refreshInterval = 2000,
tardyWait = 500;
var refreshInterval = 2000;
// example params to buildTable:
// {
......@@ -49,8 +48,7 @@
onResp = fs.isF(o.respCb),
idKey = o.idKey || 'id',
oldTableData = [],
refreshPromise,
tardyPromise;
refreshPromise;
o.scope.tableData = [];
o.scope.changedData = [];
......@@ -61,7 +59,6 @@
// === websocket functions --------------------
// response
function respCb(data) {
cancelTardy();
ls.stop();
o.scope.tableData = data[root];
o.scope.annots = data.annots;
......@@ -85,24 +82,12 @@
handlers[resp] = respCb;
wss.bindHandlers(handlers);
// handle "loading..." animation
function scheduleTardy() {
tardyPromise = $timeout(ls.start, tardyWait);
}
function cancelTardy() {
if (tardyPromise) {
$timeout.cancel(tardyPromise);
tardyPromise = null;
}
}
// request
function sortCb(params) {
var p = angular.extend({}, params, o.query);
if (wss.isConnected()) {
wss.sendEvent(req, p);
scheduleTardy();
ls.start();
}
}
o.scope.sortCallback = sortCb;
......@@ -118,7 +103,7 @@
// === autoRefresh functions ------------------
function fetchDataIfNotWaiting() {
if (!tardyPromise) {
if (!ls.waiting()) {
if (fs.debugOn('widget')) {
$log.debug('Refreshing ' + root + ' page');
}
......@@ -147,7 +132,7 @@
o.scope.$on('$destroy', function () {
wss.unbindHandlers(handlers);
stopRefresh();
cancelTardy();
ls.stop();
});
sortCb(o.scope.sortParams);
......