js: Clean up how we track default stream names.

We continue to have page_params.realm_default_streams, but
now we do lookups on whether a stream is a default stream
by using a Dict indexed by stream_id.

We are also careful to update that during live updates.

This fixes a flaw that we weren't updating the list of realms
correctly for events that remove a default stream.
This commit is contained in:
Steve Howell 2017-08-22 12:20:00 -04:00 committed by Tim Abbott
parent 73c30774cb
commit b32a9444dd
3 changed files with 31 additions and 9 deletions

View File

@ -455,8 +455,12 @@ with_overrides(function (override) {
// default_streams
var event = event_fixtures.default_streams;
override('settings_streams.update_default_streams_table', noop);
dispatch(event);
assert_same(page_params.realm_default_streams, event.default_streams);
global.with_stub(function (stub) {
override('stream_data.set_realm_default_streams', stub.f);
dispatch(event);
var args = stub.get_args('realm_default_streams');
assert_same(args.realm_default_streams, event.default_streams);
});
});

View File

@ -11,7 +11,7 @@ exports.dispatch_normal_event = function dispatch_normal_event(event) {
break;
case 'default_streams':
page_params.realm_default_streams = event.default_streams;
stream_data.set_realm_default_streams(event.default_streams);
settings_streams.update_default_streams_table();
break;

View File

@ -10,8 +10,6 @@ var subs_by_stream_id;
var stream_ids_by_name = new Dict({fold_case: true});
var defaults = {};
exports.clear_subscriptions = function () {
stream_info = new Dict({fold_case: true});
subs_by_stream_id = new Dict();
@ -224,8 +222,29 @@ exports.get_invite_only = function (stream_name) {
return sub.invite_only;
};
var default_stream_ids = new Dict();
exports.set_realm_default_streams = function (realm_default_streams) {
page_params.realm_default_streams = realm_default_streams;
default_stream_ids.clear();
realm_default_streams.forEach(function (stream) {
default_stream_ids.set(stream.stream_id, true);
});
};
exports.get_default_status = function (stream_name) {
return defaults.hasOwnProperty(stream_name);
var stream_id = exports.get_stream_id(stream_name);
if (!stream_id) {
return false;
}
return default_stream_ids.has(stream_id);
};
exports.is_default_stream_id = function (stream_id) {
return default_stream_ids.has(stream_id);
};
exports.get_name = function (stream_name) {
@ -423,9 +442,7 @@ exports.initialize_from_page_params = function () {
});
}
page_params.realm_default_streams.forEach(function (stream) {
defaults[stream.name] = true;
});
exports.set_realm_default_streams(page_params.realm_default_streams);
populate_subscriptions(page_params.subscriptions, true);
populate_subscriptions(page_params.unsubscribed, false);
@ -466,6 +483,7 @@ exports.remove_default_stream = function (stream_id) {
return stream.stream_id === stream_id;
}
);
default_stream_ids.del(stream_id);
};
return exports;