mirror of
https://github.com/zulip/zulip.git
synced 2025-11-09 00:18:12 +00:00
stream_topic_history: Convert module to typescript.
This commit is contained in:
@@ -1331,7 +1331,7 @@ export function with_first_message_id(stream_id, topic_name, success_cb, error_c
|
|||||||
// possible that we might display a topic in the left sidebar
|
// possible that we might display a topic in the left sidebar
|
||||||
// (and thus expose the UI for moving its topic to another
|
// (and thus expose the UI for moving its topic to another
|
||||||
// stream) without having a message ID that is definitely
|
// stream) without having a message ID that is definitely
|
||||||
// within the topic. (The comments in stream_topic_history.js
|
// within the topic. (The comments in stream_topic_history.ts
|
||||||
// discuss the tricky issues around message deletion that are
|
// discuss the tricky issues around message deletion that are
|
||||||
// involved here).
|
// involved here).
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
|
import assert from "minimalistic-assert";
|
||||||
|
|
||||||
import {all_messages_data} from "./all_messages_data";
|
import {all_messages_data} from "./all_messages_data";
|
||||||
import {FoldDict} from "./fold_dict";
|
import {FoldDict} from "./fold_dict";
|
||||||
import * as message_util from "./message_util";
|
import * as message_util from "./message_util";
|
||||||
import * as sub_store from "./sub_store";
|
import * as sub_store from "./sub_store";
|
||||||
|
import type {StreamSubscription} from "./sub_store";
|
||||||
import * as unread from "./unread";
|
import * as unread from "./unread";
|
||||||
|
|
||||||
const stream_dict = new Map(); // stream_id -> PerStreamHistory object
|
// stream_id -> PerStreamHistory object
|
||||||
const fetched_stream_ids = new Set();
|
const stream_dict = new Map<number, PerStreamHistory>();
|
||||||
const request_pending_stream_ids = new Set();
|
const fetched_stream_ids = new Set<number>();
|
||||||
|
const request_pending_stream_ids = new Set<number>();
|
||||||
|
|
||||||
export function all_topics_in_cache(sub) {
|
export function all_topics_in_cache(sub: StreamSubscription): boolean {
|
||||||
// Checks whether this browser's cache of contiguous messages
|
// Checks whether this browser's cache of contiguous messages
|
||||||
// (used to locally render narrows) in all_messages_data has all
|
// (used to locally render narrows) in all_messages_data has all
|
||||||
// messages from a given stream, and thus all historical topics
|
// messages from a given stream, and thus all historical topics
|
||||||
@@ -41,13 +45,13 @@ export function all_topics_in_cache(sub) {
|
|||||||
return first_cached_message.id <= sub.first_message_id;
|
return first_cached_message.id <= sub.first_message_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function is_complete_for_stream_id(stream_id) {
|
export function is_complete_for_stream_id(stream_id: number): boolean {
|
||||||
if (fetched_stream_ids.has(stream_id)) {
|
if (fetched_stream_ids.has(stream_id)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const sub = sub_store.get(stream_id);
|
const sub = sub_store.get(stream_id);
|
||||||
const in_cache = all_topics_in_cache(sub);
|
const in_cache = sub !== undefined && all_topics_in_cache(sub);
|
||||||
|
|
||||||
if (in_cache) {
|
if (in_cache) {
|
||||||
/*
|
/*
|
||||||
@@ -63,16 +67,35 @@ export function is_complete_for_stream_id(stream_id) {
|
|||||||
return in_cache;
|
return in_cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stream_has_topics(stream_id) {
|
export function stream_has_topics(stream_id: number): boolean {
|
||||||
if (!stream_dict.has(stream_id)) {
|
if (!stream_dict.has(stream_id)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const history = stream_dict.get(stream_id);
|
const history = stream_dict.get(stream_id);
|
||||||
|
assert(history !== undefined);
|
||||||
|
|
||||||
return history.has_topics();
|
return history.has_topics();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type TopicHistoryEntry =
|
||||||
|
| {
|
||||||
|
historical: true;
|
||||||
|
message_id: number;
|
||||||
|
pretty_name: string;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
historical: false;
|
||||||
|
count: number;
|
||||||
|
message_id: number;
|
||||||
|
pretty_name: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type ServerTopicHistoryEntry = {
|
||||||
|
name: string;
|
||||||
|
max_id: number;
|
||||||
|
};
|
||||||
|
|
||||||
export class PerStreamHistory {
|
export class PerStreamHistory {
|
||||||
/*
|
/*
|
||||||
For a given stream, this structure has a dictionary of topics.
|
For a given stream, this structure has a dictionary of topics.
|
||||||
@@ -103,26 +126,26 @@ export class PerStreamHistory {
|
|||||||
deleted.
|
deleted.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
topics = new FoldDict();
|
topics = new FoldDict<TopicHistoryEntry>();
|
||||||
// Most recent message ID for the stream.
|
// Most recent message ID for the stream.
|
||||||
max_message_id = 0;
|
max_message_id = 0;
|
||||||
|
stream_id: number;
|
||||||
|
|
||||||
constructor(stream_id) {
|
constructor(stream_id: number) {
|
||||||
this.stream_id = stream_id;
|
this.stream_id = stream_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
has_topics() {
|
has_topics(): boolean {
|
||||||
return this.topics.size !== 0;
|
return this.topics.size !== 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
update_stream_max_message_id(message_id) {
|
update_stream_max_message_id(message_id: number): void {
|
||||||
if (message_id > this.max_message_id) {
|
if (message_id > this.max_message_id) {
|
||||||
this.max_message_id = message_id;
|
this.max_message_id = message_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
add_or_update(topic_name, message_id) {
|
add_or_update(topic_name: string, message_id: number): void {
|
||||||
message_id = Number.parseInt(message_id, 10);
|
|
||||||
this.update_stream_max_message_id(message_id);
|
this.update_stream_max_message_id(message_id);
|
||||||
|
|
||||||
const existing = this.topics.get(topic_name);
|
const existing = this.topics.get(topic_name);
|
||||||
@@ -147,7 +170,7 @@ export class PerStreamHistory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
maybe_remove(topic_name, num_messages) {
|
maybe_remove(topic_name: string, num_messages: number): void {
|
||||||
const existing = this.topics.get(topic_name);
|
const existing = this.topics.get(topic_name);
|
||||||
|
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
@@ -169,7 +192,7 @@ export class PerStreamHistory {
|
|||||||
existing.count -= num_messages;
|
existing.count -= num_messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
add_history(server_history) {
|
add_history(server_history: ServerTopicHistoryEntry[]): void {
|
||||||
// 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.
|
||||||
@@ -199,7 +222,7 @@ export class PerStreamHistory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get_recent_topic_names() {
|
get_recent_topic_names(): string[] {
|
||||||
const my_recents = [...this.topics.values()];
|
const my_recents = [...this.topics.values()];
|
||||||
|
|
||||||
/* Add any older topics with unreads that may not be present
|
/* Add any older topics with unreads that may not be present
|
||||||
@@ -218,12 +241,17 @@ export class PerStreamHistory {
|
|||||||
return names;
|
return names;
|
||||||
}
|
}
|
||||||
|
|
||||||
get_max_message_id() {
|
get_max_message_id(): number {
|
||||||
return this.max_message_id;
|
return this.max_message_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function remove_messages(opts) {
|
export function remove_messages(opts: {
|
||||||
|
stream_id: number;
|
||||||
|
topic_name: string;
|
||||||
|
num_messages: number;
|
||||||
|
max_removed_msg_id: number;
|
||||||
|
}): void {
|
||||||
const stream_id = opts.stream_id;
|
const stream_id = opts.stream_id;
|
||||||
const topic_name = opts.topic_name;
|
const topic_name = opts.topic_name;
|
||||||
const num_messages = opts.num_messages;
|
const num_messages = opts.num_messages;
|
||||||
@@ -263,7 +291,7 @@ export function remove_messages(opts) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function find_or_create(stream_id) {
|
export function find_or_create(stream_id: number): PerStreamHistory {
|
||||||
let history = stream_dict.get(stream_id);
|
let history = stream_dict.get(stream_id);
|
||||||
|
|
||||||
if (!history) {
|
if (!history) {
|
||||||
@@ -274,9 +302,13 @@ export function find_or_create(stream_id) {
|
|||||||
return history;
|
return history;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function add_message(opts) {
|
export function add_message(opts: {
|
||||||
|
stream_id: number;
|
||||||
|
message_id: number;
|
||||||
|
topic_name: string;
|
||||||
|
}): void {
|
||||||
const stream_id = opts.stream_id;
|
const stream_id = opts.stream_id;
|
||||||
const message_id = opts.message_id ?? 0;
|
const message_id = opts.message_id;
|
||||||
const topic_name = opts.topic_name;
|
const topic_name = opts.topic_name;
|
||||||
|
|
||||||
const history = find_or_create(stream_id);
|
const history = find_or_create(stream_id);
|
||||||
@@ -284,43 +316,43 @@ export function add_message(opts) {
|
|||||||
history.add_or_update(topic_name, message_id);
|
history.add_or_update(topic_name, message_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function add_history(stream_id, server_history) {
|
export function add_history(stream_id: number, server_history: ServerTopicHistoryEntry[]): void {
|
||||||
const history = find_or_create(stream_id);
|
const history = find_or_create(stream_id);
|
||||||
history.add_history(server_history);
|
history.add_history(server_history);
|
||||||
fetched_stream_ids.add(stream_id);
|
fetched_stream_ids.add(stream_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function has_history_for(stream_id) {
|
export function has_history_for(stream_id: number): boolean {
|
||||||
return fetched_stream_ids.has(stream_id);
|
return fetched_stream_ids.has(stream_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function get_recent_topic_names(stream_id) {
|
export function get_recent_topic_names(stream_id: number): string[] {
|
||||||
const history = find_or_create(stream_id);
|
const history = find_or_create(stream_id);
|
||||||
|
|
||||||
return history.get_recent_topic_names();
|
return history.get_recent_topic_names();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function get_max_message_id(stream_id) {
|
export function get_max_message_id(stream_id: number): number {
|
||||||
const history = find_or_create(stream_id);
|
const history = find_or_create(stream_id);
|
||||||
|
|
||||||
return history.get_max_message_id();
|
return history.get_max_message_id();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function reset() {
|
export function reset(): void {
|
||||||
// This is only used by tests.
|
// This is only used by tests.
|
||||||
stream_dict.clear();
|
stream_dict.clear();
|
||||||
fetched_stream_ids.clear();
|
fetched_stream_ids.clear();
|
||||||
request_pending_stream_ids.clear();
|
request_pending_stream_ids.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function is_request_pending_for(stream_id) {
|
export function is_request_pending_for(stream_id: number): boolean {
|
||||||
return request_pending_stream_ids.has(stream_id);
|
return request_pending_stream_ids.has(stream_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function add_request_pending_for(stream_id) {
|
export function add_request_pending_for(stream_id: number): void {
|
||||||
request_pending_stream_ids.add(stream_id);
|
request_pending_stream_ids.add(stream_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function remove_request_pending_for(stream_id) {
|
export function remove_request_pending_for(stream_id: number): void {
|
||||||
request_pending_stream_ids.delete(stream_id);
|
request_pending_stream_ids.delete(stream_id);
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ import * as people from "./people";
|
|||||||
import * as recent_view_util from "./recent_view_util";
|
import * as recent_view_util from "./recent_view_util";
|
||||||
import * as settings_config from "./settings_config";
|
import * as settings_config from "./settings_config";
|
||||||
import * as stream_data from "./stream_data";
|
import * as stream_data from "./stream_data";
|
||||||
|
import type {TopicHistoryEntry} from "./stream_topic_history";
|
||||||
import * as sub_store from "./sub_store";
|
import * as sub_store from "./sub_store";
|
||||||
import type {UpdateMessageEvent} from "./types";
|
import type {UpdateMessageEvent} from "./types";
|
||||||
import {user_settings} from "./user_settings";
|
import {user_settings} from "./user_settings";
|
||||||
@@ -49,13 +50,6 @@ const unread_messages = new Set<number>();
|
|||||||
// for how we can refresh it efficiently.
|
// for how we can refresh it efficiently.
|
||||||
export const unread_mention_topics = new Map();
|
export const unread_mention_topics = new Map();
|
||||||
|
|
||||||
type Topic = {
|
|
||||||
message_id: number;
|
|
||||||
pretty_name: string;
|
|
||||||
historical: boolean;
|
|
||||||
count: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
type StreamCountInfo = {
|
type StreamCountInfo = {
|
||||||
unmuted_count: number;
|
unmuted_count: number;
|
||||||
muted_count: number;
|
muted_count: number;
|
||||||
@@ -307,7 +301,7 @@ class UnreadTopicCounter {
|
|||||||
|
|
||||||
get_missing_topics(opts: {
|
get_missing_topics(opts: {
|
||||||
stream_id: number;
|
stream_id: number;
|
||||||
topic_dict: FoldDict<Topic>;
|
topic_dict: FoldDict<TopicHistoryEntry>;
|
||||||
}): {pretty_name: string; message_id: number}[] {
|
}): {pretty_name: string; message_id: number}[] {
|
||||||
/* Clients have essentially complete unread data, but
|
/* Clients have essentially complete unread data, but
|
||||||
* stream_topic_history.is_complete_for_stream_id() can be
|
* stream_topic_history.is_complete_for_stream_id() can be
|
||||||
@@ -992,7 +986,7 @@ export function get_all_msg_ids(): number[] {
|
|||||||
|
|
||||||
export function get_missing_topics(opts: {
|
export function get_missing_topics(opts: {
|
||||||
stream_id: number;
|
stream_id: number;
|
||||||
topic_dict: FoldDict<Topic>;
|
topic_dict: FoldDict<TopicHistoryEntry>;
|
||||||
}): {pretty_name: string; message_id: number}[] {
|
}): {pretty_name: string; message_id: number}[] {
|
||||||
return unread_topic_counter.get_missing_topics(opts);
|
return unread_topic_counter.get_missing_topics(opts);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user