Skip to content

Commit

Permalink
going away from tracking the individual listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
max.shirshin committed May 2, 2013
1 parent b1db77b commit 643d41f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.idea
54 changes: 15 additions & 39 deletions extra-tools/jquery.update.js
Expand Up @@ -11,7 +11,6 @@

// keys for .data() to associate some internal properties with
valueCache = 'update:prev',
isTracked = 'update:istracked',

timeoutId,
cacheTimeoutId,
Expand Down Expand Up @@ -70,8 +69,8 @@
var cachedVal = $.data(el, valueCache),
val = cfg.valFn(el);

if (cachedVal === undefined) {
// first run, cache value
if (cachedVal === undefined || cfg.eqFn(cachedVal, val)) {
// cache value
$.data(el, valueCache, val);
} else if (! cfg.eqFn(cachedVal, val)) {
$(el)
Expand All @@ -81,20 +80,17 @@
}

function refreshCache() {
// empty the polling pool
$pool = $();
// filter out dead elements
$pool = $pool.filter(function() {
// elements removed from DOM shouldn't be polled anymore
return $.contains(document.documentElement, this);
});

$source = $source.filter(function() {
// elements removed from DOM shouldn't be polled anymore
return $.contains(document.documentElement, this);
});

$source.each(function() {
addToPolling(this);
});
$source = $source
.filter(function() {
// elements removed from DOM shouldn't be polled anymore
return $.contains(document.documentElement, this);
})
.each(function() {
addToPolling(this);
});

// schedule next run
cacheTimeoutId = setTimeout(refreshCache, cfg.cacheTimeout);
Expand All @@ -111,13 +107,6 @@
$pool = $pool.add($getPolled(el));
}

function increaseListenerCount(el) {
$getPolled(el).each(function() {
var trackedNum = $.data(this, isTracked) || 0;
$.data(this, isTracked, trackedNum + 1);
});
}

// see which elements are being polled for this element.
// it can either be the element itself or its descendants (if event delegation was used)
function $getPolled(el) {
Expand Down Expand Up @@ -163,34 +152,21 @@
},

add: function() {
increaseListenerCount(this);
// start polling
timeoutId || poll();
// start refreshing cache periodically
cacheTimeoutId || refreshCache();
},

remove: function() {
var $trackedElems = $getPolled(this);

// for each polled element, decrease the number of listeners interested in its polling
$trackedElems.each(function() {
var currentTrackedNum = $.data(this, isTracked) || 0;
$.data(this, isTracked, Math.max(currentTrackedNum - 1, 0));
});

$pool = $pool.filter(function() {
// element must stay if there is at least one listener interested
return $.data(this, isTracked) > 0;
});
clearTimeout(cacheTimeoutId);
refreshCache();
},

teardown: function() {
$source = $source.not(this);
// this is the last event handler for this element, so we remove .data() properties
$(this)
.removeData(valueCache)
.removeData(isTracked);
$(this).removeData(valueCache);
}
};

Expand Down

0 comments on commit 643d41f

Please sign in to comment.