Use a Timer

This commit is contained in:
Travis Ralston 2019-06-27 10:27:11 -06:00
parent 67ecf9db62
commit eb1f911d15

View file

@ -16,6 +16,7 @@ limitations under the License.
import MatrixClientPeg from "../MatrixClientPeg"; import MatrixClientPeg from "../MatrixClientPeg";
import SettingsStore from "../settings/SettingsStore"; import SettingsStore from "../settings/SettingsStore";
import Timer from "../utils/Timer";
export const TYPING_SERVER_TIMEOUT = 30000; export const TYPING_SERVER_TIMEOUT = 30000;
@ -39,7 +40,7 @@ export default class TypingStore {
* MatrixClientPeg client changes. * MatrixClientPeg client changes.
*/ */
reset() { reset() {
this._typingStates = {}; // roomId => { isTyping, expireTs } this._typingStates = {}; // roomId => { isTyping, Timer }
} }
/** /**
@ -51,27 +52,30 @@ export default class TypingStore {
if (!SettingsStore.getValue('sendTypingNotifications')) return; if (!SettingsStore.getValue('sendTypingNotifications')) return;
if (SettingsStore.getValue('lowBandwidth')) return; if (SettingsStore.getValue('lowBandwidth')) return;
const currentTyping = this._typingStates[roomId]; let currentTyping = this._typingStates[roomId];
if ((!isTyping && !currentTyping) || (currentTyping && currentTyping.isTyping === isTyping)) { if ((!isTyping && !currentTyping) || (currentTyping && currentTyping.isTyping === isTyping)) {
// No change in state, so don't do anything. We'll let the timer run its course. // No change in state, so don't do anything. We'll let the timer run its course.
return; return;
} }
const now = new Date().getTime(); if (!currentTyping) {
this._typingStates[roomId] = { currentTyping = this._typingStates[roomId] = {
isTyping: isTyping, isTyping: isTyping,
expireTs: now + TYPING_SERVER_TIMEOUT, timer: new Timer(TYPING_SERVER_TIMEOUT),
}; };
}
currentTyping.isTyping = isTyping;
if (isTyping) { if (isTyping) {
setTimeout(() => { currentTyping.timer.restart();
currentTyping.timer.finished().then(() => {
const currentTyping = this._typingStates[roomId]; const currentTyping = this._typingStates[roomId];
const now = new Date().getTime(); if (currentTyping) currentTyping.isTyping = false;
if (currentTyping && currentTyping.expireTs >= now) { // The server will (should) time us out on typing, so we don't
currentTyping.isTyping = false; // need to advertise a stop of typing.
} });
}, TYPING_SERVER_TIMEOUT);
} }
MatrixClientPeg.get().sendTyping(roomId, isTyping, TYPING_SERVER_TIMEOUT); MatrixClientPeg.get().sendTyping(roomId, isTyping, TYPING_SERVER_TIMEOUT);