ts: Convert typing_data.js to TypeScript.

This commit is contained in:
Lalit
2023-02-25 13:14:21 +05:30
committed by Tim Abbott
parent 88c23933bb
commit 1b7f3eeb76

View File

@@ -3,55 +3,49 @@ import * as util from "./util";
// See docs/subsystems/typing-indicators.md for details on typing indicators.
const typist_dct = new Map();
const inbound_timer_dict = new Map();
const typist_dct = new Map<string, number[]>();
const inbound_timer_dict = new Map<string, ReturnType<typeof setInterval> | undefined>();
export function clear_for_testing() {
export function clear_for_testing(): void {
typist_dct.clear();
inbound_timer_dict.clear();
}
function to_int(s) {
return Number.parseInt(s, 10);
}
function get_key(group) {
function get_key(group: number[]): string {
const ids = util.sorted_ids(group);
return ids.join(",");
}
export function add_typist(group, typist) {
export function add_typist(group: number[], typist: number): void {
const key = get_key(group);
const current = typist_dct.get(key) || [];
typist = to_int(typist);
if (!current.includes(typist)) {
current.push(typist);
}
typist_dct.set(key, util.sorted_ids(current));
}
export function remove_typist(group, typist) {
export function remove_typist(group: number[], typist: number): boolean {
const key = get_key(group);
let current = typist_dct.get(key) || [];
typist = to_int(typist);
if (!current.includes(typist)) {
return false;
}
current = current.filter((user_id) => to_int(user_id) !== to_int(typist));
current = current.filter((user_id) => user_id !== typist);
typist_dct.set(key, current);
return true;
}
export function get_group_typists(group) {
export function get_group_typists(group: number[]): number[] {
const key = get_key(group);
const user_ids = typist_dct.get(key) || [];
return muted_users.filter_muted_user_ids(user_ids);
}
export function get_all_typists() {
export function get_all_typists(): number[] {
let typists = [...typist_dct.values()].flat();
typists = util.sorted_ids(typists);
return muted_users.filter_muted_user_ids(typists);
@@ -59,7 +53,7 @@ export function get_all_typists() {
// The next functions aren't pure data, but it is easy
// enough to mock the setTimeout/clearTimeout functions.
export function clear_inbound_timer(group) {
export function clear_inbound_timer(group: number[]): void {
const key = get_key(group);
const timer = inbound_timer_dict.get(key);
if (timer) {
@@ -68,7 +62,11 @@ export function clear_inbound_timer(group) {
}
}
export function kickstart_inbound_timer(group, delay, callback) {
export function kickstart_inbound_timer(
group: number[],
delay: number,
callback: () => void,
): void {
const key = get_key(group);
clear_inbound_timer(group);
const timer = setTimeout(callback, delay);