hashchange: Add hashchange.update_browser_history().

This helps us encapsulate the situation where we don't
want to trigger hashchanged(), without having to do it
within sub.js with prevent_once().
This commit is contained in:
Steve Howell
2018-12-01 19:15:50 +00:00
committed by Tim Abbott
parent 212ee015d4
commit 84d0b541e3
3 changed files with 27 additions and 17 deletions

View File

@@ -168,6 +168,7 @@ function do_hashchange(from_reload) {
// hash change functionally inert.
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - -- //
var state = {
is_internal_change: false,
is_exiting_overlay: false,
hash_before_overlay: null,
old_hash: typeof window !== "undefined" ? window.location.hash : "#",
@@ -269,7 +270,29 @@ function hashchanged_overlay(old_hash) {
state.old_overlay_group = get_hash_group(base);
}
exports.update_browser_history = function (new_hash) {
var old_hash = window.location.hash;
if (old_hash === new_hash) {
// If somebody is calling us with the same hash we already have, it's
// probably harmless, and we just ignore it. But it could be a symptom
// of disorganized code that's prone to an infinite loop of repeatedly
// assigning the same hash.
blueslip.info('ignoring probably-harmless call to update_browser_history: ' + new_hash);
return;
}
state.old_hash = old_hash;
state.is_internal_change = true;
window.location.hash = new_hash;
};
function hashchanged(from_reload, e) {
if (state.is_internal_change) {
state.is_internal_change = false;
return;
}
var old_hash;
if (e) {
old_hash = "#" + (e.oldURL || state.old_hash).split(/#/).slice(1).join("");