Files
zulip/static/js/typing_data.js
Anders Kaseorg 6ec808b8df js: Add "use strict" directive to CommonJS files.
ES and TypeScript modules are strict by default and don’t need this
directive.  ESLint will remind us to add it to new CommonJS files and
remove it from ES and TypeScript modules.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-07-31 22:09:46 -07:00

76 lines
1.9 KiB
JavaScript

"use strict";
const _ = require("lodash");
const util = require("./util");
// See docs/subsystems/typing-indicators.md for details on typing indicators.
const typist_dct = new Map();
const inbound_timer_dict = new Map();
function to_int(s) {
return parseInt(s, 10);
}
function get_key(group) {
const ids = util.sorted_ids(group);
return ids.join(",");
}
exports.add_typist = function (group, typist) {
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));
};
exports.remove_typist = function (group, typist) {
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));
typist_dct.set(key, current);
return true;
};
exports.get_group_typists = function (group) {
const key = get_key(group);
return typist_dct.get(key) || [];
};
exports.get_all_typists = function () {
let typists = [].concat(...Array.from(typist_dct.values()));
typists = util.sorted_ids(typists);
typists = _.sortedUniq(typists);
return typists;
};
// The next functions aren't pure data, but it is easy
// enough to mock the setTimeout/clearTimeout functions.
exports.clear_inbound_timer = function (group) {
const key = get_key(group);
const timer = inbound_timer_dict.get(key);
if (timer) {
clearTimeout(timer);
inbound_timer_dict.set(key, undefined);
}
};
exports.kickstart_inbound_timer = function (group, delay, callback) {
const key = get_key(group);
exports.clear_inbound_timer(group);
const timer = setTimeout(callback, delay);
inbound_timer_dict.set(key, timer);
};
window.typing_data = exports;