From b32a9444dde492cda448c0e5a485ae7e46e36387 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Tue, 22 Aug 2017 12:20:00 -0400 Subject: [PATCH] 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. --- frontend_tests/node_tests/dispatch.js | 8 +++++-- static/js/server_events_dispatch.js | 2 +- static/js/stream_data.js | 30 +++++++++++++++++++++------ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index 7e0dc2f7ae..21e1886999 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -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); + }); }); diff --git a/static/js/server_events_dispatch.js b/static/js/server_events_dispatch.js index b148b2eb1a..77d80fb677 100644 --- a/static/js/server_events_dispatch.js +++ b/static/js/server_events_dispatch.js @@ -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; diff --git a/static/js/stream_data.js b/static/js/stream_data.js index f300bca3a7..74ee7f8288 100644 --- a/static/js/stream_data.js +++ b/static/js/stream_data.js @@ -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;