typing_status: Convert to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2019-10-11 19:48:15 -07:00 committed by Tim Abbott
parent 173c9cee42
commit a3475b422d
6 changed files with 40 additions and 52 deletions

View File

@ -193,7 +193,6 @@
"typing": false,
"typing_data": false,
"typing_events": false,
"typing_status": false,
"ui": false,
"ui_init": false,
"ui_report": false,

View File

@ -1,7 +1,7 @@
zrequire('typing');
zrequire('people');
zrequire('compose_pm_pill');
zrequire('typing_status');
const typing_status = zrequire('typing_status');
function return_false() { return false; }
function return_true() { return true; }
@ -296,9 +296,9 @@ run_test('basics', () => {
// stub functions to see how may time they are called
for (const method in call_count) {
if (!call_count.hasOwnProperty(method)) { continue; }
typing_status[method] = function () {
typing_status.__Rewire__(method, function () {
call_count[method] += 1;
};
});
}
// User ids of poeple in compose narrow doesn't change and is same as stat.current_recipent

View File

@ -189,7 +189,6 @@ import "../emoji.js";
import "../bot_data.js";
import "../reactions.js";
import "../typing.js";
import "../typing_status.js";
import "../typing_data.js";
import "../typing_events.js";
import "../ui_init.js";

View File

@ -159,7 +159,6 @@ declare var typeahead_helper: any;
declare var typing_data: any;
declare var typing_events: any;
declare var typing: any;
declare var typing_status: any;
declare var ui_init: any;
declare var ui: any;
declare var ui_report: any;

View File

@ -1,3 +1,5 @@
const typing_status = require("./typing_status");
var typing = (function () {
var exports = {};

View File

@ -1,6 +1,4 @@
var typing_status = (function () {
var exports = {};
import _ from "underscore";
// See docs/subsystems/typing-indicators.md for details on typing indicators.
@ -32,44 +30,44 @@ var TYPING_STOPPED_WAIT_PERIOD = 5000; // 5s
appropriately.)
*/
exports.state = {};
export const state = {};
exports.initialize_state = function () {
exports.state.current_recipient = undefined;
exports.state.next_send_start_time = undefined;
exports.state.idle_timer = undefined;
};
export function initialize_state() {
state.current_recipient = undefined;
state.next_send_start_time = undefined;
state.idle_timer = undefined;
}
exports.initialize_state();
initialize_state();
exports.stop_last_notification = function stop_last_notification(worker) {
if (exports.state.idle_timer) {
clearTimeout(exports.state.idle_timer);
export function stop_last_notification(worker) {
if (state.idle_timer) {
clearTimeout(state.idle_timer);
}
worker.notify_server_stop(exports.state.current_recipient);
exports.initialize_state();
};
worker.notify_server_stop(state.current_recipient);
initialize_state();
}
exports.start_or_extend_idle_timer = function start_or_extend_idle_timer(worker) {
export function start_or_extend_idle_timer(worker) {
function on_idle_timeout() {
// We don't do any real error checking here, because
// if we've been idle, we need to tell folks, and if
// our current recipient has changed, previous code will
// have stopped the timer.
exports.stop_last_notification(worker);
stop_last_notification(worker);
}
if (exports.state.idle_timer) {
clearTimeout(exports.state.idle_timer);
if (state.idle_timer) {
clearTimeout(state.idle_timer);
}
exports.state.idle_timer = setTimeout(
state.idle_timer = setTimeout(
on_idle_timeout,
TYPING_STOPPED_WAIT_PERIOD
);
};
}
function set_next_start_time(current_time) {
exports.state.next_send_start_time = current_time + TYPING_STARTED_WAIT_PERIOD;
state.next_send_start_time = current_time + TYPING_STARTED_WAIT_PERIOD;
}
function actually_ping_server(worker, recipient, current_time) {
@ -77,16 +75,16 @@ function actually_ping_server(worker, recipient, current_time) {
set_next_start_time(current_time);
}
exports.maybe_ping_server = function maybe_ping_server(worker, recipient) {
export function maybe_ping_server(worker, recipient) {
var current_time = worker.get_current_time();
if (current_time > exports.state.next_send_start_time) {
if (current_time > state.next_send_start_time) {
actually_ping_server(worker, recipient, current_time);
}
};
}
exports.handle_text_input = function (worker) {
export function handle_text_input(worker) {
var new_recipient = worker.get_recipient();
var current_recipient = exports.state.current_recipient;
var current_recipient = state.current_recipient;
if (current_recipient) {
// We need to use _.isEqual for comparisons; === doesn't work
@ -94,10 +92,10 @@ exports.handle_text_input = function (worker) {
if (_.isEqual(new_recipient, current_recipient)) {
// Nothing has really changed, except we may need
// to send a ping to the server.
exports.maybe_ping_server(worker, new_recipient);
maybe_ping_server(worker, new_recipient);
// We can also extend out our idle time.
exports.start_or_extend_idle_timer(worker);
start_or_extend_idle_timer(worker);
return;
}
@ -105,7 +103,7 @@ exports.handle_text_input = function (worker) {
// We apparently stopped talking to our old recipient,
// so we must stop the old notification. Don't return
// yet, because we may have a new recipient.
exports.stop_last_notification(worker);
stop_last_notification(worker);
}
if (!worker.is_valid_conversation(new_recipient)) {
@ -116,26 +114,17 @@ exports.handle_text_input = function (worker) {
// We just started talking to this recipient, so notify
// the server.
exports.state.current_recipient = new_recipient;
state.current_recipient = new_recipient;
var current_time = worker.get_current_time();
actually_ping_server(worker, new_recipient, current_time);
exports.start_or_extend_idle_timer(worker);
};
start_or_extend_idle_timer(worker);
}
exports.stop = function (worker) {
export function stop(worker) {
// We get this if somebody closes the compose box, but
// it doesn't necessarily mean we had typing indicators
// active before this.
if (exports.state.current_recipient) {
exports.stop_last_notification(worker);
if (state.current_recipient) {
stop_last_notification(worker);
}
};
return exports;
}());
if (typeof module !== 'undefined') {
module.exports = typing_status;
}
window.typing_status = typing_status;