2019-06-27 04:36:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import MatrixClientPeg from "../MatrixClientPeg";
|
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2019-06-27 16:27:11 +00:00
|
|
|
import Timer from "../utils/Timer";
|
2019-06-27 04:36:55 +00:00
|
|
|
|
|
|
|
export const TYPING_SERVER_TIMEOUT = 30000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks typing state for users.
|
|
|
|
*/
|
|
|
|
export default class TypingStore {
|
|
|
|
constructor() {
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
static sharedInstance(): TypingStore {
|
|
|
|
if (global.mxTypingStore === undefined) {
|
|
|
|
global.mxTypingStore = new TypingStore();
|
|
|
|
}
|
|
|
|
return global.mxTypingStore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all cached typing states. Intended to be called when the
|
|
|
|
* MatrixClientPeg client changes.
|
|
|
|
*/
|
|
|
|
reset() {
|
2019-06-27 16:27:11 +00:00
|
|
|
this._typingStates = {}; // roomId => { isTyping, Timer }
|
2019-06-27 04:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the typing status for the MatrixClientPeg user.
|
|
|
|
* @param {string} roomId The room ID to set the typing state in.
|
|
|
|
* @param {boolean} isTyping Whether the user is typing or not.
|
|
|
|
*/
|
|
|
|
setSelfTyping(roomId: string, isTyping: boolean): void {
|
|
|
|
if (!SettingsStore.getValue('sendTypingNotifications')) return;
|
|
|
|
if (SettingsStore.getValue('lowBandwidth')) return;
|
|
|
|
|
2019-06-27 16:27:11 +00:00
|
|
|
let currentTyping = this._typingStates[roomId];
|
2019-06-27 04:36:55 +00:00
|
|
|
if ((!isTyping && !currentTyping) || (currentTyping && currentTyping.isTyping === isTyping)) {
|
|
|
|
// No change in state, so don't do anything. We'll let the timer run its course.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-27 16:27:11 +00:00
|
|
|
if (!currentTyping) {
|
|
|
|
currentTyping = this._typingStates[roomId] = {
|
|
|
|
isTyping: isTyping,
|
|
|
|
timer: new Timer(TYPING_SERVER_TIMEOUT),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTyping.isTyping = isTyping;
|
2019-06-27 04:36:55 +00:00
|
|
|
|
|
|
|
if (isTyping) {
|
2019-06-27 16:27:11 +00:00
|
|
|
currentTyping.timer.restart();
|
|
|
|
currentTyping.timer.finished().then(() => {
|
2019-06-27 04:36:55 +00:00
|
|
|
const currentTyping = this._typingStates[roomId];
|
2019-06-27 16:27:11 +00:00
|
|
|
if (currentTyping) currentTyping.isTyping = false;
|
2019-06-27 04:36:55 +00:00
|
|
|
|
2019-06-27 16:27:11 +00:00
|
|
|
// The server will (should) time us out on typing, so we don't
|
|
|
|
// need to advertise a stop of typing.
|
|
|
|
});
|
2019-06-27 04:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MatrixClientPeg.get().sendTyping(roomId, isTyping, TYPING_SERVER_TIMEOUT);
|
|
|
|
}
|
2019-06-27 04:40:08 +00:00
|
|
|
}
|