From 498a471c342c9ec07a56deac405022b0875a78cf Mon Sep 17 00:00:00 2001 From: Curtis Maloney Date: Wed, 24 May 2017 21:57:43 +1000 Subject: [PATCH 1/2] Make JS better performant --- notifications/static/notifications/notify.js | 32 ++++++++------------ 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/notifications/static/notifications/notify.js b/notifications/static/notifications/notify.js index 7e464a5..6b786f9 100644 --- a/notifications/static/notifications/notify.js +++ b/notifications/static/notifications/notify.js @@ -18,11 +18,9 @@ function fill_notification_badge(data) { function fill_notification_list(data) { var menu = document.getElementById(notify_menu_id); if (menu) { - menu.innerHTML = ""; - for (var i=0; i < data.unread_list.length; i++) { - var item = data.unread_list[i]; - console.log(item) - var message = "" + var content = []; + while(var item = data.unread_list.shift()) { + var message = ""; if(typeof item.actor !== 'undefined'){ message = item.actor; } @@ -35,9 +33,9 @@ function fill_notification_list(data) { if(typeof item.timestamp !== 'undefined'){ message = message + " " + item.timestamp; } - - menu.innerHTML = menu.innerHTML + "
  • "+ message + "
  • "; + content.append('
  • ' + message + '
  • '); } + menu.innerHTML = content.join(''); } } @@ -50,17 +48,13 @@ function fetch_api_data() { //only fetch data if a function is setup var r = new XMLHttpRequest(); r.open("GET", notify_api_url+'?max='+notify_fetch_count, true); - r.onreadystatechange = function () { - if (r.readyState != 4 || r.status != 200) { - consecutive_misfires++; - } - else { - consecutive_misfires = 0; - for (var i=0; i < registered_functions.length; i++) { - var func = registered_functions[i]; - func(JSON.parse(r.responseText)); - } - } + r.onerror = function () { + consecutive_misfires++; + } + r.onready = function () { + consecutive_misfires = 0; + var data = JSON.parse(r.responseText); + registered_functions.forEach(function (func) { func(data); }); } r.send(); } @@ -75,4 +69,4 @@ function fetch_api_data() { } } -setTimeout(fetch_api_data,1000); \ No newline at end of file +setTimeout(fetch_api_data, 1000); From ef12471f653912f9365c7ea1c02bf5c0cc569baf Mon Sep 17 00:00:00 2001 From: Curtis Maloney Date: Wed, 24 May 2017 22:57:46 +1000 Subject: [PATCH 2/2] Use map for building menu list --- notifications/static/notifications/notify.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/notifications/static/notifications/notify.js b/notifications/static/notifications/notify.js index 6b786f9..df21a25 100644 --- a/notifications/static/notifications/notify.js +++ b/notifications/static/notifications/notify.js @@ -19,7 +19,7 @@ function fill_notification_list(data) { var menu = document.getElementById(notify_menu_id); if (menu) { var content = []; - while(var item = data.unread_list.shift()) { + menu.innerHTML = data.unread_list.map(function (item) { var message = ""; if(typeof item.actor !== 'undefined'){ message = item.actor; @@ -33,9 +33,8 @@ function fill_notification_list(data) { if(typeof item.timestamp !== 'undefined'){ message = message + " " + item.timestamp; } - content.append('
  • ' + message + '
  • '); - } - menu.innerHTML = content.join(''); + return '
  • ' + message + '
  • '; + }).join('') } }