stream_topic_history: Convert per_stream_history to an ES6 class.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-07-22 17:59:59 -07:00 committed by Tim Abbott
parent e47fd521e3
commit 2bf6731f5a
1 changed files with 40 additions and 38 deletions

View File

@ -1,6 +1,6 @@
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();
exports.is_complete_for_stream_id = (stream_id) => {
@ -42,7 +42,7 @@ exports.stream_has_topics = function (stream_id) {
return history.has_topics();
};
exports.per_stream_history = function (stream_id) {
class PerStreamHistory {
/*
For a given stream, this structure has a dictionary of topics.
The main getter of this object is get_recent_topic_names, and
@ -72,32 +72,35 @@ exports.per_stream_history = function (stream_id) {
deleted.
*/
const topics = new FoldDict();
topics = new FoldDict();
// Most recent message ID for the stream.
let max_message_id = 0;
const self = {};
max_message_id = 0;
self.has_topics = function () {
return topics.size !== 0;
};
self.update_stream_max_message_id = function (message_id) {
if (message_id > max_message_id) {
max_message_id = message_id;
constructor(stream_id) {
this.stream_id = stream_id;
}
};
self.add_or_update = function (opts) {
has_topics() {
return this.topics.size !== 0;
}
update_stream_max_message_id(message_id) {
if (message_id > this.max_message_id) {
this.max_message_id = message_id;
}
}
add_or_update(opts) {
const topic_name = opts.topic_name;
let message_id = opts.message_id || 0;
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) {
topics.set(opts.topic_name, {
this.topics.set(opts.topic_name, {
message_id,
pretty_name: topic_name,
historical: false,
@ -114,10 +117,10 @@ exports.per_stream_history = function (stream_id) {
existing.message_id = message_id;
existing.pretty_name = topic_name;
}
};
}
self.maybe_remove = function (topic_name, num_messages) {
const existing = topics.get(topic_name);
maybe_remove(topic_name, num_messages) {
const existing = this.topics.get(topic_name);
if (!existing) {
return;
@ -131,14 +134,14 @@ exports.per_stream_history = function (stream_id) {
}
if (existing.count <= num_messages) {
topics.delete(topic_name);
this.topics.delete(topic_name);
return;
}
existing.count -= num_messages;
};
}
self.add_history = function (server_history) {
add_history(server_history) {
// This method populates historical topics from the
// server. We have less data about these than the
// client can maintain for newer topics.
@ -147,7 +150,7 @@ exports.per_stream_history = function (stream_id) {
const topic_name = obj.name;
const message_id = obj.max_id;
const existing = topics.get(topic_name);
const existing = this.topics.get(topic_name);
if (existing) {
if (!existing.historical) {
@ -161,21 +164,21 @@ exports.per_stream_history = function (stream_id) {
// the topic for the first time, or we are getting
// more current data for it.
topics.set(topic_name, {
this.topics.set(topic_name, {
message_id,
pretty_name: topic_name,
historical: true,
});
self.update_stream_max_message_id(message_id);
this.update_stream_max_message_id(message_id);
}
}
};
self.get_recent_topic_names = function () {
const my_recents = Array.from(topics.values());
get_recent_topic_names() {
const my_recents = Array.from(this.topics.values());
const missing_topics = unread.get_missing_topics({
stream_id,
topic_dict: topics,
stream_id: this.stream_id,
topic_dict: this.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);
return names;
};
}
self.get_max_message_id = function () {
return max_message_id;
};
return self;
};
get_max_message_id() {
return this.max_message_id;
}
}
exports.PerStreamHistory = PerStreamHistory;
exports.remove_messages = function (opts) {
const stream_id = opts.stream_id;
@ -215,7 +217,7 @@ exports.find_or_create = function (stream_id) {
let history = stream_dict.get(stream_id);
if (!history) {
history = exports.per_stream_history(stream_id);
history = new PerStreamHistory(stream_id);
stream_dict.set(stream_id, history);
}