Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

activity: Replace $.idle plugin with simple _.debounce use.

Anders Kaseorgd1d85a3d67f3
diff --git a/web/src/activity.ts b/web/src/activity.ts
index 38656137e7c..f913fc4593a 100644
--- a/web/src/activity.ts
+++ b/web/src/activity.ts
@@ -1,4 +1,5 @@
import $ from "jquery";
+import _ from "lodash";
import assert from "minimalistic-assert";
import * as z from "zod/mini";
@@ -85,6 +86,8 @@ export function mark_client_idle(): void {
client_is_active = false;
}
+export const mark_client_idle_later = _.debounce(mark_client_idle, DEFAULT_IDLE_TIMEOUT_MS);
+
export function compute_active_status(): ActivityState {
// The overall algorithm intent for the `status` field is to send
// `ACTIVE` (aka green circle) if we know the user is at their
@@ -181,6 +184,7 @@ export function mark_client_active(): void {
client_is_active = true;
send_presence_to_server();
}
+ mark_client_idle_later();
}
export function initialize(): void {
@@ -188,11 +192,11 @@ export function initialize(): void {
set_new_user_input(true);
});
- $(window).on("focus", mark_client_active);
- $(window).idle({
- idle: DEFAULT_IDLE_TIMEOUT_MS,
- onIdle: mark_client_idle,
- onActive: mark_client_active,
- keepTracking: true,
- });
+ $(window).on(
+ "focus keydown mousedown mousemove touchmove touchstart wheel",
+ mark_client_active,
+ );
+ if (client_is_active) {
+ mark_client_idle_later();
+ }
}
diff --git a/web/tests/activity_init.test.cjs b/web/tests/activity_init.test.cjs
index 94a4733b76d..71166bbe87c 100644
--- a/web/tests/activity_init.test.cjs
+++ b/web/tests/activity_init.test.cjs
@@ -27,13 +27,13 @@ run_test("activity.initialize", ({override}) => {
set_new_user_input(true);
});
- $(window).on("focus", mark_client_active);
- $(window).idle({
- idle: DEFAULT_IDLE_TIMEOUT_MS,
- onIdle: mark_client_idle,
- onActive: mark_client_active,
- keepTracking: true,
- });
+ $(window).on(
+ "focus keydown mousedown mousemove touchmove touchstart wheel",
+ mark_client_active,
+ );
+ if (client_is_active) {
+ mark_client_idle_later();
+ }
}
*/
const $document_stub = $("document-stub");
@@ -45,19 +45,10 @@ run_test("activity.initialize", ({override}) => {
};
$window_stub.on = (event, callback) => {
- assert.equal(event, "focus");
+ assert.equal(event, "focus keydown mousedown mousemove touchmove touchstart wheel");
assert.equal(callback, activity.mark_client_active);
};
- $window_stub.idle = (info) => {
- assert.deepEqual(info, {
- idle: 300000,
- keepTracking: true,
- onActive: activity.mark_client_active,
- onIdle: activity.mark_client_idle,
- });
- };
-
override(window, "to_$", () => $window_stub);
override(document, "to_$", () => $document_stub);