From 31e409f36147d857b8c5bca0fe76f38fe2950f1e Mon Sep 17 00:00:00 2001 From: Zixaphir Date: Tue, 7 Jan 2014 14:18:22 -0700 Subject: [PATCH] I get asked a lot, "do you even test your code?" No. No, the answer is no. --- builds/4chan-X.user.js | 46 ++++++++++++--------------------- builds/crx/script.js | 46 ++++++++++++--------------------- src/General/Main.coffee | 32 +++++++---------------- src/General/lib/callbacks.class | 10 +++---- 4 files changed, 49 insertions(+), 85 deletions(-) diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index 2ad2d02a4..073057b82 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -813,7 +813,7 @@ Callbacks.prototype.push = function(_arg) { var cb, name; name = _arg.name, cb = _arg.cb; - return this.name = cb; + return this[name] = cb; }; Callbacks.prototype.clean = function() { @@ -826,16 +826,15 @@ }; Callbacks.prototype.rm = function(name) { - return delete this.name; + return delete this[name]; }; - Callbacks.prototype.execute = function(target) { - var cb, err, errors, name; + Callbacks.prototype.execute = function(node) { + var err, errors, name; for (name in this) { - cb = this[name]; if (this.hasOwnProperty(name)) { try { - cb.call(target); + this[name].call(node); } catch (_error) { err = _error; if (!errors) { @@ -12571,43 +12570,32 @@ } }, callbackNodes: function(klass, nodes) { - var len, node; - len = nodes.length; + var cb, i, node; + i = 0; + cb = klass.callbacks; while (node = nodes[i++]) { - klass.callback.execute(node); + cb.execute(node); } }, callbackNodesDB: function(klass, nodes, cb) { - var errors, func, i, len, node, queue, softTask; - queue = []; + var callbacks, errors, len, softTask; errors = null; - func = function(node) { - klass.callback.execute(node); - if (!queue.length) { - if (cb) { - return cb(); - } - } - }; + len = 0; + callbacks = klass.callbacks; softTask = function() { var node; - node = queue.shift(); - func(node); - if (!queue.length) { - return; + node = nodes.shift(); + callbacks.execute(node); + if (!--len && cb) { + return cb(); } - if (!(queue.length % 7)) { + if (!(len % 7)) { return setTimeout(softTask, 0); } else { return softTask(); } }; len = nodes.length; - i = 0; - while (i < len) { - node = nodes[i++]; - queue.push(node); - } return softTask(); }, addCallback: function(e) { diff --git a/builds/crx/script.js b/builds/crx/script.js index a6551d072..4fb51c702 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -818,7 +818,7 @@ Callbacks.prototype.push = function(_arg) { var cb, name; name = _arg.name, cb = _arg.cb; - return this.name = cb; + return this[name] = cb; }; Callbacks.prototype.clean = function() { @@ -831,16 +831,15 @@ }; Callbacks.prototype.rm = function(name) { - return delete this.name; + return delete this[name]; }; - Callbacks.prototype.execute = function(target) { - var cb, err, errors, name; + Callbacks.prototype.execute = function(node) { + var err, errors, name; for (name in this) { - cb = this[name]; if (this.hasOwnProperty(name)) { try { - cb.call(target); + this[name].call(node); } catch (_error) { err = _error; if (!errors) { @@ -12545,43 +12544,32 @@ } }, callbackNodes: function(klass, nodes) { - var len, node; - len = nodes.length; + var cb, i, node; + i = 0; + cb = klass.callbacks; while (node = nodes[i++]) { - klass.callback.execute(node); + cb.execute(node); } }, callbackNodesDB: function(klass, nodes, cb) { - var errors, func, i, len, node, queue, softTask; - queue = []; + var callbacks, errors, len, softTask; errors = null; - func = function(node) { - klass.callback.execute(node); - if (!queue.length) { - if (cb) { - return cb(); - } - } - }; + len = 0; + callbacks = klass.callbacks; softTask = function() { var node; - node = queue.shift(); - func(node); - if (!queue.length) { - return; + node = nodes.shift(); + callbacks.execute(node); + if (!--len && cb) { + return cb(); } - if (!(queue.length % 7)) { + if (!(len % 7)) { return setTimeout(softTask, 0); } else { return softTask(); } }; len = nodes.length; - i = 0; - while (i < len) { - node = nodes[i++]; - queue.push(node); - } return softTask(); }, addCallback: function(e) { diff --git a/src/General/Main.coffee b/src/General/Main.coffee index 3148ef4a7..fcf36a7f7 100755 --- a/src/General/Main.coffee +++ b/src/General/Main.coffee @@ -229,40 +229,28 @@ Main = new Notice 'warning', 'Cookies need to be enabled on 4chan for <%= meta.name %> to operate properly.', 30 callbackNodes: (klass, nodes) -> - # get the nodes' length only once - len = nodes.length + i = 0 + cb = klass.callbacks while node = nodes[i++] - klass.callback.execute node + cb.execute node return callbackNodesDB: (klass, nodes, cb) -> - queue = [] errors = null + len = 0 - func = (node) -> - klass.callback.execute node - - # finish - unless queue.length - cb() if cb + {callbacks} = klass softTask = -> - node = queue.shift() - func node - return unless queue.length - unless queue.length % 7 + node = nodes.shift() + callbacks.execute node + return cb() if not --len and cb + unless len % 7 setTimeout softTask, 0 else softTask() - # get the nodes' length only once - len = nodes.length - i = 0 - - while i < len - node = nodes[i++] - queue.push node - + len = nodes.length softTask() addCallback: (e) -> diff --git a/src/General/lib/callbacks.class b/src/General/lib/callbacks.class index ef425fce5..7a832f43c 100644 --- a/src/General/lib/callbacks.class +++ b/src/General/lib/callbacks.class @@ -1,16 +1,16 @@ class Callbacks - push: ({name, cb}) -> @name = cb + push: ({name, cb}) -> @[name] = cb clean: -> @rm name for name of @ when @hasOwnProperty name return - rm: (name) -> delete @name + rm: (name) -> delete @[name] - execute: (target) -> - for name, cb of @ when @hasOwnProperty name + execute: (node) -> + for name of @ when @hasOwnProperty name try - cb.call target + @[name].call node catch err errors = [] unless errors errors.push