From 2751811e56129b14a423e64a6d638ad466268202 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Mon, 28 Sep 2015 18:35:32 +0000 Subject: Name and comment addEventListener functions Using anonymous functions with event handlers is painfully slow according to benchmarks. Anonymous functions are up to 99% slower. It is also illegal to use anonymous functions in this way according to Airbnb ES5 style guide. Fixes some ESLint func-names errors using Airbnb ES5 config. Reference: https://jsperf.com/event-handler-vs-anonymous-function/3 --- js/cheesesteak.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'js') diff --git a/js/cheesesteak.js b/js/cheesesteak.js index ea3dc19..4b00c3e 100644 --- a/js/cheesesteak.js +++ b/js/cheesesteak.js @@ -152,13 +152,15 @@ var xhr = new XMLHttpRequest(); xhr.open(opts.method, this.url, true); - xhr.upload.addEventListener('loadstart', function(e) { + + function initProgressBar(e) { for (var i = 0, l = files.length; i < l; i++) { files[i].uploadedSize = 0; } - }); + } + xhr.upload.addEventListener('loadstart', initProgressBar); - xhr.upload.addEventListener('progress', function(e) { + function updateProgressBar(e) { if (e.lengthComputable) { size = e.loaded; @@ -181,23 +183,31 @@ } _this.emit('uploadprogress', e, files); - }, false); + } + xhr.upload.addEventListener('progress', updateProgressBar, false); - xhr.upload.addEventListener('loadstart', function(e) { + function startUpload(e) { _this.emit('uploadstart', e); - }); + } + xhr.upload.addEventListener('loadstart', startUpload); - xhr.upload.addEventListener('load', function(e) { + // The upload is complete, now tell the user to wait for URLs. + function postUpload(e) { _this.emit('uploadcomplete', e); - }); + } + xhr.upload.addEventListener('load', postUpload); - xhr.addEventListener('progress', function(e) { + // Tell the browser the upload completed and wait for response. + function postProgress(e) { _this.emit('progress', e); - }); + } + xhr.upload.addEventListener('progress', postProgress); - xhr.addEventListener('load', function(e) { + // Send a success/error response. Nothing more to do. + function uploadFinished(e) { _this.emit('load', e, xhr.responseText); - }); + } + xhr.upload.addEventListener('load', uploadFinished); xhr.send(data); -- cgit v1.2.3 From 85aec0691ab19aa1f9dcb501c77497fc21765c6c Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Mon, 28 Sep 2015 18:42:04 +0000 Subject: Remove dead uploadstart emitter startUpload() The loadstart even is only triggered once to indicate the progress has begun (initialize the progress bar). Essentially, this is dead code for two reasons: 1. It never gets emitted because initProgressBar() gets emitted first. 2. No JavaScript file in pomf core uses the non-standard uploadstart event. --- js/cheesesteak.js | 5 ----- 1 file changed, 5 deletions(-) (limited to 'js') diff --git a/js/cheesesteak.js b/js/cheesesteak.js index 4b00c3e..31d6086 100644 --- a/js/cheesesteak.js +++ b/js/cheesesteak.js @@ -186,11 +186,6 @@ } xhr.upload.addEventListener('progress', updateProgressBar, false); - function startUpload(e) { - _this.emit('uploadstart', e); - } - xhr.upload.addEventListener('loadstart', startUpload); - // The upload is complete, now tell the user to wait for URLs. function postUpload(e) { _this.emit('uploadcomplete', e); -- cgit v1.2.3 From d3f9e9a2929301b19a69ade53a6051859883f7f8 Mon Sep 17 00:00:00 2001 From: Juuso Lapinlampi Date: Wed, 7 Oct 2015 16:11:23 +0000 Subject: Fix incorrect progress/load event listeners This is a symptom of copy-paste code. A file would be uploaded, but it would return the "Something went wrong" error message without telling why. Me, ewhal@ and bursey on IRC tried had a look to see what was wrong, but none of us could catch this subtle bug. It wasn't until alchimist@ (cenci0) looked at this and found the issue. Thanks. This bug was introduced in commit 2751811. --- js/cheesesteak.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'js') diff --git a/js/cheesesteak.js b/js/cheesesteak.js index 31d6086..e090a12 100644 --- a/js/cheesesteak.js +++ b/js/cheesesteak.js @@ -196,13 +196,13 @@ function postProgress(e) { _this.emit('progress', e); } - xhr.upload.addEventListener('progress', postProgress); + xhr.addEventListener('progress', postProgress); // Send a success/error response. Nothing more to do. function uploadFinished(e) { _this.emit('load', e, xhr.responseText); } - xhr.upload.addEventListener('load', uploadFinished); + xhr.addEventListener('load', uploadFinished); xhr.send(data); -- cgit v1.2.3