From a3475b422da7f5bd7526ca080477892d36f7b99b Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 11 Oct 2019 19:48:15 -0700 Subject: [PATCH] typing_status: Convert to ES6 module. Signed-off-by: Anders Kaseorg --- .eslintrc.json | 1 - frontend_tests/node_tests/typing_status.js | 6 +- static/js/bundles/app.js | 1 - static/js/js_typings/zulip/index.d.ts | 1 - static/js/typing.js | 2 + static/js/typing_status.js | 81 ++++++++++------------ 6 files changed, 40 insertions(+), 52 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 4ed5a6f727..d19483cc42 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -193,7 +193,6 @@ "typing": false, "typing_data": false, "typing_events": false, - "typing_status": false, "ui": false, "ui_init": false, "ui_report": false, diff --git a/frontend_tests/node_tests/typing_status.js b/frontend_tests/node_tests/typing_status.js index aef577f55f..dec95a5944 100644 --- a/frontend_tests/node_tests/typing_status.js +++ b/frontend_tests/node_tests/typing_status.js @@ -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 diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index 0a57707631..7a2b962c72 100644 --- a/static/js/bundles/app.js +++ b/static/js/bundles/app.js @@ -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"; diff --git a/static/js/js_typings/zulip/index.d.ts b/static/js/js_typings/zulip/index.d.ts index f6182ec5fe..443c55edf2 100644 --- a/static/js/js_typings/zulip/index.d.ts +++ b/static/js/js_typings/zulip/index.d.ts @@ -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; diff --git a/static/js/typing.js b/static/js/typing.js index 8097da8246..258e658464 100644 --- a/static/js/typing.js +++ b/static/js/typing.js @@ -1,3 +1,5 @@ +const typing_status = require("./typing_status"); + var typing = (function () { var exports = {}; diff --git a/static/js/typing_status.js b/static/js/typing_status.js index 058b1cf7d6..08fb08b9c5 100644 --- a/static/js/typing_status.js +++ b/static/js/typing_status.js @@ -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;