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("");

View File

@@ -4,9 +4,9 @@ var exports = {};
function setup_subscriptions_stream_hash(sub) {
var id = sub.stream_id;
subs.change_state.prevent_once();
window.location.hash = "#streams" + "/" + id + "/" + hash_util.encodeHashComponent(sub.name);
var hash = "#streams" + "/" + id + "/" + hash_util.encodeHashComponent(sub.name);
hashchange.update_browser_history(hash);
}
function settings_for_sub(sub) {

View File

@@ -613,14 +613,9 @@ exports.switch_to_stream_row = function (stream_id) {
};
exports.change_state = (function () {
var prevent_next = false;
// TODO: flatten this code
var func = function (hash) {
if (prevent_next) {
prevent_next = false;
return;
}
// if there are any arguments the state should be modified.
if (hash.arguments.length > 0) {
// if in #streams/new form.
@@ -638,10 +633,6 @@ exports.change_state = (function () {
}
};
func.prevent_once = function () {
prevent_next = true;
};
return func;
}());
@@ -796,11 +787,7 @@ exports.do_open_create_stream = function () {
exports.open_create_stream = function () {
exports.do_open_create_stream();
// this will change the hash which will attempt to retrigger the create
// stream code, so we prevent this once.
exports.change_state.prevent_once();
window.location.hash = "#streams/new";
hashchange.update_browser_history('#streams/new');
};