2017-03-22 00:41:09 +01:00
|
|
|
var typing_status = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2017-11-08 17:55:36 +01:00
|
|
|
// See docs/subsystems/typing-indicators.md for details on typing indicators.
|
2017-09-25 20:33:29 +02:00
|
|
|
|
2017-03-22 00:41:09 +01:00
|
|
|
// The following constants are tuned to work with
|
|
|
|
// TYPING_STARTED_EXPIRY_PERIOD, which is what the other
|
|
|
|
// users will use to time out our messages. (Or us,
|
2017-03-22 15:11:41 +01:00
|
|
|
// depending on your perspective.) See typing_events.js.
|
2017-03-22 00:41:09 +01:00
|
|
|
|
|
|
|
// How frequently 'still typing' notifications are sent
|
|
|
|
// to extend the expiry
|
|
|
|
var TYPING_STARTED_WAIT_PERIOD = 10000; // 10s
|
|
|
|
// How long after someone stops editing in the compose box
|
|
|
|
// do we send a 'stopped typing' notification
|
|
|
|
var TYPING_STOPPED_WAIT_PERIOD = 5000; // 5s
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Our parent should pass in a worker object with the following
|
|
|
|
callbacks:
|
|
|
|
|
|
|
|
notify_server_start
|
|
|
|
notify_server_stop
|
|
|
|
get_recipient
|
|
|
|
get_current_time
|
|
|
|
is_valid_conversation
|
|
|
|
|
|
|
|
See typing.js for the implementations of the above. (Our
|
|
|
|
node tests also act as workers and will stub those functions
|
|
|
|
appropriately.)
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.state = {};
|
|
|
|
|
|
|
|
exports.initialize_state = function () {
|
|
|
|
exports.state.current_recipient = undefined;
|
|
|
|
exports.state.next_send_start_time = undefined;
|
|
|
|
exports.state.idle_timer = undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.initialize_state();
|
|
|
|
|
2019-06-07 03:17:35 +02:00
|
|
|
exports.stop_last_notification = function stop_last_notification(worker) {
|
2017-03-22 00:41:09 +01:00
|
|
|
var state = exports.state;
|
|
|
|
if (state.idle_timer) {
|
|
|
|
clearTimeout(state.idle_timer);
|
|
|
|
}
|
|
|
|
worker.notify_server_stop(state.current_recipient);
|
|
|
|
exports.initialize_state();
|
2019-06-07 03:17:35 +02:00
|
|
|
};
|
2017-03-22 00:41:09 +01:00
|
|
|
|
2019-06-07 03:17:35 +02:00
|
|
|
exports.start_or_extend_idle_timer = function start_or_extend_idle_timer(worker) {
|
2017-03-22 00:41:09 +01:00
|
|
|
var state = exports.state;
|
|
|
|
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.
|
2019-06-07 03:17:35 +02:00
|
|
|
exports.stop_last_notification(worker);
|
2017-03-22 00:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state.idle_timer) {
|
|
|
|
clearTimeout(state.idle_timer);
|
|
|
|
}
|
|
|
|
state.idle_timer = setTimeout(
|
|
|
|
on_idle_timeout,
|
|
|
|
TYPING_STOPPED_WAIT_PERIOD
|
|
|
|
);
|
2019-06-07 03:17:35 +02:00
|
|
|
};
|
2017-03-22 00:41:09 +01:00
|
|
|
|
|
|
|
function set_next_start_time(current_time) {
|
|
|
|
exports.state.next_send_start_time = current_time + TYPING_STARTED_WAIT_PERIOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
function actually_ping_server(worker, recipient, current_time) {
|
|
|
|
worker.notify_server_start(recipient);
|
|
|
|
set_next_start_time(current_time);
|
|
|
|
}
|
|
|
|
|
2019-06-07 03:17:35 +02:00
|
|
|
exports.maybe_ping_server = function maybe_ping_server(worker, recipient) {
|
2017-03-22 00:41:09 +01:00
|
|
|
var current_time = worker.get_current_time();
|
|
|
|
if (current_time > exports.state.next_send_start_time) {
|
|
|
|
actually_ping_server(worker, recipient, current_time);
|
|
|
|
}
|
2019-06-07 03:17:35 +02:00
|
|
|
};
|
2017-03-22 00:41:09 +01:00
|
|
|
|
|
|
|
exports.handle_text_input = function (worker) {
|
|
|
|
var new_recipient = worker.get_recipient();
|
|
|
|
var current_recipient = exports.state.current_recipient;
|
|
|
|
|
|
|
|
if (current_recipient) {
|
2019-06-06 21:54:55 +02:00
|
|
|
// We need to use _.isEqual for comparisons; === doesn't work
|
|
|
|
// on arrays.
|
|
|
|
if (_.isEqual(new_recipient, current_recipient)) {
|
2017-03-22 00:41:09 +01:00
|
|
|
// Nothing has really changed, except we may need
|
|
|
|
// to send a ping to the server.
|
2019-06-07 03:17:35 +02:00
|
|
|
exports.maybe_ping_server(worker, new_recipient);
|
2017-03-22 00:41:09 +01:00
|
|
|
|
|
|
|
// We can also extend out our idle time.
|
2019-06-07 03:17:35 +02:00
|
|
|
exports.start_or_extend_idle_timer(worker);
|
2017-03-22 00:41:09 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2019-06-07 03:17:35 +02:00
|
|
|
exports.stop_last_notification(worker);
|
2017-03-22 00:41:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!worker.is_valid_conversation(new_recipient)) {
|
|
|
|
// If we are not talking to somebody we care about,
|
|
|
|
// then there is no more action to take.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We just started talking to this recipient, so notify
|
|
|
|
// the server.
|
|
|
|
exports.state.current_recipient = new_recipient;
|
|
|
|
var current_time = worker.get_current_time();
|
|
|
|
actually_ping_server(worker, new_recipient, current_time);
|
2019-06-07 03:17:35 +02:00
|
|
|
exports.start_or_extend_idle_timer(worker);
|
2017-03-22 00:41:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.stop = function (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) {
|
2019-06-07 03:17:35 +02:00
|
|
|
exports.stop_last_notification(worker);
|
2017-03-22 00:41:09 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = typing_status;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.typing_status = typing_status;
|