Skip to content

Node.js based forum software built for the modern web

added a way to track socket listners added to the page to prevent memory leaks and duplicated definitions of events

psychobunnye5a6d1bf8b6d
diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js
index 59263bbd70..971f9aef71 100644
--- a/public/src/ajaxify.js
+++ b/public/src/ajaxify.js
@@ -10,6 +10,16 @@ var ajaxify = {};
var current_state = null;
var executed = {};
+ var events = [];
+ ajaxify.register_events = function(new_page_events) {
+ for (var i = 0, ii = events.length; i<ii; i++) {
+ socket.removeAllListeners(events[i]); // optimize this to user removeListener(event, listener) instead.
+ }
+
+ events = new_page_events;
+ };
+
+
ajaxify.go = function(url, callback) {
var url = url.replace(/\/$/, "");
var tpl_url = (url === '') ? 'home' : url;
@@ -23,10 +33,7 @@ var ajaxify = {};
jQuery('#content, #footer').fadeOut(150, function() {
//content.innerHTML = templates[tpl_url];
load_template(function() {
- if (executed[url] !== true) {
- exec_body_scripts(content);
- executed[url] = true;
- }
+ exec_body_scripts(content);
ajaxify.enable();
if (callback) {
diff --git a/public/templates/login.tpl b/public/templates/login.tpl
index 52dd03745b..b73f8749b7 100644
--- a/public/templates/login.tpl
+++ b/public/templates/login.tpl
@@ -23,6 +23,7 @@
});
};
+ ajaxify.register_events(['user.login']);
socket.on('user.login', function(data) {
console.log(data);
if (data.status === 0) {
diff --git a/public/templates/register.tpl b/public/templates/register.tpl
index 5a8c38766b..60988336d3 100644
--- a/public/templates/register.tpl
+++ b/public/templates/register.tpl
@@ -36,6 +36,11 @@
socket.emit('user.email.exists', { email: emailEl.value });
}, false);
+
+
+
+ ajaxify.register_events(['user.create', 'user.exists', 'user.email.exists']);
+
socket.on('user.create', function(data) {
//console.log('user create: ' + data.status);
ajaxify.go('/');
diff --git a/public/templates/reset.tpl b/public/templates/reset.tpl
index 17c974c883..3dee8b302b 100644
--- a/public/templates/reset.tpl
+++ b/public/templates/reset.tpl
@@ -29,6 +29,8 @@
}
};
+ ajaxify.register_events(['user.send_reset']);
+
socket.on('user.send_reset', function(data) {
var submitEl = document.getElementById('reset');
diff --git a/public/templates/reset_code.tpl b/public/templates/reset_code.tpl
index 90115baaa0..42a7987095 100644
--- a/public/templates/reset_code.tpl
+++ b/public/templates/reset_code.tpl
@@ -39,6 +39,9 @@
// Enable the form if the code is valid
socket.emit('user:reset.valid', { code: '{reset_code}' });
+
+
+ ajaxify.register_events(['user:reset.valid', 'user:reset.commit']);
socket.on('user:reset.valid', function(data) {
if (!!data.valid) resetEl.disabled = false;
else {