rm $.debounce, better Relative Dates.
This commit is contained in:
parent
7657167953
commit
479ddffd95
@ -835,20 +835,6 @@
|
|||||||
open: function(url) {
|
open: function(url) {
|
||||||
return (GM_openInTab || window.open)(url, '_blank');
|
return (GM_openInTab || window.open)(url, '_blank');
|
||||||
},
|
},
|
||||||
debounce: function(wait, fn) {
|
|
||||||
var timeout;
|
|
||||||
timeout = null;
|
|
||||||
return function() {
|
|
||||||
if (timeout) {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
} else {
|
|
||||||
fn.apply(this, arguments);
|
|
||||||
}
|
|
||||||
return timeout = setTimeout((function() {
|
|
||||||
return timeout = null;
|
|
||||||
}), wait);
|
|
||||||
};
|
|
||||||
},
|
|
||||||
queueTask: (function() {
|
queueTask: (function() {
|
||||||
var execTask, taskChannel, taskQueue;
|
var execTask, taskChannel, taskQueue;
|
||||||
taskQueue = [];
|
taskQueue = [];
|
||||||
@ -3334,18 +3320,21 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$.on(d, 'visibilitychange ThreadUpdate', this.flush);
|
$.on(d, 'visibilitychange ThreadUpdate', this.flush);
|
||||||
|
this.flush();
|
||||||
return Post.prototype.callbacks.push({
|
return Post.prototype.callbacks.push({
|
||||||
name: 'Relative Post Dates',
|
name: 'Relative Post Dates',
|
||||||
cb: this.node
|
cb: this.node
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
node: function() {
|
node: function() {
|
||||||
var dateEl, diff;
|
var dateEl;
|
||||||
|
if (this.isClone) {
|
||||||
|
RelativeDates.flush();
|
||||||
|
return;
|
||||||
|
}
|
||||||
dateEl = this.nodes.date;
|
dateEl = this.nodes.date;
|
||||||
dateEl.title = dateEl.textContent;
|
dateEl.title = dateEl.textContent;
|
||||||
diff = Date.now() - this.info.date;
|
return RelativeDates.setUpdate(this);
|
||||||
dateEl.textContent = RelativeDates.relative(diff);
|
|
||||||
return RelativeDates.setUpdate(this, diff);
|
|
||||||
},
|
},
|
||||||
relative: function(diff) {
|
relative: function(diff) {
|
||||||
var number, rounded, unit;
|
var number, rounded, unit;
|
||||||
@ -3357,7 +3346,7 @@
|
|||||||
return "" + rounded + " " + unit + " ago";
|
return "" + rounded + " " + unit + " ago";
|
||||||
},
|
},
|
||||||
stale: [],
|
stale: [],
|
||||||
flush: $.debounce($.SECOND, function() {
|
flush: function() {
|
||||||
var now, update, _i, _len, _ref;
|
var now, update, _i, _len, _ref;
|
||||||
if (d.hidden) {
|
if (d.hidden) {
|
||||||
return;
|
return;
|
||||||
@ -3371,26 +3360,30 @@
|
|||||||
RelativeDates.stale = [];
|
RelativeDates.stale = [];
|
||||||
clearTimeout(RelativeDates.timeout);
|
clearTimeout(RelativeDates.timeout);
|
||||||
return RelativeDates.timeout = setTimeout(RelativeDates.flush, RelativeDates.INTERVAL);
|
return RelativeDates.timeout = setTimeout(RelativeDates.flush, RelativeDates.INTERVAL);
|
||||||
}),
|
},
|
||||||
setUpdate: function(post, diff) {
|
setUpdate: function(post) {
|
||||||
var dateEl, markStale, setOwnTimeout, update;
|
var markStale, setOwnTimeout, update;
|
||||||
setOwnTimeout = function(diff) {
|
setOwnTimeout = function(diff) {
|
||||||
var delay;
|
var delay;
|
||||||
delay = diff > $.HOUR ? diff % $.HOUR : diff > $.MINUTE ? diff % $.MINUTE : diff % $.SECOND;
|
delay = diff > $.HOUR ? diff % $.HOUR : diff > $.MINUTE ? diff % $.MINUTE : diff % $.SECOND;
|
||||||
return setTimeout(markStale, delay);
|
return setTimeout(markStale, delay);
|
||||||
};
|
};
|
||||||
dateEl = post.nodes.date;
|
|
||||||
update = function(now) {
|
update = function(now) {
|
||||||
if (d.contains(dateEl)) {
|
var dateEl, diff, relative, singlePost, _i, _len, _ref;
|
||||||
diff = now - post.info.date;
|
diff = now - post.info.date;
|
||||||
dateEl.textContent = RelativeDates.relative(diff);
|
relative = RelativeDates.relative(diff);
|
||||||
return setOwnTimeout(diff);
|
_ref = [post].concat(post.clones);
|
||||||
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||||
|
singlePost = _ref[_i];
|
||||||
|
dateEl = singlePost.nodes.date;
|
||||||
|
dateEl.textContent = relative;
|
||||||
}
|
}
|
||||||
|
return setOwnTimeout(diff);
|
||||||
};
|
};
|
||||||
markStale = function() {
|
markStale = function() {
|
||||||
return RelativeDates.stale.push(update);
|
return RelativeDates.stale.push(update);
|
||||||
};
|
};
|
||||||
return setOwnTimeout(diff);
|
return update(Date.now());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
11
lib/$.coffee
11
lib/$.coffee
@ -140,17 +140,6 @@ $.extend $,
|
|||||||
root.dispatchEvent new CustomEvent event, {bubbles: true, detail}
|
root.dispatchEvent new CustomEvent event, {bubbles: true, detail}
|
||||||
open: (url) ->
|
open: (url) ->
|
||||||
(GM_openInTab or window.open) url, '_blank'
|
(GM_openInTab or window.open) url, '_blank'
|
||||||
debounce: (wait, fn) ->
|
|
||||||
timeout = null
|
|
||||||
return ->
|
|
||||||
if timeout
|
|
||||||
# stop current reset
|
|
||||||
clearTimeout timeout
|
|
||||||
else
|
|
||||||
fn.apply this, arguments
|
|
||||||
|
|
||||||
# after wait, let next invocation execute immediately
|
|
||||||
timeout = setTimeout (-> timeout = null), wait
|
|
||||||
queueTask: (->
|
queueTask: (->
|
||||||
# inspired by https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007
|
# inspired by https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007
|
||||||
taskQueue = []
|
taskQueue = []
|
||||||
|
|||||||
@ -2002,21 +2002,24 @@ RelativeDates =
|
|||||||
# flush when page becomes visible again
|
# flush when page becomes visible again
|
||||||
$.on d, 'visibilitychange ThreadUpdate', @flush
|
$.on d, 'visibilitychange ThreadUpdate', @flush
|
||||||
|
|
||||||
|
# setup the timeout
|
||||||
|
@flush()
|
||||||
|
|
||||||
Post::callbacks.push
|
Post::callbacks.push
|
||||||
name: 'Relative Post Dates'
|
name: 'Relative Post Dates'
|
||||||
cb: @node
|
cb: @node
|
||||||
node: ->
|
node: ->
|
||||||
dateEl = @nodes.date
|
if @isClone
|
||||||
|
RelativeDates.flush()
|
||||||
|
return
|
||||||
|
|
||||||
# Show original absolute time as tooltip so users can still know exact times
|
# Show original absolute time as tooltip so users can still know exact times
|
||||||
# Since "Time Formatting" runs `node` before us, the title tooltip will
|
# Since "Time Formatting" runs `node` before us, the title tooltip will
|
||||||
# pick up the user-formatted time instead of 4chan time when enabled.
|
# pick up the user-formatted time instead of 4chan time when enabled.
|
||||||
|
dateEl = @nodes.date
|
||||||
dateEl.title = dateEl.textContent
|
dateEl.title = dateEl.textContent
|
||||||
|
|
||||||
diff = Date.now() - @info.date
|
RelativeDates.setUpdate @
|
||||||
|
|
||||||
dateEl.textContent = RelativeDates.relative diff
|
|
||||||
RelativeDates.setUpdate @, diff
|
|
||||||
|
|
||||||
# diff is milliseconds from now
|
# diff is milliseconds from now
|
||||||
relative: (diff) ->
|
relative: (diff) ->
|
||||||
@ -2044,7 +2047,7 @@ RelativeDates =
|
|||||||
# each individual dateTime element will add its update() function to the stale list
|
# each individual dateTime element will add its update() function to the stale list
|
||||||
# when it is to be called.
|
# when it is to be called.
|
||||||
stale: []
|
stale: []
|
||||||
flush: $.debounce($.SECOND, ->
|
flush: ->
|
||||||
# no point in changing the dates until the user sees them
|
# no point in changing the dates until the user sees them
|
||||||
return if d.hidden
|
return if d.hidden
|
||||||
|
|
||||||
@ -2054,12 +2057,12 @@ RelativeDates =
|
|||||||
|
|
||||||
# reset automatic flush
|
# reset automatic flush
|
||||||
clearTimeout RelativeDates.timeout
|
clearTimeout RelativeDates.timeout
|
||||||
RelativeDates.timeout = setTimeout RelativeDates.flush, RelativeDates.INTERVAL)
|
RelativeDates.timeout = setTimeout RelativeDates.flush, RelativeDates.INTERVAL
|
||||||
|
|
||||||
# create function `update()`, closed over post and diff, that, when called
|
# create function `update()`, closed over post and diff, that, when called
|
||||||
# from `flush()`, updates the element, and re-calls `setOwnTimeout()` to
|
# from `flush()`, updates the element, and re-calls `setOwnTimeout()` to
|
||||||
# re-add `update()` to the stale list later.
|
# re-add `update()` to the stale list later.
|
||||||
setUpdate: (post, diff) ->
|
setUpdate: (post) ->
|
||||||
setOwnTimeout = (diff) ->
|
setOwnTimeout = (diff) ->
|
||||||
delay = if diff > $.HOUR
|
delay = if diff > $.HOUR
|
||||||
diff % $.HOUR
|
diff % $.HOUR
|
||||||
@ -2069,17 +2072,18 @@ RelativeDates =
|
|||||||
diff % $.SECOND
|
diff % $.SECOND
|
||||||
setTimeout markStale, delay
|
setTimeout markStale, delay
|
||||||
|
|
||||||
dateEl = post.nodes.date
|
|
||||||
update = (now) ->
|
update = (now) ->
|
||||||
if d.contains dateEl # not removed from DOM
|
diff = now - post.info.date
|
||||||
diff = now - post.info.date
|
relative = RelativeDates.relative diff
|
||||||
dateEl.textContent = RelativeDates.relative diff
|
for singlePost in [post].concat post.clones
|
||||||
setOwnTimeout diff
|
dateEl = singlePost.nodes.date
|
||||||
|
dateEl.textContent = relative
|
||||||
|
setOwnTimeout diff
|
||||||
|
|
||||||
markStale = -> RelativeDates.stale.push update
|
markStale = -> RelativeDates.stale.push update
|
||||||
|
|
||||||
# kick off initial timeout with current diff
|
# kick off initial timeout with current diff
|
||||||
setOwnTimeout diff
|
update Date.now()
|
||||||
|
|
||||||
FileInfo =
|
FileInfo =
|
||||||
init: ->
|
init: ->
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user