widgetize: Convert widget_contents from object to Map.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2020-02-06 03:47:46 +00:00 committed by Tim Abbott
parent 56436188a4
commit 52a8449a0e
2 changed files with 8 additions and 8 deletions

View File

@ -96,14 +96,14 @@ run_test('activate', () => {
is_widget_elem_inserted = false;
is_widget_activated = false;
is_event_handled = false;
assert.equal(widgetize.widget_contents[opts.message.id], undefined);
assert(!widgetize.widget_contents.has(opts.message.id));
widgetize.activate(opts);
assert(is_widget_elem_inserted);
assert(is_widget_activated);
assert(is_event_handled);
assert.equal(widgetize.widget_contents[opts.message.id], widget_elem);
assert.equal(widgetize.widget_contents.get(opts.message.id), widget_elem);
is_widget_elem_inserted = false;
is_widget_activated = false;

View File

@ -5,7 +5,7 @@ const widgets = new Map([
["zform", zform],
]);
const widget_contents = {};
const widget_contents = new Map();
exports.widget_contents = widget_contents;
function set_widget_in_message(row, widget_elem) {
@ -41,7 +41,7 @@ exports.activate = function (in_opts) {
return;
}
let widget_elem = widget_contents[message.id];
let widget_elem = widget_contents.get(message.id);
if (widget_elem) {
set_widget_in_message(row, widget_elem);
return;
@ -58,7 +58,7 @@ exports.activate = function (in_opts) {
extra_data: extra_data,
});
widget_contents[message.id] = widget_elem;
widget_contents.set(message.id, widget_elem);
set_widget_in_message(row, widget_elem);
// Replay any events that already happened. (This is common
@ -70,16 +70,16 @@ exports.activate = function (in_opts) {
};
exports.set_widgets_for_list = function () {
_.each(widget_contents, function (widget_elem, idx) {
for (const [idx, widget_elem] of widget_contents) {
if (current_msg_list.get(idx) !== undefined) {
const row = current_msg_list.get_row(idx);
set_widget_in_message(row, widget_elem);
}
});
}
};
exports.handle_event = function (widget_event) {
const widget_elem = widget_contents[widget_event.message_id];
const widget_elem = widget_contents.get(widget_event.message_id);
if (!widget_elem) {
// It is common for submessage events to arrive on