-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trying to Fix the Duplicate Event Listeners Added on Every AJAX Complete
- Loading branch information
1 parent
64daf76
commit 2b4076c
Showing
13 changed files
with
114 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,67 @@ | ||
(function(win, doc, _) { | ||
|
||
function count(x) { | ||
return Object.keys(x).length; | ||
} | ||
|
||
function isSet(x) { | ||
return 'undefined' !== typeof x; | ||
} | ||
var hooks = {}; | ||
|
||
var hooks = {}; | ||
|
||
_.on = function(name, fn, id) { | ||
if (!isSet(hooks[name])) { | ||
hooks[name] = {}; | ||
} | ||
if (!isSet(id)) { | ||
id = count(hooks[name]); | ||
} | ||
return hooks[name][id] = fn, _; | ||
}; | ||
function isSet(x) { | ||
return 'undefined' !== typeof x; | ||
} | ||
|
||
_.off = function(name, id) { | ||
if (!isSet(name)) { | ||
return hooks = {}, _; | ||
} | ||
if (!isSet(id)) { | ||
return hooks[name] = {}, _; | ||
} | ||
return delete hooks[name][id], _; | ||
}; | ||
_.on = function(name, fn) { | ||
if (!isSet(hooks[name])) { | ||
hooks[name] = []; | ||
} | ||
if (isSet(fn)) { | ||
hooks[name].push(fn); | ||
} | ||
return _; | ||
}; | ||
|
||
_.fire = function(name, lot, id) { | ||
if (!isSet(hooks[name])) { | ||
return _; | ||
} | ||
if (!isSet(id)) { | ||
for (var i in hooks[name]) { | ||
hooks[name][i].apply(_, lot); | ||
_.off = function(name, fn) { | ||
if (!isSet(name)) { | ||
return (hooks = {}), _; | ||
} | ||
if (isSet(hooks[name])) { | ||
if (isSet(fn)) { | ||
for (var i = 0, j = hooks[name].length; i < j; ++i) { | ||
if (fn === hooks[name][i]) { | ||
hooks[name].splice(i, 1); | ||
} | ||
} | ||
} else { | ||
if (isSet(hooks[name][id])) { | ||
hooks[name][id].apply(_, lot); | ||
} | ||
delete hooks[name]; | ||
} | ||
return _; | ||
}; | ||
|
||
var src = doc.currentScript.src, | ||
a = src.split('/'), i, | ||
// `../` | ||
end = a.pop().split('?')[1] || '0'; | ||
|
||
// `../` | ||
a.pop(); | ||
} | ||
return _; | ||
}; | ||
|
||
src = a.join('/'); | ||
_.fire = function(name, lot) { | ||
if (!isSet(hooks[name])) { | ||
return _; | ||
} | ||
for (var i = 0, j = hooks[name].length; i < j; ++i) { | ||
hooks[name][i].apply(_, lot); | ||
} | ||
return _; | ||
}; | ||
|
||
_.folder = src; | ||
_.hooks = hooks; | ||
|
||
_.on('pop', function() { | ||
// ... | ||
doc.addEventListener('load', function() { | ||
_.fire('get'); | ||
}); | ||
|
||
doc.addEventListener('beforeunload', function() { | ||
_.fire('let'); | ||
}); | ||
|
||
doc.addEventListener('DOMContentLoaded', function() { | ||
_.fire('set'); | ||
}); | ||
|
||
_.on('let', function() { | ||
var title = doc.querySelector('title'); | ||
title = title.getAttribute('data-loading-text'); | ||
title && (doc.title = title); | ||
}); | ||
|
||
})(window, document, window._ = window._ || {}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
(function(win, doc, _) { | ||
var timer; | ||
function onChange() { | ||
var bell = doc.querySelector('.js\\:alert'); | ||
if (bell) { | ||
var a = bell.querySelector('a'), | ||
svg = a.querySelector('use'), | ||
icon = svg.getAttribute('href'), | ||
url = a.href; | ||
url = url.replace(/\/::\w+::\/.*$/, '/::f::/affd6ded?token=' + _.token); | ||
url = url.replace(/\/::\w+::\/.*$/, '/::f::/affd6ded?token=' + (_.token || "")); | ||
function count() { | ||
fetch(url).then(function(request) { | ||
return request.text(); | ||
fetch(url).then(function(response) { | ||
return response.text(); | ||
}).then(function(i) { | ||
i = i ? +i : 0; | ||
a.title = i; | ||
svg.setAttribute('href', i ? '#i:e1a64362' : icon); | ||
bell.classList[i ? 'add' : 'remove']('is:active'); | ||
}); | ||
setTimeout(count, 10000); | ||
timer && clearTimeout(timer); | ||
timer = setTimeout(count, 10000); | ||
} | ||
count(); | ||
} | ||
} onChange(); | ||
} | ||
_.on('set', onChange); | ||
_.on('change', onChange); | ||
})(window, document, _); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.