mirror of https://github.com/zulip/zulip.git
stream_topic_history: Convert per_stream_history to an ES6 class.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
e47fd521e3
commit
2bf6731f5a
|
@ -1,6 +1,6 @@
|
||||||
const FoldDict = require("./fold_dict").FoldDict;
|
const FoldDict = require("./fold_dict").FoldDict;
|
||||||
|
|
||||||
const stream_dict = new Map(); // stream_id -> per_stream_history object
|
const stream_dict = new Map(); // stream_id -> PerStreamHistory object
|
||||||
const fetched_stream_ids = new Set();
|
const fetched_stream_ids = new Set();
|
||||||
|
|
||||||
exports.is_complete_for_stream_id = (stream_id) => {
|
exports.is_complete_for_stream_id = (stream_id) => {
|
||||||
|
@ -42,7 +42,7 @@ exports.stream_has_topics = function (stream_id) {
|
||||||
return history.has_topics();
|
return history.has_topics();
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.per_stream_history = function (stream_id) {
|
class PerStreamHistory {
|
||||||
/*
|
/*
|
||||||
For a given stream, this structure has a dictionary of topics.
|
For a given stream, this structure has a dictionary of topics.
|
||||||
The main getter of this object is get_recent_topic_names, and
|
The main getter of this object is get_recent_topic_names, and
|
||||||
|
@ -72,32 +72,35 @@ exports.per_stream_history = function (stream_id) {
|
||||||
deleted.
|
deleted.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const topics = new FoldDict();
|
topics = new FoldDict();
|
||||||
// Most recent message ID for the stream.
|
// Most recent message ID for the stream.
|
||||||
let max_message_id = 0;
|
max_message_id = 0;
|
||||||
const self = {};
|
|
||||||
|
|
||||||
self.has_topics = function () {
|
constructor(stream_id) {
|
||||||
return topics.size !== 0;
|
this.stream_id = stream_id;
|
||||||
};
|
}
|
||||||
|
|
||||||
self.update_stream_max_message_id = function (message_id) {
|
has_topics() {
|
||||||
if (message_id > max_message_id) {
|
return this.topics.size !== 0;
|
||||||
max_message_id = message_id;
|
}
|
||||||
|
|
||||||
|
update_stream_max_message_id(message_id) {
|
||||||
|
if (message_id > this.max_message_id) {
|
||||||
|
this.max_message_id = message_id;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
self.add_or_update = function (opts) {
|
add_or_update(opts) {
|
||||||
const topic_name = opts.topic_name;
|
const topic_name = opts.topic_name;
|
||||||
let message_id = opts.message_id || 0;
|
let message_id = opts.message_id || 0;
|
||||||
|
|
||||||
message_id = parseInt(message_id, 10);
|
message_id = parseInt(message_id, 10);
|
||||||
self.update_stream_max_message_id(message_id);
|
this.update_stream_max_message_id(message_id);
|
||||||
|
|
||||||
const existing = topics.get(topic_name);
|
const existing = this.topics.get(topic_name);
|
||||||
|
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
topics.set(opts.topic_name, {
|
this.topics.set(opts.topic_name, {
|
||||||
message_id,
|
message_id,
|
||||||
pretty_name: topic_name,
|
pretty_name: topic_name,
|
||||||
historical: false,
|
historical: false,
|
||||||
|
@ -114,10 +117,10 @@ exports.per_stream_history = function (stream_id) {
|
||||||
existing.message_id = message_id;
|
existing.message_id = message_id;
|
||||||
existing.pretty_name = topic_name;
|
existing.pretty_name = topic_name;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
self.maybe_remove = function (topic_name, num_messages) {
|
maybe_remove(topic_name, num_messages) {
|
||||||
const existing = topics.get(topic_name);
|
const existing = this.topics.get(topic_name);
|
||||||
|
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
return;
|
return;
|
||||||
|
@ -131,14 +134,14 @@ exports.per_stream_history = function (stream_id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (existing.count <= num_messages) {
|
if (existing.count <= num_messages) {
|
||||||
topics.delete(topic_name);
|
this.topics.delete(topic_name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
existing.count -= num_messages;
|
existing.count -= num_messages;
|
||||||
};
|
}
|
||||||
|
|
||||||
self.add_history = function (server_history) {
|
add_history(server_history) {
|
||||||
// This method populates historical topics from the
|
// This method populates historical topics from the
|
||||||
// server. We have less data about these than the
|
// server. We have less data about these than the
|
||||||
// client can maintain for newer topics.
|
// client can maintain for newer topics.
|
||||||
|
@ -147,7 +150,7 @@ exports.per_stream_history = function (stream_id) {
|
||||||
const topic_name = obj.name;
|
const topic_name = obj.name;
|
||||||
const message_id = obj.max_id;
|
const message_id = obj.max_id;
|
||||||
|
|
||||||
const existing = topics.get(topic_name);
|
const existing = this.topics.get(topic_name);
|
||||||
|
|
||||||
if (existing) {
|
if (existing) {
|
||||||
if (!existing.historical) {
|
if (!existing.historical) {
|
||||||
|
@ -161,21 +164,21 @@ exports.per_stream_history = function (stream_id) {
|
||||||
// the topic for the first time, or we are getting
|
// the topic for the first time, or we are getting
|
||||||
// more current data for it.
|
// more current data for it.
|
||||||
|
|
||||||
topics.set(topic_name, {
|
this.topics.set(topic_name, {
|
||||||
message_id,
|
message_id,
|
||||||
pretty_name: topic_name,
|
pretty_name: topic_name,
|
||||||
historical: true,
|
historical: true,
|
||||||
});
|
});
|
||||||
self.update_stream_max_message_id(message_id);
|
this.update_stream_max_message_id(message_id);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
self.get_recent_topic_names = function () {
|
get_recent_topic_names() {
|
||||||
const my_recents = Array.from(topics.values());
|
const my_recents = Array.from(this.topics.values());
|
||||||
|
|
||||||
const missing_topics = unread.get_missing_topics({
|
const missing_topics = unread.get_missing_topics({
|
||||||
stream_id,
|
stream_id: this.stream_id,
|
||||||
topic_dict: topics,
|
topic_dict: this.topics,
|
||||||
});
|
});
|
||||||
|
|
||||||
const recents = my_recents.concat(missing_topics);
|
const recents = my_recents.concat(missing_topics);
|
||||||
|
@ -185,14 +188,13 @@ exports.per_stream_history = function (stream_id) {
|
||||||
const names = recents.map((obj) => obj.pretty_name);
|
const names = recents.map((obj) => obj.pretty_name);
|
||||||
|
|
||||||
return names;
|
return names;
|
||||||
};
|
}
|
||||||
|
|
||||||
self.get_max_message_id = function () {
|
get_max_message_id() {
|
||||||
return max_message_id;
|
return this.max_message_id;
|
||||||
};
|
}
|
||||||
|
}
|
||||||
return self;
|
exports.PerStreamHistory = PerStreamHistory;
|
||||||
};
|
|
||||||
|
|
||||||
exports.remove_messages = function (opts) {
|
exports.remove_messages = function (opts) {
|
||||||
const stream_id = opts.stream_id;
|
const stream_id = opts.stream_id;
|
||||||
|
@ -215,7 +217,7 @@ exports.find_or_create = function (stream_id) {
|
||||||
let history = stream_dict.get(stream_id);
|
let history = stream_dict.get(stream_id);
|
||||||
|
|
||||||
if (!history) {
|
if (!history) {
|
||||||
history = exports.per_stream_history(stream_id);
|
history = new PerStreamHistory(stream_id);
|
||||||
stream_dict.set(stream_id, history);
|
stream_dict.set(stream_id, history);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue