From 17ed62de7db29714ed5559283dd1138fb486ed73 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 27 Jun 2019 10:35:44 -0600 Subject: [PATCH] Move MessageComposer typing timeout to TypingStore --- .../views/rooms/MessageComposerInput.js | 50 +------------------ src/stores/TypingStore.js | 21 ++++++-- 2 files changed, 18 insertions(+), 53 deletions(-) diff --git a/src/components/views/rooms/MessageComposerInput.js b/src/components/views/rooms/MessageComposerInput.js index b61e1191b6..274e347c5f 100644 --- a/src/components/views/rooms/MessageComposerInput.js +++ b/src/components/views/rooms/MessageComposerInput.js @@ -62,12 +62,10 @@ import {ContentHelpers} from 'matrix-js-sdk'; import AccessibleButton from '../elements/AccessibleButton'; import {findEditableEvent} from '../../../utils/EventUtils'; import ComposerHistoryManager from "../../../ComposerHistoryManager"; -import TypingStore, {TYPING_SERVER_TIMEOUT} from "../../../stores/TypingStore"; +import TypingStore from "../../../stores/TypingStore"; const REGEX_EMOTICON_WHITESPACE = new RegExp('(?:^|\\s)(' + EMOTICON_REGEX.source + ')\\s$'); -const TYPING_USER_TIMEOUT = 10000; - // the Slate node type to default to for unstyled text const DEFAULT_NODE = 'paragraph'; @@ -427,55 +425,11 @@ export default class MessageComposerInput extends React.Component { }; onTypingActivity() { - this.isTyping = true; - if (!this.userTypingTimer) { - this.sendTyping(true); - } - this.startUserTypingTimer(); - this.startServerTypingTimer(); + this.sendTyping(true); } onFinishedTyping() { - this.isTyping = false; this.sendTyping(false); - this.stopUserTypingTimer(); - this.stopServerTypingTimer(); - } - - startUserTypingTimer() { - this.stopUserTypingTimer(); - const self = this; - this.userTypingTimer = setTimeout(function() { - self.isTyping = false; - self.sendTyping(self.isTyping); - self.userTypingTimer = null; - }, TYPING_USER_TIMEOUT); - } - - stopUserTypingTimer() { - if (this.userTypingTimer) { - clearTimeout(this.userTypingTimer); - this.userTypingTimer = null; - } - } - - startServerTypingTimer() { - if (!this.serverTypingTimer) { - const self = this; - this.serverTypingTimer = setTimeout(function() { - if (self.isTyping) { - self.sendTyping(self.isTyping); - self.startServerTypingTimer(); - } - }, TYPING_SERVER_TIMEOUT / 2); - } - } - - stopServerTypingTimer() { - if (this.serverTypingTimer) { - clearTimeout(this.serverTypingTimer); - this.serverTypingTimer = null; - } } sendTyping(isTyping) { diff --git a/src/stores/TypingStore.js b/src/stores/TypingStore.js index 17be8ca14b..02778ee4f4 100644 --- a/src/stores/TypingStore.js +++ b/src/stores/TypingStore.js @@ -18,7 +18,8 @@ import MatrixClientPeg from "../MatrixClientPeg"; import SettingsStore from "../settings/SettingsStore"; import Timer from "../utils/Timer"; -export const TYPING_SERVER_TIMEOUT = 30000; +const TYPING_USER_TIMEOUT = 10000; +const TYPING_SERVER_TIMEOUT = 30000; /** * Tracks typing state for users. @@ -40,7 +41,13 @@ export default class TypingStore { * MatrixClientPeg client changes. */ reset() { - this._typingStates = {}; // roomId => { isTyping, Timer } + this._typingStates = { + // "roomId": { + // isTyping: bool, // Whether the user is typing or not + // userTimer: Timer, // Local timeout for "user has stopped typing" + // serverTimer: Timer, // Maximum timeout for the typing state + // }, + }; } /** @@ -61,21 +68,25 @@ export default class TypingStore { if (!currentTyping) { currentTyping = this._typingStates[roomId] = { isTyping: isTyping, - timer: new Timer(TYPING_SERVER_TIMEOUT), + serverTimer: new Timer(TYPING_SERVER_TIMEOUT), + userTimer: new Timer(TYPING_USER_TIMEOUT), }; } currentTyping.isTyping = isTyping; if (isTyping) { - currentTyping.timer.restart(); - currentTyping.timer.finished().then(() => { + currentTyping.serverTimer.restart().finished().then(() => { const currentTyping = this._typingStates[roomId]; if (currentTyping) currentTyping.isTyping = false; // The server will (should) time us out on typing, so we don't // need to advertise a stop of typing. }); + + currentTyping.userTimer.restart().finished().then(() => { + this.setSelfTyping(roomId, false); + }); } MatrixClientPeg.get().sendTyping(roomId, isTyping, TYPING_SERVER_TIMEOUT);