mirror of
https://github.com/zulip/zulip.git
synced 2026-06-21 21:32:29 +08:00
local echo: Extract message_store.reify_message_id().
We no longer do the message_store piece of reifying ids
via a trigger. We now make an explicit call to an
ordinary function.
This has several benefits:
- no more initialize() function
- no more scary comments about garbage collection
- the function has a real name now
- the function is less indented
- we can easily see when the message_store step happens
- simpler node tests
- simpler tracebacks (no jQuery cruft)
This commit is contained in:
parent
b4d49720b0
commit
c256b1663e
@ -62,7 +62,6 @@ people.add_in_realm(cindy);
|
||||
global.people.initialize_current_user(me.user_id);
|
||||
|
||||
var message_store = require('js/message_store.js');
|
||||
message_store.initialize();
|
||||
|
||||
(function test_insert_recent_private_message() {
|
||||
message_store.insert_recent_private_message('1', 1001);
|
||||
@ -244,16 +243,14 @@ message_store.initialize();
|
||||
set_global('message_list', {});
|
||||
set_global('home_msg_list', {});
|
||||
|
||||
var event = {
|
||||
var opts = {
|
||||
old_id: 401,
|
||||
new_id: 402,
|
||||
};
|
||||
|
||||
var on_id_message_changed_func = $(document).get_on_handler('message_id_changed');
|
||||
|
||||
global.with_stub(function (stub) {
|
||||
home_msg_list.change_message_id = stub.f;
|
||||
on_id_message_changed_func(event);
|
||||
message_store.reify_message_id(opts);
|
||||
var msg_id = stub.get_args('old', 'new');
|
||||
assert.equal(msg_id.old, 401);
|
||||
assert.equal(msg_id.new, 402);
|
||||
@ -262,7 +259,7 @@ message_store.initialize();
|
||||
home_msg_list.view = {};
|
||||
global.with_stub(function (stub) {
|
||||
home_msg_list.view.change_message_id = stub.f;
|
||||
on_id_message_changed_func(event);
|
||||
message_store.reify_message_id(opts);
|
||||
var msg_id = stub.get_args('old', 'new');
|
||||
assert.equal(msg_id.old, 401);
|
||||
assert.equal(msg_id.new, 402);
|
||||
|
||||
@ -182,8 +182,11 @@ exports.reify_message_id = function reify_message_id(local_id, server_id) {
|
||||
message.id = server_id;
|
||||
delete message.local_id;
|
||||
|
||||
// We have the real message ID for this message
|
||||
$(document).trigger($.Event('message_id_changed', {old_id: local_id, new_id: server_id}));
|
||||
var opts = {old_id: local_id, new_id: server_id};
|
||||
|
||||
message_store.reify_message_id(opts);
|
||||
|
||||
$(document).trigger($.Event('message_id_changed', opts));
|
||||
};
|
||||
|
||||
exports.process_from_server = function process_from_server(messages) {
|
||||
|
||||
@ -176,33 +176,25 @@ exports.add_message_metadata = function (message) {
|
||||
return message;
|
||||
};
|
||||
|
||||
exports.initialize = function () {
|
||||
$(document).on('message_id_changed', function (event) {
|
||||
var old_id = event.old_id;
|
||||
var new_id = event.new_id;
|
||||
if (pointer.furthest_read === old_id) {
|
||||
pointer.furthest_read = new_id;
|
||||
}
|
||||
if (stored_messages[old_id]) {
|
||||
stored_messages[new_id] = stored_messages[old_id];
|
||||
delete stored_messages[old_id];
|
||||
}
|
||||
exports.reify_message_id = function (opts) {
|
||||
var old_id = opts.old_id;
|
||||
var new_id = opts.new_id;
|
||||
if (pointer.furthest_read === old_id) {
|
||||
pointer.furthest_read = new_id;
|
||||
}
|
||||
if (stored_messages[old_id]) {
|
||||
stored_messages[new_id] = stored_messages[old_id];
|
||||
delete stored_messages[old_id];
|
||||
}
|
||||
|
||||
// This handler cannot be in the MessageList constructor, which is the logical place
|
||||
// If it's there, the event handler creates a closure with a reference to the message
|
||||
// list itself. When narrowing, the old narrow message list is discarded and a new one
|
||||
// created, but due to the closure, the old list is not garbage collected. This also leads
|
||||
// to the old list receiving the change id events, and throwing errors as it does not
|
||||
// have the messages that you would expect in its internal data structures.
|
||||
_.each([message_list.all, home_msg_list, message_list.narrowed], function (msg_list) {
|
||||
if (msg_list !== undefined) {
|
||||
msg_list.change_message_id(old_id, new_id);
|
||||
_.each([message_list.all, home_msg_list, message_list.narrowed], function (msg_list) {
|
||||
if (msg_list !== undefined) {
|
||||
msg_list.change_message_id(old_id, new_id);
|
||||
|
||||
if (msg_list.view !== undefined) {
|
||||
msg_list.view.change_message_id(old_id, new_id);
|
||||
}
|
||||
if (msg_list.view !== undefined) {
|
||||
msg_list.view.change_message_id(old_id, new_id);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@ -251,7 +251,6 @@ $(function () {
|
||||
people.initialize();
|
||||
bot_data.initialize(); // Must happen after people.initialize()
|
||||
message_fetch.initialize();
|
||||
message_store.initialize();
|
||||
markdown.initialize();
|
||||
composebox_typeahead.initialize();
|
||||
search.initialize();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user